问题描述
我正在尝试将Dagger2添加到我在Android Studio中的项目中,但是找不到合适的依赖项粘贴到build.gradle中.您能帮忙给我发送正确的电话吗?
I'm trying to add Dagger2 to my project in Android Studio but I can't find proper dependency to paste in build.gradle. Could you help and send me the proper line?
推荐答案
在Android Studio 2上安装Dagger 2
// Application build.gradle
dependencies {
compile 'com.google.dagger:dagger:2.4'
annotationProcessor "com.google.dagger:dagger-compiler:2.4"
}
Find the latest versions of the above dependencies in the Maven Repository:
- dagger
- dagger-compiler
较旧版本的Android Studio需要android-apt
进行注释处理.
Older versions of Android Studio need android-apt
for annotation processing.
// Project build.gradle
buildscript {
dependencies {
// Assists in working with annotation processors for Android Studio.
// No longer needed with Android Studio 2.2+
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
apply plugin: 'com.neenbedankt.android-apt'
还有
// Application build.gradle
dependencies {
compile 'com.google.dagger:dagger:2.4'
apt "com.google.dagger:dagger-compiler:2.4"
}
注意:匕首< 2.1
匕首< 2.1-SNAPSHOT Dagger生成的代码中使用的@Generated
批注需要javax.annotation
(请参见 github.com/google/dagger/issues/95 ).该注释未包含在Android API jar中,因此您需要使用以下库之一(查看差异):
Notes: Dagger < 2.1
For Dagger < 2.1-SNAPSHOT the javax.annotation
is needed for the @Generated
annotation used in Dagger generated code (see github.com/google/dagger/issues/95). The annotation is not included in the Android API jar, so you'll need to use one of these libraries (see differences):
// Application build.gradle
dependencies {
compile 'javax.annotation:jsr250-api:1.0'
}
- javax.annotation:jsr250-api:1.0
- javax.annotation:javax.annotation-api:1.2
- org.glassfish:javax.annotation:10.0-b28
- javax.annotation:jsr250-api:1.0
- javax.annotation:javax.annotation-api:1.2
- org.glassfish:javax.annotation:10.0-b28
这篇关于Dagger2依赖-Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!