找不到导入 javax.inject.Inject 包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19718662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't locate import javax.inject.Inject package
提问by Indigo Nai
I'm trying to implement Dagger as a dependency injector in an IntelliJ project, but my code is failing on:
我正在尝试在 IntelliJ 项目中将 Dagger 实现为依赖项注入器,但我的代码失败了:
import javax.inject.Inject;
Intellij is finding the 'javax
' package, but not the 'inject
' package, so it fails.
Intellij 正在查找“ javax
”包,但未找到“ inject
”包,因此失败。
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?
我是 Android 的新手,所以如果这很简单,我深表歉意,但谁能告诉我为什么找不到注入包?
回答by Jake Wharton
Dagger depends on JSR 330, the Java standard annotations which are used for dependency injection (think: @Inject
, @Singleton
, etc.).
匕首依赖于JSR 330,其用于依赖注入的Java标准注释(认为:@Inject
,@Singleton
,等)。
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.
这是一个单独的 jar,您必须包含在内。如果您使用具有集成依赖项管理(Maven、Gradle、Ant+Ivy、sbt)的构建系统,您将免费获得它。如果您仍然在 jars 周围复制,那么您必须手动添加它。
You can download the latest jar from Maven central(at the bottom).
您可以从Maven 中心(底部)下载最新的 jar 。
回答by Dwight Lisper
add this to your pom.xml
将此添加到您的 pom.xml
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
回答by hasskell
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
如果有人使用纯 Java 项目而不是 Maven 或 Gradle 等,您可以从此处下载单独的 Jar 文件注入 Jar 文件
and then add to your external libraries, in IDEA you can do that as follows: File -> Project Structure -> Libraries -> New Project Library (+)
然后添加到您的外部库中,在 IDEA 中您可以按如下方式进行:文件 -> 项目结构 -> 库 -> 新建项目库 (+)
Then find the path to library and job is done.
然后找到库的路径,工作就完成了。
回答by Liubomyr Protsyk
// dependency injection implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
// 依赖注入实现 "com.google.dagger:dagger:$rootProject.dagger2Version"
// dependency injection
implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
implementation {
exclude(group: 'javax.inject', module: 'javax.inject')
}
回答by donturner
Add the inject library directly from Maven like this:
直接从 Maven 添加注入库,如下所示:
- File -> Project Structure -> Project Settings -> Libraries -> Add -> From Maven
- Search for
javax.inject:javax.inject:1
- After it's been found click OK
- 文件 -> 项目结构 -> 项目设置 -> 库 -> 添加 -> 从 Maven
- 搜索
javax.inject:javax.inject:1
- 找到后点击确定