【问题标题】:External gradle dependency on libGDX projectlibGDX 项目的外部 gradle 依赖
【发布时间】:2017-10-18 15:03:36
【问题描述】:

我正在使用 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。 在安卓设备上启动我的游戏时。 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 不喜欢共享库对 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 时默认生成的项目结构没有太大变化 请注意,共享项目是从 intellij 作为新的 gradle 项目创建的,而不是使用 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"
}

核心梯度文件:

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

【问题讨论】:

    标签: java android intellij-idea gradle libgdx


    【解决方案1】:

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

    【讨论】:

    • 在一组新的测试项目中尝试了这一点,出于某种原因,我遇到了同样的问题,尽管在此测试中不依赖于共享库上的 libGDX。适用于桌面,但将本地 maven 依赖项添加到核心会导致 android 部署失败并出现完全相同的错误。更令人困惑的是,导致原始项目失败的一件事甚至没有在这个测试中引入,但我遇到了同样的问题。 (顺便感谢您的帮助,不知道本地 maven repos)
    【解决方案2】:

    添加:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 2016-05-06
      • 1970-01-01
      相关资源
      最近更新 更多