在libGDX项目中使用外部gradle依赖

3

我正在使用LibGDX制作一个Android游戏和外部库。我遇到了以下错误:

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。 当在Android设备上启动我的游戏时, Gradle同步和构建项目都可以正常工作,只有在尝试部署到设备时才会出现问题。 我也可以在桌面上部署而没有任何问题。
我认为由于重复的库依赖关系可能超过了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不喜欢共享库与android模块具有相同的gdx依赖关系)
另一个可能性是,android需要我的共享项目构建成jar文件,然后放入android模块中的libs文件夹中。这可能可以通过android gradle文件完成,但我对gradle还不熟悉,不知道是否需要这样做或如何操作。
我做出的更改:
将共享项目添加到settings 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生成的默认项目结构并没有太多变化。 请注意,共享项目是在Intellij中作为新的Gradle项目创建的,而不是使用gdx-setup.jar。 下面是完整的Gradle文件。


GRADLE SETTINGS FILE:

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')
}

安卓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"
}
2个回答

0
如果您正在为多个游戏创建通用库,我建议您创建一个包含这些库的单独项目,使用gradle maven-publish插件将其发布到本地maven仓库,然后在游戏项目中声明对这些库的依赖关系。这将有助于您现在调试问题,并使以后修改这些库并在其他游戏中使用它们更加容易。

{btsdaf} - BlueTomato

0

添加:

compileJava   {
    sourceCompatibility = '1.7'
    targetCompatibility = '1.7'
}

将根gradle文件添加到库项目中,并从库项目中删除settings.gradle文件即可解决问题。
适用于maven本地依赖和外部gradle依赖。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接