【问题标题】:Multiple dex file error while adding Firebase analytics in Android Studio在 Android Studio 中添加 Firebase 分析时出现多个 dex 文件错误
【发布时间】:2016-06-27 14:30:26
【问题描述】:

在我的 Android Studio 项目中添加 Firebase 分析时出现以下错误: “错误:将字节码转换为 dex 时出错: 原因:com.android.dex.DexException: 多个dex文件定义Lcom/google/android/gms/measurement/AppMeasurementContentProvider;"

如果有人告诉我额外的 AppmeasurementContentProvider 来自哪里以及相应的“排除”命令,我真的很感激。

这是我的 build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 21
buildToolsVersion "22.0.1"

lintOptions {
    abortOnError false
}
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
    exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
    exclude 'com.google.android.gms.measurement'

}
defaultConfig {
    applicationId "com.elisiumlabs.sarthi"
    minSdkVersion 10
    targetSdkVersion 19
    testInstrumentationRunner    "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {

    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),   'proguard-rules.txt'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

}

dependencies {

compile project(':androidQuery')
compile project(':pinnedSectionListActivity')
compile project(':volley')
compile project(':facebookSDK')
compile 'com.google.code.gson:gson:2.2.4'
compile 'joda-time:joda-time:2.3'
compile 'com.android.support:gridlayout-v7:21.0.0'
compile "com.google.android.gms:play-services-location:8.3.0"
// compile "com.google.android.gms:play-services-gcm:8.3.0"
compile 'com.android.support:appcompat-v7:21.0.3'


compile('com.google.android.gms:play-services-gcm:8.3.0') {
    exclude group: 'com.google.android.gms.measurement'
}
compile('com.google.firebase:firebase-core:9.0.2')
        {
           exclude group: 'com.google.android.gms.measurement'
        }
/*compile files('libs/appcompat_v7.jar')*/
compile files('libs/commons-io-1.4.jar')
compile files('libs/commons-lang-2.4.jar')
compile files('libs/httpclient-4.3.5.jar')
/*compile files('libs/objectify-5.0.3.jar')*/
compile 'commons-codec:commons-codec:1.9'
compile files('libs/splunk-mint-4.1.jar')
testCompile 'junit:junit:4.12'
// androidTestCompile 'com.android.support:support-annotations:21.0.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
/*  androidTestCompile('com.android.support.test.espresso:espresso-    core:2.2.1') {
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}*/
androidTestCompile('com.android.support.test.espresso:espresso-   contrib:2.2.2') {
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'javax.inject'
    exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test.espresso:espresso-web:2.2.2'){
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}
// Optional -- UI testing with UI Automator
  androidTestCompile('com.android.support.test.uiautomator:uiautomator-v18:2.1.1'){
      // Necessary if your app targets Marshmallow (since the test runner
      // hasn't moved to Marshmallow yet)
      exclude group: 'com.android.support', module: 'support-annotations'
  }


}
configurations.all {
resolutionStrategy {
    force 'com.android.support:support-annotations:23.0.1'
}
}

【问题讨论】:

    标签: android android-studio firebase-analytics


    【解决方案1】:

    使用 Gradle 为 Multidex 配置您的应用 参考https://developer.android.com/studio/build/multidex.html#mdex-gradle

    修改模块级别的build.gradle文件配置,包含支持库并启用multidex输出,如下代码sn -p所示:

    android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"
    
    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...
    
        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
    }
    
    dependencies {
    compile 'com.android.support:multidex:1.0.0'
    }
    

    扩展了Application类,你可以重写attachBaseContext()方法并调用MultiDex.install(this)来启用multidex。有关详细信息,请参阅 MultiDexApplication 参考文档。

    【讨论】:

      【解决方案2】:

      试试我的答案....

      这是链接

      Android Studio Gradle Error:Execution failed for task ':app:dexDebug' using Ion koush lib

      请以 Google 服务作为我的答案,并在您的 gradle 中添加一些代码,如图所示,在您的主要文件中添加一些代码

      而且,multidexenable 也是如此,如答案中所示的 multidex

      首先编译构建

      编译'com.android.support:multidex:1.0.1'

      编译'com.google.android.gms:play-services:+'

      在你的 AndroidManifest.xml 添加这行 android:name

      <application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"
          android:name="android.support.multidex.MultiDexApplication"
          >
      

      并且在你的 build.gradle 中也添加

      dexOptions {
          //incremental = true;
          preDexLibraries = false
          javaMaxHeapSize "4g"
      }
      

      【讨论】:

        【解决方案3】:

        我真的不认为您可以混合使用 GCM 和 FCM,因为 FCM 是新的 GCM。从逻辑上讲,它们必须共享一个几乎相似的 api 才能互通。

        您应该选择 FCM 或 GCM。从您的 build.gradles 看起来您使用 gcm。所以删除 FCM 代码。删除这些行:

        compile('com.google.firebase:firebase-core:9.0.2')
        {
           exclude group: 'com.google.android.gms.measurement'
        }
        

        Setting Up Google Play Services 上使用 GCM 阅读播放服务设置 和 FCM 在Add Firebase to your Android Project

        【讨论】:

          【解决方案4】:

          当我尝试版本级别不匹配时,Android Studio 会发出以下警告:

          请通过更新 google-services 插件的版本来修复版本冲突(有关最新版本的信息,请访问 https://bintray.com/android/android-tools/com.google.gms.google-services/

          因此设置为相同的版本级别可以解决相互不兼容的依赖关系

              compile('com.google.android.gms:play-services-gcm:11.8.0')
              compile('com.google.firebase:firebase-core:11.8.0')
          

          【讨论】:

            猜你喜欢
            • 2017-05-10
            • 2014-11-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-04-09
            • 2015-09-13
            相关资源
            最近更新 更多