Can't locate import javax.inject.Inject package

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 23:03:15

问题


I'm trying to implement Dagger as a dependency injector in an IntelliJ project, but my code is failing on:

import javax.inject.Inject;

Intellij is finding the 'javax' package, but not the 'inject' package, so it fails.

I am new to Android, so I apologize if this is a no brainer, but can anyone tell me why the inject package is not being found?


回答1:


Dagger depends on JSR 330, the Java standard annotations which are used for dependency injection (think: @Inject, @Singleton, etc.).

This is a separate jar that you have to include. If you were using a build system with integrated dependency management (Maven, Gradle, Ant+Ivy, sbt) you'd get this for free. If you're still copying around jars then you have to add it manually.

You can download the latest jar from Maven central (at the bottom).




回答2:


add this to your pom.xml

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>



回答3:


In case if anyone using plain Java project not Maven or Gradle or e.t.c. You can download a separate Jar file from here Inject Jar file

and then add to your external libraries, in IDEA you can do that as follows: File -> Project Structure -> Libraries -> New Project Library (+)

Then find the path to library and job is done.




回答4:


// dependency injection implementation "com.google.dagger:dagger:$rootProject.dagger2Version"

// dependency injection
    implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
    implementation {
        exclude(group: 'javax.inject', module: 'javax.inject')
    }


来源:https://stackoverflow.com/questions/19718662/cant-locate-import-javax-inject-inject-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!