繁体   English   中英

如何在 libgdx 应用程序中创建新模块(android 库)? (如果可能的话)

[英]How to create new module (android library) into libgdx apllication? (if it is possible)

我使用 android 模块(kotlin - libktx)创建了 libgdx 应用程序 - 我使用: https://github.com/tommyettinger/gdx-liftoff

我尝试在 Android Studio 中添加新模块(Android 库),但它不起作用:无法添加名称为“kotlin”的扩展,因为已使用该名称注册了一个扩展。

我试图修改创建模块的 build.gradle,但它没有帮助。 只是错误在改变。 想请问各位知不知道:Android Library Module可以加到libgdx应用吗? 如何?

如果我添加新模块(Java 或 Kotlin 库),它就可以工作。 但我想创建具有 android 依赖项的模块。 如果可能的话。 谢谢你。

更新:

这是我的 gradle 文件和我尝试做的更改的描述。 如果有人可以提供帮助。

我正在尝试使用新的 libgdx 项目来做到这一点。 我复制了文件夹“android”并将其重命名为“androTest”。

我在项目中更改(在根文件夹中):settings.gradle

include 'core', 'lwjgl3', 'android', 'androTest'

在文件 build.gradle 的根文件夹中,我复制了一个子项目配置为 android 的部分,最终文件是:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://s01.oss.sonatype.org' }
        mavenLocal()
        google()
        gradlePluginPortal()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
        maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$androidPluginVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

        // This follows advice from https://blog.gradle.org/log4j-vulnerability
        constraints {
            classpath("org.apache.logging.log4j:log4j-core") {
                version {
                    strictly("[2.18, 3[")
                    prefer("2.18.0")
                }
                because("CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities")
            }
        }
    }
}

allprojects {
    apply plugin: 'eclipse'
    apply plugin: 'idea'
}

configure(subprojects - project(':android')) {
    apply plugin: 'java-library'
    apply plugin: 'kotlin'
    sourceCompatibility = 1.8
    compileJava {
        options.incremental = true
    }
    dependencies {
        // This follows advice from https://blog.gradle.org/log4j-vulnerability
        constraints {
            implementation("org.apache.logging.log4j:log4j-core") {
                version {
                    strictly("[2.18, 3[")
                    prefer("2.18.0")
                }
                because("CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities")
            }
        }
    }
}

configure(subprojects - project(':androTest')) {
    apply plugin: 'java-library'
    apply plugin: 'kotlin'
    sourceCompatibility = 1.8
    compileJava {
        options.incremental = true
    }
    dependencies {
        // This follows advice from https://blog.gradle.org/log4j-vulnerability
        constraints {
            implementation("org.apache.logging.log4j:log4j-core") {
                version {
                    strictly("[2.18, 3[")
                    prefer("2.18.0")
                }
                because("CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities")
            }
        }
    }
}

subprojects {
    version = '1.0.0'
    ext.appName = 'TestLibGDXProject'
    repositories {
        mavenCentral()
        maven { url 'https://s01.oss.sonatype.org' }
        mavenLocal()
        gradlePluginPortal()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
        maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' }
        maven { url 'https://jitpack.io' }
    }
}

eclipse.project.name = 'TestLibGDXProject' + '-parent'

新文件夹“androTest”中的文件 build.gradle 与原始“android”文件夹中的文件 build.gradle 相同,除了最后一行。 在“androTest”中,我只更改了最后一行。 “androTest”文件夹中有文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 30
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/main/java', 'src/main/kotlin']
            aidl.srcDirs = ['src/main/java', 'src/main/kotlin']
            renderscript.srcDirs = ['src/main/java', 'src/main/kotlin']
            res.srcDirs = ['res']
            assets.srcDirs = ['../assets']
            jniLibs.srcDirs = ['libs']
        }
    }
    packagingOptions {
        // Preventing from license violations (more or less):
        pickFirst 'META-INF/LICENSE.txt'
        pickFirst 'META-INF/LICENSE'
        pickFirst 'META-INF/license.txt'
        pickFirst 'META-INF/LGPL2.1'
        pickFirst 'META-INF/NOTICE.txt'
        pickFirst 'META-INF/NOTICE'
        pickFirst 'META-INF/notice.txt'
        // Excluding unnecessary meta-data:
        exclude 'META-INF/robovm/ios/robovm.xml'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/dependencies.txt'
    }
    defaultConfig {
        applicationId 'com.testgdx.testlibgdxproject'
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    compileOptions {
        sourceCompatibility "1.8"
        targetCompatibility "1.8"
        coreLibraryDesugaringEnabled true
    }
    kotlinOptions.jvmTarget = "1.8"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

repositories {
    // needed for AAPT2, may be needed for other tools
    google()
}

configurations { natives }

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
    implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
    implementation project(':core')

    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"

    // This follows advice from https://blog.gradle.org/log4j-vulnerability
    constraints {
        implementation("org.apache.logging.log4j:log4j-core") {
            version {
                strictly("[2.18, 3[")
                prefer("2.18.0")
            }
            because("CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities")
        }
    }
}

// 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() {
    doFirst {
        file("libs/armeabi-v7a/").mkdirs()
        file("libs/arm64-v8a/").mkdirs()
        file("libs/x86_64/").mkdirs()
        file("libs/x86/").mkdirs()

        configurations.getByName("natives").copy().files.each { jar ->
            def outputDir = null
            if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
            if(jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
            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"
                }
            }
        }
    }
}
tasks.matching { it.name.contains("merge") && it.name.contains("JniLibFolders") }.configureEach { packageTask ->
    packageTask.dependsOn 'copyAndroidNatives'
}

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_SDK_ROOT"
        }
    } else {
        path = "$System.env.ANDROID_SDK_ROOT"
    }

    def adb = path + "/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.testgdx.testlibgdxproject/com.testgdx.testlibgdxproject.android.AndroidLauncher'
}

eclipse.project.name = appName + "-androTest"

如果我尝试构建它,我会看到错误:

A problem occurred evaluating project ':android'.
> Failed to apply plugin 'kotlin-android'.
   > Cannot add extension with name 'kotlin', as there is an extension already registered with that name.

如果我从 build.gradle 文件中删除问题行(应用插件:'kotlin-android'),我会看到新错误:

* What went wrong:
A problem occurred evaluating project ':android'.
> Could not get unknown property 'kotlinOptions' for extension 'android' of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.

我从 build.gradle 文件 (kotlinOptions.jvmTarget = "1.8") 中删除了该行,我可以看到错误:

A problem occurred configuring project ':android'.
> com.android.build.gradle.internal.BadPluginException: The 'java' plugin has been applied, but it is not compatible with the Android plugins.

所有错误均来自原始“android”模块。

Liftoff 的部分作者在这里。 看起来问题与根 build.gradle 文件中的configure(subprojects - project(':android')) {有关,该文件包括除android项目之外的所有项目,并且仍然包括新添加的androTest项目。 您可能需要configure(subprojects - project(':android') - project(':androTest')) {相反,这使得 androTest 使用与 android 项目使用的相同配置(或不使用)。 您还需要删除整个configure(subprojects - project(':androTest')) {块,该块完全损坏,因为它将尝试使用相同的配置配置android ,最后一个块从中排除android

我已经有一段时间没有发布新版本了,但应该很快就会发布。 我注意到这里的依赖约束有多么丑陋,在当前的 Gradle 版本中不需要它们,因此下一个 GDX-Liftoff 版本将删除这些约束(如果你愿意,你现在可以自己删除它们)。

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM