gpt4 book ai didi

java - Libgdx 和 Dagger 2 - 不生成代码

转载 作者:行者123 更新时间:2023-12-02 04:39:31 24 4
gpt4 key购买 nike

我正在努力移植我的 libgdx 游戏以支持通过 Graddle 2 进行依赖注入(inject)。

我遇到的问题是 dagger 似乎没有为我的组件生成代码。我认为问题在于 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() 线上编译器无法识别该符号,因此正在生成代码。

我尝试过的:

  • 清理/ build /组装项目
  • 将依赖项/插件添加到 Android 模块本身
  • 检查项目文件以查看代码是否存在但只是隐藏(不存在)
  • 我在非 libgdx 项目中设置了 dagger,并让它正常工作
  • 我尝试过使用 glassfish 注释和 jrs250 注释
  • 我尝试使用安装文件生成仅适用于 Android 的项目,从而将项目限制为仅适用于 Android。 (github 链接包含的代码不仅仅是 Android 操作系统的代码)

此时,我认为关于如何将不同的文件添加到各个 build.gradle 文件中,这只是一些愚蠢的事情。如果您需要更多信息,请在下面评论。

最佳答案

它在我们的项目中运行良好,但我们的 libgdx 项目中有一个高度定制的 gradle 构建。

下面是相关的 build.gradle fragment 。

项目根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.

关于java - Libgdx 和 Dagger 2 - 不生成代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com