我在一个Android应用程序中使用libgdx作为片段,这就是我的项目结构的样子:
我想做的是从模块“ my-gdx-game-core”中调用模块“ app”类,以便我可以在libGDX游戏和android应用之间进行通信。
顺便说一下,我可以my-gdx-game-android
从 app
和my-gdx-game-core
从 打来的电话,my-gdx-game-android.
这样我就可以在android片段内开始游戏了。
即使我将应用程序添加为对my-gdx-game-core的依存关系,也看不到。Gradle同步成功,但由于某种原因我无法访问类。此外,如果我右键单击“ my-gdx-game-core”模块并检查依存关系从android studio我可以在那看到应用程序。
Project:LibGDXInAndroidKotlin的build.gradle:
// 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抛出项目未解决的错误,我不知道有什么区别。
无论如何,即使我添加了依赖性,我也无法访问模块“ app”的类。
我已经尝试过使缓存重新启动无效
编辑::::
里面的GameActivity类的app
开始my-gdx-game-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(){
}
}
AndroidGameFragment inside my-gdx-game-android which starts my-gdx-game-core(actual libgdx game)
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)
}
}
You have a cyclical relationship there...two different modules that are dependent on each other. I don't think that will work.
LibGDX projects are typically set up with a core module that is platform independent, and then android and desktop modules that depend on the the core. This allows you to very rapidly iterate on the desktop without having to compile and install Android builds over and over through most of your development process.
If you truly don't care about the benefits of being able to test on your computer, you don't need a core module at all. You would just put everything into Android. What you're trying to do now effectively defeats the purpose of having a separate core module.
但是,我建议将它们分开,以防万一您改变主意或决定移植到其他平台(如iOS)。
如果您需要从调用android特定代码core
,则无需依赖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);
}});
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句