如何在数据模块中使用 Android Kotlin Dagger 2 和 Firebase Auth?

How to use Android Kotlin Dagger 2 and Firebase Auth in Data module?

我正在尝试设置一个应用程序,其中 data 模块是一个库,presentation 层是实际的 android 应用程序。理想情况下,我希望 firebase 依赖项只存在于 data 模块中,但是 apply plugin: 'com.google.gms.google-services' 只能从 presentation 模块中调用。

将 dagger 2 和 firebase 依赖项放在一起会出现以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':presentation:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

删除 firebase auth 依赖项或 dagger 2 依赖项可解决此问题。 如何在 data 模块中同时使用这两个库?

我的 build.gradle data

apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'


android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'


    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    compile "android.arch.lifecycle:extensions:1.0.0-alpha9-1"

    compile 'com.google.dagger:dagger:2.11'
    kapt 'com.google.dagger:dagger-compiler:2.11'

    compile 'com.google.firebase:firebase-auth:11.4.2'
}

和build.gradle进行演示:

    apply plugin: 'com.android.application'

    apply plugin: 'kotlin-android'


    android {

        def configs = rootProject.extensions.getByName("ext")

        compileSdkVersion configs["compileSdkVersion"]
        buildToolsVersion configs["buildToolsVersion"]
        defaultConfig {
            applicationId configs["androidApplicationId"]
            minSdkVersion configs["minSdkVersion"]
            targetSdkVersion configs["targetSdkVersion"]
            versionCode configs["versionCode"]
            versionName configs["versionName"]
            testInstrumentationRunner configs["testInstrumentationRunner"]
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        buildToolsVersion '26.0.2'
    }

    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation project(':data')
        implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation "junit:junit:$rootProject.junitVersion"
        androidTestImplementation("com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion", {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    }

    apply plugin: 'com.google.gms.google-services'

这是顶级构建文件(如果需要)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.4-3'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta7'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
    ext {
        //Android
        androidApplicationId = 'com.test.myapp'
        minSdkVersion = 19
        targetSdkVersion = 26
        versionCode = 1
        versionName = "1.0"
        compileSdkVersion = 26
        buildToolsVersion = "26.0.2"

        //Libraries
        supportLibraryVersion = '26.1.0'

        //Testing
        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
        junitVersion = "4.12"
        espressoVersion = "3.0.1"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

问题似乎出在 google-services 版本上。更新播放服务版本后,问题已解决:

classpath 'com.google.gms:google-services:3.1.1'

来自 3.1.0