【问题标题】:Square Dagger: How to configure Android Studio?Square Dagger:如何配置 Android Studio?
【发布时间】:2014-11-28 15:06:44
【问题描述】:

如何配置 Android Studio 项目以使用 Square Dagger?我让它在 Eclipse 中运行,但我绝对不知道如何设置 Android Studio 来进行注释和代码生成。

编辑:我的 build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 21
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.test.myapp"
        minSdkVersion 18
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

configurations {
    apt
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:6.1.71'
    compile 'com.android.support:support-v4:21.0.2'
    apt "org.androidannotations:androidannotations:3.2"
    compile "org.androidannotations:androidannotations-api:3.2"
    apt 'com.squareup.dagger:dagger-compiler:1.2.2'
    compile 'com.squareup.dagger:dagger:1.2.2'
    compile 'javax.inject:javax.inject:1'
}

def getSourceSetName(variant) {
    return new File(variant.dirName).getName();
}

android.applicationVariants.all { variant ->
    def aptOutputDir = project.file("${project.buildDir}/source/apt_generated    /${variant.dirName}")
    def aptOutput = new File(aptOutputDir, variant.dirName)
    println "****************************"
    println "variant: ${variant.name}"
    println "manifest:  ${variant.processResources.manifestFile}"
    println "aptOutput:  ${aptOutput}"
    println "****************************"

    android.sourceSets[getSourceSetName(variant)].java.srcDirs+= aptOutput.getPath()

    variant.javaCompile.options.compilerArgs += [
            '-processorpath', configurations.apt.getAsPath(),
            '-AandroidManifestFile=' + variant.processResources.manifestFile,
            '-s', aptOutput
    ]

    variant.javaCompile.source = variant.javaCompile.source.filter { p ->
        return !p.getPath().startsWith(aptOutputDir.getPath())
    }

    variant.javaCompile.doFirst {
        aptOutput.mkdirs()
    }
}

【问题讨论】:

    标签: android-studio installation code-generation dagger


    【解决方案1】:

    试试下面的 apt 插件 - 它对我们有用。

            com.jimdo.gradle:gradle-apt-plugin:0.2-SNAPSHOT
    

    在您的 buildscript 存储库中添加闭包

    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    

    并在 buildscript 依赖闭包中添加

    classpath 'com.jimdo.gradle:gradle-apt-plugin:0.2-SNAPSHOT'
    

    然后在你的应用 build.gradle 中

    apply plugin: 'apt'
    

    您已经在应用程序依赖项关闭中获得了 dagger 的 compile 和 apt 依赖项,因此您应该一切顺利。我不确定对org.androidannotations:android annotations:3.2 的恰当依赖是为了什么。如果您将其用于支持您的 apt 解决方案,您可能可以将其取出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 2018-01-28
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多