导入的依赖模块 类 未在 Android 应用程序中看到 LibGDX
Imported Dependency Module Classes are not seen LibGDX inside Android app
我在 android 应用程序中使用 libgdx 作为片段,这就是我的项目结构:
我想做的是调用模块“app”classes 来自模块“my-gdx-game-core” 这样我就可以在两者之间进行通信libGDX 游戏和 android 应用。
顺便说一下,我可以从 app
my-gdx-game-android
app
和 my-gdx-game-core
my-gdx-game-android.
这样我就可以在 android 片段中开始游戏了。
尽管我将应用程序添加为对 my-gdx-game-core 的依赖,但它没有 seen.Gradle 同步成功,但我只是无法访问 classes 对于一些 reason.Also 如果我正确单击“my-gdx-game-core”模块并从 android studio 检查依赖项我可以在那里看到应用程序。
build.gradle 对于 Project: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()
}
build.gradle 对于我的 gdx-game-android
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')
它会同步 succesfully.By 如果我只添加
implementation project(':app')
gradle 抛出项目未解决错误我不知道有什么区别。
无论如何,即使我添加了依赖项,我也无法访问模块 'app'.
的 classes
我已经尝试过使缓存无效重新启动
编辑::::
GameActivity class inside 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(){
}
}
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
模块。您可以创建一个传递给游戏构造函数的接口。例如,如果你想显示一个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 应用程序中使用 libgdx 作为片段,这就是我的项目结构:
我想做的是调用模块“app”classes 来自模块“my-gdx-game-core” 这样我就可以在两者之间进行通信libGDX 游戏和 android 应用。
顺便说一下,我可以从 app
my-gdx-game-android
app
和 my-gdx-game-core
my-gdx-game-android.
这样我就可以在 android 片段中开始游戏了。
尽管我将应用程序添加为对 my-gdx-game-core 的依赖,但它没有 seen.Gradle 同步成功,但我只是无法访问 classes 对于一些 reason.Also 如果我正确单击“my-gdx-game-core”模块并从 android studio 检查依赖项我可以在那里看到应用程序。
build.gradle 对于 Project: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()
}
build.gradle 对于我的 gdx-game-android
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')
它会同步 succesfully.By 如果我只添加
implementation project(':app')
gradle 抛出项目未解决错误我不知道有什么区别。
无论如何,即使我添加了依赖项,我也无法访问模块 'app'.
的 classes我已经尝试过使缓存无效重新启动
编辑::::
GameActivity class inside 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(){
}
}
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
模块。您可以创建一个传递给游戏构造函数的接口。例如,如果你想显示一个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);
}});
}
}