在LibGDX And​​roid部署

时间:2017-08-07 05:02:33

标签: 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'
}

Current Manifest:

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

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

1 个答案:

答案 0 :(得分:1)

根据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中的修改外,您需要修改清单文件,以便在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>