Java Libgdx和Dagger 2-不生成代码

Java Libgdx和Dagger 2-不生成代码,java,android,dependency-injection,libgdx,dagger-2,Java,Android,Dependency Injection,Libgdx,Dagger 2,我正在通过Graddle 2移植libgdx游戏以支持依赖注入 我遇到的问题是dagger似乎没有为我的组件生成代码。我相信问题在于libgdx处理gradle文件的方式,以及它如何将这些文件分割成不同的操作系统 我的整个(非常不完整的)项目都可以找到,但我将发布仅针对android的项目代码 整个项目的My build.gradle: buildscript { repositories { mavenCentral() maven { url "htt

我正在通过Graddle 2移植libgdx游戏以支持依赖注入

我遇到的问题是dagger似乎没有为我的组件生成代码。我相信问题在于libgdx处理gradle文件的方式,以及它如何将这些文件分割成不同的操作系统

我的整个(非常不完整的)项目都可以找到,但我将发布仅针对android的项目代码

整个项目的My 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模块的My 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")
                        }
                    }
                }
            }
        }
    }
}
我的模块类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();
    }
}
问题发生在
daggerasetLoaderComponent.create()的行上。编译器无法识别符号,因此正在生成代码

我所尝试的:

  • 清洁/建造/组装项目
  • 将依赖项/插件添加到android模块本身
  • 检查项目文件以查看代码是否存在,但只是隐藏(不存在)
  • 我在一个非libgdx项目中安装了dagger,并使其正常工作
  • 我已经尝试使用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.

我花了一整天的时间试图让它工作,看起来libgdx和dagger 2是不兼容的。不过,我不知道为什么。我也在做这件事。我看到生成的类,并得到Dagger类。起初,IntelliJ IDEA没有“看到”代码,这是因为默认情况下,.java文件是在build/mediates/classes中生成的,而不是build/generated/source/apt。您是否在build目录中找到DaggerasetLoaderComponent.java?当我尝试它时,我找不到生成的文件,就好像它们从未被创建过一样。实际上,我已经在我们的libgdx项目中集成了Dagger 2。在桌面和安卓系统上运行良好(RoboVM/iOS还没有尝试过,我们很快就会开始)有没有办法发布gradle文件?这可能会帮助我明白我做错了什么。或者至少是与匕首2有关的部分?我会很快检查这是否对我有效。非常感谢!
apply plugin: "net.ltgt.apt"
apply plugin: 'idea' // plugin needed to make IDEA see build/generated/source/apt/main as sourcefolder.