[英]Gradle Build failed with unexpected error when trying to build APK
我写了一个小代码,但每当我尝试构建apk时,我都会收到以下错误:
错误:任务':app:transformClassesWithMultidexlistForDebug'的执行失败。 com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:使用带有参数{C:\\ Users \\ xxxx \\ Desktop的主类com.android.multidex.ClassReferenceListBuilder执行java进程时出错\\ 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 }
其中xxxx是我的用户名,yyyyy是项目名称。 我已经尝试了我在Stack Overflow上找到的所有可能的解决方案,但我意识到所有这些解决方案都被问及旧版本。
我有最新版本的Android Studio。 我正在使用最新版本的Android SDK,构建工具,gradle,JDK 8.0和JRE 7.我还尝试了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'
}
在你build.gradle / app
defaultConfig {
....
//Add this line
multiDexEnabled true
}
dependencies {
....
//Add this line
compile 'com.android.support:multidex:1.0.0'
}
这种情况发生在Android平台持续增长的情况下,Android应用程序的大小也在增长。 当您的应用及其引用的库达到一定大小时,您会遇到构建错误,表明您的应用已达到Android应用构建架构的限制
修改模块级build.gradle文件以启用multidex并将multidex库添加为依赖项,如下所示 : Source
android {
defaultConfig {
multiDexEnabled true
}
}
在Module:App gradle文件夹中尝试此操作。
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'
}
您尚未共享任何与您合作的代码和依赖项,因此很难猜出确切的问题。
但是从你发布的错误来看,我认为你的程序超过了64K方法的限制。
Apk文件以DEX的形式包含字节码文件,并且在单个DEX文件中限制为65,536个方法。
因此,如果你的应用程序有超过64k的方法,你应该去多个Dex文件。
启用多个Dex,意味着应用程序构建过程将生成多个DEX文件
defaultConfig {
....
multiDexEnabled true
}
dependencies {
....
compile 'com.android.support:multidex:1.0.0'
}
我发现没有必要安装JDK和JRE(参考:android studio 2.3.2系统要求下的最新下载页面)
在programfiles / android-studio中有一个名为“jre”的文件夹。
所以我想我会卸载JDK和JRE(因为与java有关的问题)
在此之后我重新安装了android studio。
现在它工作正常。 (我没有深入研究,虽然它帮助了我)
当您的方法参考限制超过65,536时会发生这种情况。
您可以将以下内容添加到应用程序的build.gradle文件中
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 25
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
之后,您必须以下列方式扩展Application类
public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
或者你可以这样做:
public class MyApplication extends MultiDexApplication { ... }
最后更新您的清单文件,如下所示
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.