0

Graddle 2 による依存性注入をサポートするために、libgdx ゲームの移植に取り組んでいます。

私が直面している問題は、dagger がコンポーネントのコードを生成していないように見えることです。問題は、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 以外のプロジェクトでダガーをセットアップし、問題なく動作するようにしました
  • Glassfish アノテーションと jrs250 アノテーションの両方を使用してみました
  • セットアップファイルを使用してAndroidのみのプロジェクトを生成することにより、プロジェクトをAndroidのみに制限しようとしました。(ただし、github リンクには、Android オペレーティング システム以外のコードも含まれています)

この時点で、さまざまなファイルをさまざまな build.gradle ファイルに追加する方法について、ばかげていると考えています。さらに情報が必要な場合は、以下にコメントしてください。

4

1 に答える 1