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

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

把匕首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身份validation依赖关系或匕首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 } 

这似乎是与谷歌服务版本的问题。 更新播放服务版本后,我解决了问题:

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

3.1.0