簡體   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