【问题标题】:Error while building APK in Android Studio在 Android Studio 中构建 APK 时出错
【发布时间】:2017-09-27 02:48:41
【问题描述】:

我的应用在模拟器上运行良好。构建 APK 时显示以下错误:

错误:任务 ':app:transformClassesWithDexForDebug' 执行失败。 com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: 多个dex文件定义Lcom/google/android/ gms/common/api/zza;

我找不到错误所在。我的 gradle 文件是:

项目 Gradle:

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

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
    //this
    classpath 'com.google.gms:google-services:3.0.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
}
}

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

我的Module Gradle是:

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.example.sugansk.quill"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
//this new
apply plugin: 'com.google.gms.google-services'
//nothis
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.google.firebase:firebase-messaging:10.0.1'
testCompile 'junit:junit:4.12'
}

感谢您的帮助。

【问题讨论】:

    标签: android android-studio gradle apk build.gradle


    【解决方案1】:

    您应用了错误的 Google 服务插件。您必须在最后设置,因为它是一个插件,而不是一个依赖项。

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.example.sugansk.quill"
            minSdkVersion 15
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
    //this new
    
    //nothis
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile 'com.android.support:design:25.1.0'
        compile 'com.android.support:support-v4:25.1.0'
        compile 'com.google.firebase:firebase-messaging:10.0.1'
        testCompile 'junit:junit:4.12'
    }
    
    apply plugin: 'com.google.gms.google-services'
    

    【讨论】:

      【解决方案2】:

      我认为这个错误是插件冲突引起的,所以首先

      • 清理你的项目
      • 将您的 build.gradle 更新到最新版本

      【讨论】:

        【解决方案3】:

        它似乎对我有用
        类路径 'com.google.gms:google-services:3.0.0'

        应用插件:'com.google.gms.google-services'

        compile 'com.android.support:appcompat-v7:25.1.0'
        compile 'com.android.support:design:25.1.0'
        compile 'com.android.support:support-v4:25.1.0'
        compile 'com.google.firebase:firebase-messaging:10.0.1'
        

        【讨论】:

        • 这是因为我认为缺少 google-services.json
        【解决方案4】:

        在您的应用程序级别的 gradle 文件中添加以下 android 标签并尝试同步,清理构建 apk。

        compileOptions {
           sourceCompatibility JavaVersion.VERSION_1_8
           targetCompatibility JavaVersion.VERSION_1_8
           }
        

        【讨论】:

          猜你喜欢
          • 2022-10-25
          • 1970-01-01
          • 2020-04-24
          • 2016-12-08
          • 1970-01-01
          • 1970-01-01
          • 2018-09-28
          • 1970-01-01
          • 2020-03-10
          相关资源
          最近更新 更多