简体   繁体   中英

Gradle Build failed with unexpected error when trying to build APK

I have written a small code, but whenever I try to build the apk, I get the following error:

Error:Execution failed for task ':app:transformClassesWithMultidexlistForDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: Error while executing java process with main class com.android.multidex.ClassReferenceListBuilder with arguments {C:\\Users\\xxxx\\Desktop\\yyyyy\\app\\build\\intermediates\\multi-dex\\debug\\componentClasses.jar C:\\Users\\xxxx\\Desktop\\yyyyy\\app\\build\\intermediates\\transforms\\jarMerging\\debug\\jars\\1\\1f\\combined.jar}

Where xxxx is my username, and yyyyy is project name. I have tried all possible solutions that I found on Stack Overflow, but I realized that all of them were asked about older versions.

I have the latest version of Android Studio. I am using latest version of Android SDK, the build tools, gradle, JDK 8.0 , and JRE 7. I also tried JRE 8.

build.gradle(module app) :
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
    applicationId "xxxx.yyyyy"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "Unreleased Demo Version"
    testInstrumentationRunner                 
"android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }

}
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'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:multidex:1.0.1'
}

In you build.gradle/app

defaultConfig {
    ....
   //Add this line
    multiDexEnabled true
}

dependencies {
   ....
   //Add this line
   compile 'com.android.support:multidex:1.0.0'
}

This happens as the Android platform has continued to grow, so has the size of Android apps. When your app and the libraries it references reach a certain size, you encounter build errors that indicate your app has reached a limit of the Android app build architecture

Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here : Source

android {
    defaultConfig {


        multiDexEnabled true
    }
}

Try this in Module:App gradle folder.

 multiDexEnabled true

        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }

You have not shared any of your codes and dependencies you working with ,so it is difficult to guess exact problem.

but from error you posted ,i think your program cross the limit of 64K Methods.

Apk files conatins bytecode files in the form of DEX, and it has a limit of 65,536 methods in a single DEX file.

So if your App has more then 64k methods ,you should go for multiple Dex files.

Enabling mutiple Dex ,means app build process will generate more than one DEX file

defaultConfig {
....

multiDexEnabled true 
}

dependencies {
....

compile 'com.android.support:multidex:1.0.0'
}

I found there is no need to install JDK and JRE (ref:android studio 2.3.2 latest download page under system requirements)

And there is a folder named "jre" in programfiles/android-studio.

So I thought I shall uninstall JDK and JRE (as the problem related to java)

After this I reinstalled android studio.

And now it works fine. (I haven't done research in deep although it helped me)

This happen when your have cross 65,536 method reference limit.

Step 1

You can add the following to your app's build.gradle file

    android {
      defaultConfig {
      ...
      minSdkVersion 15 
      targetSdkVersion 25
      multiDexEnabled true
     }
  ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

Step 2A

After that you have to extend the Application class in the following way

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

Or you can do as below:

Step 2B

public class MyApplication extends MultiDexApplication { ... }

STEP 3

Finally update your manifest file like below

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.myapp">
   <application
        android:name="android.support.multidex.MultiDexApplication" >
    ...
   </application>
</manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM