在 LibGDX Android 部署中执行 transformClassesWithDexForDebug 失败



我正在尝试为我的 LibGDX 项目运行 Android 构建,当构建尝试执行 android:transformClassesWithDexForDebug 时出现错误。我已经寻找这个问题的解决方案一段时间了,但似乎没有一个建议奏效。我正在运行一台具有 16GB RAM 的 Linux 机器,因此 RAM 问题的介词非常不可能。我已经为我的应用程序启用了Dex,将Dex添加为依赖项,降低了目标SDK版本,清理并重建了项目,重新启动了Android Studio,重新启动了我的计算机,尝试了Android Studio的最新稳定版本和beta Canary 9版本,但都无济于事。我收到的执行失败的消息是:

Error:Execution failed for task ':android:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/programs/android-studio-preview/jre/bin/java'' finished with non-zero exit value 1

这是我当前用于 android 模块的 build.gradle 文件:

android {
buildToolsVersion '21.1.2'
compileSdkVersion 21
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
instrumentTest.setRoot('tests')
}
packagingOptions {
exclude 'META-INF/robovm/ios/robovm.xml'
}
defaultConfig {
applicationId "com.bradvok.gravityballz"
minSdkVersion 21
targetSdkVersion 21
multiDexEnabled true
}
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives() {
file("libs/armeabi/").mkdirs();
file("libs/armeabi-v7a/").mkdirs();
file("libs/arm64-v8a/").mkdirs();
file("libs/x86_64/").mkdirs();
file("libs/x86/").mkdirs();
configurations.natives.files.each { jar ->
def outputDir = null
if (jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
if (jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
if (outputDir != null) {
copy {
from zipTree(jar)
into outputDir
include "*.so"
}
}
}
}
task run(type: Exec) {
def path
def localProperties = project.file("../local.properties")
if (localProperties.exists()) {
Properties properties = new Properties()
localProperties.withInputStream { instr ->
properties.load(instr)
}
def sdkDir = properties.getProperty('sdk.dir')
if (sdkDir) {
path = sdkDir
} else {
path = "$System.env.ANDROID_HOME"
}
} else {
path = "$System.env.ANDROID_HOME"
}
def adb = path + "/platform-tools/adb"
commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.bradvok.gravityballz/com.bradvok.gravityballz.AndroidLauncher'
}
// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
// need to specify Java source sets explicitly, SpringSource Gradle Eclipse plugin
// ignores any nodes added in classpath.file.withXml
sourceSets {
main {
java.srcDirs "src", 'gen'
}
}
jdt {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
classpath {
plusConfigurations += [project.configurations.compile]
containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'
}
project {
name = appName + "-android"
natures 'com.android.ide.eclipse.adt.AndroidNature'
buildCommands.clear();
buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
buildCommand "org.eclipse.jdt.core.javabuilder"
buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
}
}
// sets up the Android Idea project, using the old Ant based build.
idea {
module {
sourceDirs += file("src");
scopes = [COMPILE: [plus: [project.configurations.compile]]]
iml {
withXml {
def node = it.asNode()
def builder = NodeBuilder.newInstance();
builder.current = node;
builder.component(name: "FacetManager") {
facet(type: "android", name: "Android") {
configuration {
option(name: "UPDATE_PROPERTY_FILES", value: "true")
}
}
}
}
}
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}

当前清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxxxxx.xxxxxxxxxxxx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="25" android:targetSdkVersion="25" />
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >
<activity
android:name="com.xxxxxxx.xxxxxxxxxxxx.AndroidLauncher"
android:label="@string/app_name" 
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

我已经尝试了几乎所有编译 SDK 版本、目标 SDK 版本和最低 SDK 版本从 21-25 的组合。我正在测试的手机是运行API级别21的HTC One,没有可用的更新。话虽如此,问题可能出在我的手机上吗?还值得一提的是,不久前,我能够在该应用程序的早期版本上成功运行构建,当我恢复到该版本时,发生了相同的问题。同样,我已经参考了与此问题相关的所有其他StackOverflow线程,但到目前为止,没有一个解决方案能够解决我遇到的问题。还有什么我可以尝试的吗?

根据安卓文档:

android {
compileSdkVersion 26
buildToolsVersion "21.1.2"
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.1'    <-- Not required when minimum sdk is >=21
}

除了修改build.gradle外,您还需要编辑清单文件以在<application>标记中设置android:name,如下所示:

<?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>