Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android应用程序中看不到导入的依赖项模块类LibGDX_Java_Android_Android Studio_Gradle_Libgdx - Fatal编程技术网

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

Java 在Android应用程序中看不到导入的依赖项模块类LibGDX,java,android,android-studio,gradle,libgdx,Java,Android,Android Studio,Gradle,Libgdx,我在android应用程序中使用libgdx作为片段,我的项目结构如下所示: 我正在尝试从模块“my gdx game core”调用模块“app”类,这样我就可以在libGDX游戏和android应用程序之间进行通信 顺便说一下,我可以从应用程序调用我的gdx游戏安卓,从我的gdx游戏安卓调用我的gdx游戏核心。这样我就可以在安卓片段中启动游戏了 即使我将应用程序作为依赖项添加到我的gdx游戏核心中,也看不到。Gradle同步成功,但由于某些原因我无法访问类。此外,如果我右键单击“我的gdx

我在android应用程序中使用libgdx作为片段,我的项目结构如下所示:

我正在尝试从模块“my gdx game core”调用模块“app”类,这样我就可以在libGDX游戏和android应用程序之间进行通信

顺便说一下,我可以从
应用程序
调用
我的gdx游戏安卓
,从
我的gdx游戏安卓
调用
我的gdx游戏核心。
这样我就可以在安卓片段中启动游戏了

即使我将应用程序作为依赖项添加到我的gdx游戏核心中,也看不到。Gradle同步成功,但由于某些原因我无法访问类。此外,如果我右键单击“我的gdx游戏核心”模块并从android studio检查依赖项,我可以在那里看到应用程序

项目的build.gradle:libgdxinandoridkotlin:

// 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()
}
我的gdx游戏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()
}
这里是为我的gdx游戏核心构建的重要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抛出项目未解决错误我不知道有什么区别

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

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

编辑:::

app
中的GameActivity类启动了
我的gdx游戏android
的片段

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(){

    }
}
我的gdx游戏android中的AndroidGameFragment启动了我的gdx游戏核心(实际的libgdx游戏)


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

LibGDX项目通常使用独立于平台的核心模块,然后是依赖于核心的android和桌面模块。这允许您在桌面上快速迭代,而无需在大部分开发过程中反复编译和安装Android版本

如果你真的不在乎在电脑上测试的好处,你根本不需要核心模块。你只要把所有东西都放进安卓系统。你现在试图做的事实际上违背了拥有一个独立核心模块的目的

不过,我建议将它们分开,以防您改变主意或决定移植到其他平台,如iOS

如果需要从
core
调用特定于android的代码,则不需要依赖
android
模块。您可以创建一个传递给游戏构造函数的接口。例如,如果要显示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);
        }});
    }
}

嗨,谢谢你的回答。这解释了如何在android和core之间通信。假设我在应用程序中有一个套接字类,我想从核心使用它的对象。假设我用接口方法与core和android通信,我如何在android和app之间通信,以便使用app的Socket类对象?我无法通过getActivity()从android(fragment)访问应用程序,因为它看不到应用程序的活动方法。基本上,我在应用程序中有一个活动,通过supportFragmentManager.beginTransaction()启动android。。。我正在将它们添加到我的初始问题中我只是将fragment android移动到appA fragment,它可以在附加到活动后调用活动的方法,这发生在
fragment.onCreateView()
之后,因此您可以使用受保护的(活动空检查)调用。你可以让你的片段实现游戏调用的接口。例如,在我的toast示例中,您可以使用
override-fun-showToast(message:String)=activity?.let{toast.makeText(it,message,toast.LENGTH_SHORT)}
我应该补充一点,我过于简化了。您不能直接从游戏线程调用Android UI方法,因此实际上它应该是
override-fun-showtoos(message:String)=activity?.let{it.runOnUiThread{Toast.makeText(it,message,Toast.LENGTH\u SHORT)}
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);
        }});
    }
}