我正在使用 LibGDX 开发安卓游戏和外部库。 我收到以下错误:
Error:Execution failed for task ':android:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException:
org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Android\Android Studio\jre\bin\java.exe''
finished with non-zero exit value 1
我正在尝试创建一个外部共享项目,我的所有其他游戏都可以依赖该项目来获取通用代码。 问题是我的共享库项目依赖于 Libgdx,而我的游戏项目也依赖于 libgdx。 在 Android 设备上启动我的游戏时。 gradle sync 和构建项目工作正常,它只在尝试部署到设备上时发生。 我也可以毫无问题地部署到桌面。
我认为由于重复的库依赖性,我可能超过了 65,536 的 android 方法计数限制,但无法找到更好的解决方案。 或者可能是 android 不喜欢不在根目录中的嵌套依赖项。
我在下面包含了我的项目结构和 gradle 文件。 非常感谢能够让我继续在游戏项目的 IDE 中编辑库的解决方案。 如果有人推荐其他东西,我也愿意接受替代项目结构。
目录/项目结构:
C:\
Projects\
SharedProject
Project1
Project2
...
我的 android 模块依赖树(通过运行“gradlew -q dependencies android:dependencies --configuration compile”):
+--- project :core
| +--- com.badlogicgames.gdx:gdx:1.9.6
| \--- project :APD_Core
| \--- com.badlogicgames.gdx:gdx:1.9.6
\--- com.badlogicgames.gdx:gdx-backend-android:1.9.6
\--- com.badlogicgames.gdx:gdx:1.9.6
(注:APD_Core为共享项目)
可能的解决方案:
我曾尝试使用 multiDexEnabled,但这没有帮助。 我想我可能需要在将共享库编译成核心时排除 gdx 模块 但我还没有解决这个问题。 (我认为 android 不喜欢共享库对 gdx 具有与 android 模块也具有的相同依赖性) 另一种可能性是android需要将我的共享项目构建到jar中,然后放入android模块中的libs文件夹中。 这可能可以通过 android gradle 文件实现,但我是 gradle 的新手,不知道是否需要或如何做。
我改变了什么:
将共享项目添加到设置gradle
include ':APD_Core'
project(':APD_Core').projectDir = new File(settingsDir, '../APD_Core')
在核心模块中添加对共享项目的依赖
project(":core") {
...
dependencies {
...
compile project(':APD_Core')
}
}
让共享项目依赖于 libgdx
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
}
使用 gdx-setup.jar 时,默认生成的项目结构没有太多变化 请注意,共享项目是作为新的 gradle 项目从 intellij 创建的,而不是使用 gdx-setup.jar 下面是完整的 gradle 文件
渐变设置文件:
include 'desktop', 'android', 'ios', 'core', ':APD_Core'
project(':APD_Core').projectDir = new File(settingsDir, '../APD_Core')
MAIN GRADLE FILE:
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.mobidevelop.robovm:robovm-gradle-plugin:2.2.0'
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = "MyGame"
gdxVersion = '1.9.6'
roboVMVersion = '2.2.0'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
}
}
project(":ios") {
apply plugin: "java"
apply plugin: "robovm"
dependencies {
compile project(":core")
compile "com.mobidevelop.robovm:robovm-rt:$roboVMVersion"
compile "com.mobidevelop.robovm:robovm-cocoatouch:$roboVMVersion"
compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile project(':APD_Core')
}
}
tasks.eclipse.doLast {
delete ".project"
}
核心 GRADLE 文件:
apply plugin: "java"
sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
eclipse.project {
name = appName + "-core"
}
dependencies {
compile project(':APD_Core')
}
ANDROID GRADLE 文件(未修改默认生成的文件):
android {
buildToolsVersion "25.0.1"
compileSdkVersion 23
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.mygame"
minSdkVersion 10
targetSdkVersion 23
multiDexEnabled true
}
}
// 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.mygame/com.mygame.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.6
targetCompatibility = 1.6
}
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")
}
}
}
}
}
}
}
共享项目 GRADLE 文件:
group 'APD_Core'
version '1.0'
apply plugin: 'java'
sourceCompatibility = 1.8
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
gdxVersion = '1.9.6'
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
}
最佳答案
如果您正在为许多游戏制作公共(public)库,我建议您创建一个包含这些库的单独项目,使用 gradle maven-publish plugin 将其发布到本地 Maven 存储库。然后在你的游戏项目中声明对这些库的依赖。这将帮助您现在调试问题,并且可以更轻松地修改这些库并在将来将它们用于其他游戏。
关于java - 对 libGDX 项目的外部 gradle 依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46813373/