java - 在LibGDX And​​roid部署中执行transformClassesWithDexForDebug失败

标签 java android gradle libgdx dex

我正在尝试为我的 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'
}

当前 list :

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

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 中的修改您需要编辑 Manifest 文件以设置 android:name<application>标签如下:

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

关于java - 在LibGDX And​​roid部署中执行transformClassesWithDexForDebug失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539706/

相关文章:

java - getMethod什么时候需要有锁?我尝试了几次在 get 方法中放置和不放置锁都没有看到变化

android - 在 Android 的自定义启动器中创建应用程序快捷方式

android - 404 找不到资源 lint-gradle-26.1.3.pom

android - 检索所有附加了特定帐户的联系人

android - 如果发生 HttpException,Retrofit 和 RxJava 的单元测试永远不会完成

maven - Mavens的Gradle等效项Maven依赖插件

android - 如何从 fresco(facebook 的 android lib)中排除 arm64-v8a 目录

java - 如何在 JEE 中定义处理多部分数据的 JAX-RS 服务?

java - JUnit Rule TemporaryFolder 任意抛出 IOException

java - 注册日期需要以特定格式在请求正文中传递