外部gradle对libGDX项目的依赖

时间:2017-10-18 15:03:37

标签: java android intellij-idea gradle libgdx

我正在使用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以及我的游戏项目,它依赖于libgdx。 在Android设备上启动我的游戏时。 gradle sync和build项目工作正常,只有在尝试部署到设备上时才会发生。 我也可以毫无问题地部署到桌面。

我认为由于重复的库依赖性,我可能超过了Android方法计数限制65,536,但是无法找到更好的解决方案。 或者它可能是因为android不喜欢根目录中没有的嵌套依赖项。


我在下面提供了我的项目结构和gradle文件。 非常感谢一个允许我继续在游戏项目的IDE中编辑库的解决方案。 如果有人推荐别的东西,我也愿意接受另一个项目结构。



目录/项目结构:

C:\
    Projects\
        SharedProject
        Project1
        Project2
        ...

我的android模块依赖树(通过运行" gradlew -q依赖项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

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"
}

CORE GRADLE FILE:

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 FILE(未修改默认生成的文件):

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

共享项目评分文件:

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 :(得分:0)

如果您要为许多游戏制作通用库,我建议您创建一个包含这些库的单独项目,使用gradle maven-publish plugin将其发布到您当地的maven仓库,然后声明这些库的依赖关系在你的游戏项目中。这将帮助您现在调试您的问题,并且将来更容易修改这些库并在将来的其他游戏中使用它们。

答案 1 :(得分:0)

添加:

{}

到库项目的根gradle文件,从库项目中删除settings.gradle文件解决了这个问题。

适用于maven本地依赖项和外部gradle依赖项。