Dagger2 依赖项 - Gradle

Dagger2 dependency - Gradle

我正在尝试将 Dagger2 添加到我在 Android Studio 中的项目中,但我找不到合适的依赖项来粘贴到 build.gradle 中。你能帮忙把正确的线路发给我吗?

dependencies {
    implementation 'com.google.dagger:dagger:2.0-SNAPSHOT'
}

在你的 app/build.gradle

allprojects {
    repositories {
        ...
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }
    }
}

在您项目的 build.gradle 中。

Installing Dagger 2 on Android Studio 2

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.4'
    annotationProcessor "com.google.dagger:dagger-compiler:2.4"
}

Maven 存储库:

在Maven Repository中找到上述依赖的最新版本:


备注:Android Studio < 2.2

旧版本的 Android Studio 需要 android-apt 进行注释处理。

// 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

对于 Dagger < 2.1-SNAPSHOT,Dagger 生成的代码中使用的 @Generated 注释需要 javax.annotation(参见 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'
}

今天早些时候我遇到了一些麻烦。使用 Android Studio 2.0 预览版 8:

以下是截止到此日期对我有用的最新版本

buid.gradle(模块:应用程序)

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'
    defaultConfig {
        applicationId 'com.example.android.redacted.app'
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    repositories {
        mavenCentral()
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.google.dagger:dagger:2.0.2'
    apt 'com.google.dagger:dagger-compiler:2.0.2'
    provided 'org.glassfish:javax.annotation:10.0-b28'

}

build.gradle(项目:项目):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha8'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }

}

您不再需要 android-apt 插件,以前由 android-apt 提供的所有功能现在都可以在Android Gradle 插件版本 2.2

https://bitbucket.org/hvisser/android-apt/wiki/Migration

将 Gradle 插件更新为

classpath 'com.android.tools.build:gradle:2.2.0'

Dagger 依赖于

compile 'com.google.dagger:dagger:2.4'
annotationProcessor 'com.google.dagger:dagger-compiler:2.4'

干杯!

在 app/build.gradle 中添加这些最新的依赖项,使用最新版本的 Android studios 3.0

dependencies {

//Dagger
implementation 'com.google.dagger:dagger:2.24'
implementation 'com.google.dagger:dagger-android:2.24'
implementation 'com.google.dagger:dagger-android-support:2.24'

}

Dagger 2 的简单实现Dagger 2 with MVP

正在更新最新版本的 Dagger-2 依赖项

当前版本:2.36。您可以找到以下所有依赖项的最新版本 here.

对于核心匕首依赖项,

dependencies {
  implementation 'com.google.dagger:dagger:2.36'
  annotationProcessor 'com.google.dagger:dagger-compiler:2.36'
}

对于 android 个依赖项,

implementation 'com.google.dagger:dagger-android:2.36'
implementation 'com.google.dagger:dagger-android-support:2.36'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.36'

如果您的项目在 Gradle.

中使用 Kotlin, then use kapt instead of annotationProcessor. To use kapt, you need to add the plugin kotlin-kapt