我正在努力移植我的libgdx游戏,以支持通过Gradle 2进行依赖注入。
我遇到的问题是,匕首似乎没有为我的组件生成代码。我相信问题在于libgdx处理gradle文件的方式以及如何将它们拆分为不同的操作系统。
我的整个项目(非常不完整)可以在here中找到,但是我将发布仅针对android的项目代码。
我对整个项目的build.gradle:
buildscript {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = 'Aquifree'
gdxVersion = '1.5.5'
roboVMVersion = '1.0.0'
box2DLightsVersion = '1.3'
ashleyVersion = '1.3.1'
aiVersion = '1.5.0'
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":android") {
apply plugin: "android"
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
configurations { natives }
dependencies {
compile project(":core")
compile "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-x86"
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-x86"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
apt 'com.google.dagger:dagger-compiler:2.0'
//provided 'javax.annotation:jsr250-api:1.0'
provided 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
compile 'com.google.dagger:dagger:2.0'
}
}
tasks.eclipse.doLast {
delete ".project"
}
我的android模块的build.gradle:
android {
buildToolsVersion "21.1.2"
compileSdkVersion 22
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
// needed to add JNI shared libraries to APK when compiling on CLI
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = new HashSet<File>()
pkgTask.jniFolders.add(new File(projectDir, 'libs'))
}
// 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() {
file("libs/armeabi/").mkdirs();
file("libs/armeabi-v7a/").mkdirs();
file("libs/x86/").mkdirs();
configurations.natives.files.each { jar ->
def outputDir = null
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.jar")) outputDir = file("libs/x86")
if(outputDir != null) {
copy {
from zipTree(jar)
into outputDir
include "*.so"
}
}
}
}
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.lucascauthen.aquifree.android/com.lucascauthen.aquifree.android.AndroidLauncher'
}
// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
// need to specify Java source sets explicitely, SpringSource Gradle Eclipse plugin
// ignores any nodes added in classpath.file.withXml
sourceSets {
main {
java.srcDirs "src", 'gen'
}
}
jdt {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
classpath {
plusConfigurations += [ project.configurations.compile ]
containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'
}
project {
name = appName + "-android"
natures 'com.android.ide.eclipse.adt.AndroidNature'
buildCommands.clear();
buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
buildCommand "org.eclipse.jdt.core.javabuilder"
buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
}
}
// sets up the Android Idea project, using the old Ant based build.
idea {
module {
sourceDirs += file("src");
scopes = [ COMPILE: [plus:[project.configurations.compile]]]
iml {
withXml {
def node = it.asNode()
def builder = NodeBuilder.newInstance();
builder.current = node;
builder.component(name: "FacetManager") {
facet(type: "android", name: "Android") {
configuration {
option(name: "UPDATE_PROPERTY_FILES", value:"true")
}
}
}
}
}
}
}
我的build.gradle为核心模块:
apply plugin: "java"
sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
eclipse.project {
name = appName + "-core"
}
我的模块类AssetLoaderModule.java:
package com.lucascauthen.modules;
import com.lucascauthen.util.AssetLoader;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class AssetLoaderModule {
@Provides @Singleton
AssetLoader provideAssetLoader() {
return new AssetLoader();
}
}
我的组件AssetLoaderComponent.java
package com.lucascauthen.components;
import com.lucascauthen.util.AssetLoader;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {AssetLoader.class})
public interface AssetLoaderComponent {
AssetLoader provideAssetLoader();
}
最后,我尝试创建AssetLoader对象的实例:
package com.lucascauthen.screens.MenuItems;
import com.lucascauthen.components.AssetLoaderComponent;
import com.lucascauthen.components.*;
import com.lucascauthen.util.AssetLoader;
public abstract class MenuItem {
protected AssetLoader assetLoader;
public abstract void dispose();
public MenuItem() {
AssetLoaderComponent component = DaggerAssetLoaderComponent.create();
assetLoader = component.provideAssetLoader();
}
}
问题与
DaggerAssetLoaderComponent.create()
一起出现。编译器无法识别该符号,因此正在生成代码。我尝试过的
清洁/建造/组装项目
将依赖项/插件添加到android模块本身
检查项目文件以查看代码是否存在但只是隐藏(不存在)
我在非libgdx项目中设置了匕首,并使其正常工作
我尝试同时使用glassfish注释和jrs250注释
我尝试通过使用安装文件为仅Android生成项目来将项目限制为仅Android。 (github链接不仅包含Android操作系统的代码,而且还包含更多代码)
在这一点上,对于我如何将不同的文件添加到各种build.gradle文件中,我认为这是愚蠢的。如果您需要更多信息,请在下面发表评论。
最佳答案
它在我们的项目中运行良好,但是我们在libgdx项目中具有高度自定义的gradle构建。
以下相关的build.gradle片段。
项目根目录build.gradle:
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// Android apt (without this, .java files are generated in classes dir instead of generated sources dir)
// resulting in errors IDE
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// Desktop apt (without this, .java files are generated in classes dir instead of generated sources dir)
// resulting in errors IDE
classpath "net.ltgt.gradle:gradle-apt-plugin:0.3"
}
}
allprojects {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
在android模块中,需要应用neenbedankt android-apt插件:
apply plugin: 'com.neenbedankt.android-apt'
在桌面模块中,需要应用ltgt apt插件。另外,需要应用Idea插件,否则IDEA无法将build / Generated / source / apt / main识别为源文件夹。
apply plugin: "net.ltgt.apt"
apply plugin: 'idea' // plugin needed to make IDEA see build/generated/source/apt/main as sourcefolder.