0

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

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

我的整个(非常不完整的)项目可以在这里找到,但我将发布仅针对 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 项目中设置了 dagger 并让它工作得很好
  • 我尝试使用 glassfish 注释和 jrs250 注释
  • 我尝试通过使用安装文件生成仅适用于 android 的项目,将项目限制为仅适用于 android。(但 github 链接包含的代码不仅仅适用于 android 操作系统)

在这一点上,我认为我如何将不同的文件添加到各种 build.gradle 文件只是一件愚蠢的事情。如果您需要更多信息,请在下面发表评论。

4

1 回答 1

2

它在我们的项目中运行良好,但我们的 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插件,否则build/generated/source/apt/main不会被IDEA识别为源文件夹。

apply plugin: "net.ltgt.apt"
apply plugin: 'idea' // plugin needed to make IDEA see build/generated/source/apt/main as sourcefolder.
于 2015-07-07T20:32:57.887 回答