java - 在 Android 应用程序中看不到导入的依赖模块类 LibGDX

标签 java android android-studio gradle libgdx

我在 android 应用程序中使用 libgdx 作为 fragment ,这就是我的项目结构: enter image description here

我想做的是调用模块“my-gdx-game-core”中的模块“app”类这样我就可以在 libGDX 游戏和 android 应用程序之间进行通信。

顺便说一句,我可以调用 my-gdx-game-android from appmy-gdx-game-core 来自 my-gdx-game-android. 这样我就可以在 android fragment 中开始游戏了。

尽管我将应用程序添加为对 my-gdx-game-core 的依赖,但它没有看到。Gradle 同步成功但由于某种原因我无法访问类。此外,如果我右键单击“my-gdx-game-core”模块并从 android studio 检查依赖项,我可以在那里看到应用程序。

enter image description here

项目的 build.gradle:LibGDXInAndroidKotlin:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.20'

    repositories {
        google()
        jcenter()
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

project.ext {
    minSdkVersion = 16
    targetSdkVersion = 28
    compileSdkVersion = 28
    gdxVersion = '1.9.10'
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

模块应用的build.gradle

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


android {
    compileSdkVersion project.compileSdkVersion
    defaultConfig {
        applicationId "com.ersen.androidwithlibgdx"
        minSdkVersion project.minSdkVersion
        targetSdkVersion project.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation project(":my-gdx-game-android")
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:appcompat-v7:28.0.0"
    implementation "com.android.support:design:28.0.0"
    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support.constraint:constraint-layout:1.1.3"
    implementation 'junit:junit:4.12'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.android:flexbox:1.0.0'
    implementation 'com.gauravk.bubblenavigation:bubblenavigation:1.0.7'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'de.hdodenhof:circleimageview:3.0.1'
    implementation 'com.akexorcist:RoundCornerProgressBar:2.0.3'
    implementation 'com.github.anastr:speedviewlib:1.4.0'
    implementation 'com.android.support:cardview-v7:27.0.2'
    implementation 'com.android.support:recyclerview-v7:28.0.0'

    implementation 'android.arch.lifecycle:extensions:1.1.1'
}
repositories {
    mavenCentral()
}

my-gdx-game-android 的 build.gradle

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

android {
    compileSdkVersion project.compileSdkVersion

    defaultConfig {
        minSdkVersion project.minSdkVersion
        targetSdkVersion project.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

configurations { natives }

dependencies {
    implementation project(":my-gdx-game-core")

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:appcompat-v7:28.0.0"
    api "com.badlogicgames.gdx:gdx-backend-android:${project.gdxVersion}"
    natives "com.badlogicgames.gdx:gdx-platform:${project.gdxVersion}:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-platform:${project.gdxVersion}:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-platform:${project.gdxVersion}:natives-arm64-v8a"
    natives "com.badlogicgames.gdx:gdx-platform:${project.gdxVersion}:natives-x86"
    natives "com.badlogicgames.gdx:gdx-platform:${project.gdxVersion}:natives-x86_64"
    compile "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"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

// 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', '%PACKAGE%/%PACKAGE%.AndroidLauncher'
}
repositories {
    mavenCentral()
}

这里是 my-gdx-game-core 的重要 build.gradle

apply plugin: 'kotlin'

dependencies {
    implementation project(path: ':app', configuration: 'default')

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "com.badlogicgames.gdx:gdx:${project.gdxVersion}"
    implementation "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"


}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
buildscript {
    ext.kotlin_version = '1.3.20'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

如你所见,我添加了

implementation project(path: ':app', configuration: 'default')

它同步成功。顺便说一句,如果我只添加

implementation project(':app')

gradle throws project not resolved error 我不知道有什么区别。

无论如何,即使我添加了依赖项,我也无法访问模块“app”的类。

我已经尝试过使缓存无效重新启动

编辑::::

app 中的 GameActivity 类启动 my-gdx-game-android 的 fragment

class GameActivity : AppCompatActivity(), AndroidFragmentApplication.Callbacks {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val libgdxGameFragment:AndroidGameFragment = AndroidGameFragment()




        //never mind if this supportFragmentManager... shows type mismatch error.Its working. this line puts libgdx into fragment.fragment is similar to component in react.
        supportFragmentManager.beginTransaction().replace(R.id.fragment_container, libgdxGameFragment, AndroidGameFragment::class.java.simpleName).commit()


    }




    override fun exit() {

    }
      fun myFun(){

    }
}

my-gdx-game-android 中的 AndroidGameFragment 启动 my-gdx-game-core(实际的 libgdx 游戏)

class AndroidGameFragment () : AndroidFragmentApplication(){



    


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val config = AndroidApplicationConfiguration()




        return initializeForView(MyGdxGame(), config)
    }

 




}

最佳答案

你在那里有一个循环关系......两个不同的模块相互依赖。我认为这行不通。

LibGDX 项目通常设置有一个独立于平台的核心模块,然后是依赖于该核心的 android 和桌面模块。这使您可以在桌面上快速迭代,而无需在大部分开发过程中一遍又一遍地编译和安装 Android 版本。

如果您真的不关心能够在您的计算机上进行测试的好处,那么您根本不需要核心模块。您只需将所有内容都放入 Android 中即可。您现在尝试做的事情实际上违背了拥有单独核心模块的目的。

但是,我建议将它们分开,以防您改变主意或决定移植到 iOS 等其他平台。

如果你需要从core调用android特定代码,你不需要依赖android模块。您可以创建一个传递给游戏构造函数的接口(interface)。例如,如果你想显示一个 Android Toast,你可以在 core 中做一个这样的界面:

public interface PlatformAdapter {
    void showToast(String message);
}

在您的游戏中,从您的构造函数中捕获对它的引用,并在您希望 Android 执行某些操作时调用适配器:

private PlatformAdapter adapter;

public MyGame (PlatformAdapter adapter){
    this.adapter = adapter;
}

void showToast(String message){
    if (adapter != null) adapter.showToast(message);
}

然后在您的 android 模块中,您可以将适配器传递到您的游戏中。例如:

public class AndroidLauncher extends AndroidApplication implements PlatformAdapter {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        config.r = 8;
        config.g = 8;
        config.b = 8;
        config.a = 8;
        config.useWakelock = true;
        initialize(new MyGame(this), config);
    }

    @Override
    public void showToast(String message){
        runOnUiThread( new Runnable(){ public void run() {
            Toast.makeText(this, message, Toast.LENGTH_SHORT);
        }});
    }
}

关于java - 在 Android 应用程序中看不到导入的依赖模块类 LibGDX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58052251/

相关文章:

java - 如何精确除9位数以上的小数?

java - 如何为 B4A 创建插件

android - 是否可以将 Android NDK 用于 Android Wear 应用程序?

android - 从 Android Studio 中的外部项目链接模块

android - ActionBarDrawerToggle 没有合适的构造函数可绘制

java - 无法使用 Braintree Payment Nonce 创建订阅

java - Azure Blob存储: CloudBlockBlob. downloadToByteArray(byte[] buffer, int bufferOffset)如何设置缓冲区大小

android - Android 上的蓝牙打印

android - 使用 Facebook SDK 登录失败的无效 key 错误

java - LibGDX - 无法从 Android Studio 运行 destop 版本