【问题标题】:Put a LibGDX project into a Project?将 LibGDX 项目放入项目中?
【发布时间】:2020-04-07 14:46:21
【问题描述】:

我知道这个问题已经被问过很多次了,但我仍然找不到合适的步骤来解决这个问题。我正在使用安卓工作室。我如何实际将 LibGDX 项目放入 Android App 项目(而不是将 Android App 项目放入 LibGDX 项目)?这就是它应该如何工作的方式。当我在 Android 应用程序中按下一个按钮时,我可以通过设置游戏画面来启动我的游戏。玩完游戏后,我可以再次回到Android App。我需要配置 Gradle 文件吗?如果是这样,我该如何改变它?

这是我将libGDX模块导入Android App项目时遇到的问题。

Could not find method android() for arguments [build_f3607d3g9oo7ee40quzhms085$_run_closure1@18e45678] on project ':android' of type org.gradle.api.Project.

还有什么额外的配置需要做的吗?

这是我的 Android Build Gradle 文件。

android {
    buildToolsVersion "29.0.2"
    compileSdkVersion 29
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }

    }
    packagingOptions {
        exclude 'META-INF/robovm/ios/robovm.xml'
    }
    defaultConfig {
        applicationId "com.motivado.game"
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


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

tasks.whenTaskAdded { packageTask ->
    if (packageTask.name.contains("package")) {
        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_HOME"
        }
    } else {
        path = "$System.env.ANDROID_HOME"
    }

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

我的主要 build.gradle

buildscript {


    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'


    }
}

allprojects {

    version = '1.0'
    ext {
        appName = "Motivado Game"
        gdxVersion = '1.9.10'
        roboVMVersion = '2.3.8'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        google()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":desktop") {
    apply plugin: "java-library"


    dependencies {
        implementation project(":core")
        api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"

    }
}

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        implementation project(":core")
        api "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"
        api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"

    }
}

project(":core") {
    apply plugin: "java-library"


    dependencies {
        api "com.badlogicgames.gdx:gdx:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

    }
}

【问题讨论】:

  • 可以分享一下android/build.gradle吗?
  • @cricket_007 我已经包含了 build.gradle。这是你想看的文件吗?
  • 谢谢。上面那个文件夹里的那个呢?
  • 你的意思是 Android 文件之外的 build.gradle 吗?
  • 是的,请。那个

标签: android android-studio gradle libgdx


【解决方案1】:

在您的主应用中build.gradle

复制apply plugin: 'com.android.application'这一行

进入导入的gdx项目build.grade

并在android块之前粘贴apply plugin: 'com.android.application'

在您的 gdx 项目 build.gradle 中向下滚动并注释掉 eclipseidea 块。..

进入 core build.gradle 并注释掉 eclipse 块。..

同步和项目应该会成功构建

【讨论】:

  • 它没有用,因为我没有任何应该注释掉的日食和想法块。
  • @HolmesQueen,好的,有了一个新的项目构建,我认为你应该在你的 gradle 中看到这些。
猜你喜欢
  • 2019-11-24
  • 1970-01-01
  • 2015-08-09
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
  • 1970-01-01
  • 2020-05-06
相关资源
最近更新 更多