【问题标题】:Android studio + Gradle + Android Annotations [closed]Android studio + Gradle + Android Annotations [关闭]
【发布时间】:2014-01-31 18:15:03
【问题描述】:

我正在尝试将 AndroidAnnotations 添加到具有 gradle 构建系统的 Android Studio 项目中。有人做过吗?谁能帮我这个?我什至不知道从哪里开始。我知道如何将库添加到 gradle 但 AndroidAnnotations 需要 2 个 jar 文件,我不知道该怎么做。

【问题讨论】:

    标签: android annotations gradle android-studio


    【解决方案1】:
    After 4 months I am 4 months older and a little smarter :) If you want to use      
    Annotations in Android use http://jakewharton.github.io/butterknife/. 
    It is way better and it is easy to set up :)
    

    这是你需要做的:

    • 您需要修改 build.gradle 文件(应用程序模块的构建文件)

    首先添加匕首和注释版本。您也可以在依赖项中声明它们。当您有很多依赖项时,这会更方便。

    ext.daggerVersion = '1.0.0';
    ext.androidAnnotationsVersion = '2.7.1';
    
    configurations {
        apt
    }
    

    添加依赖:

    dependencies {
    
    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
    apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
    compile "com.squareup.dagger:dagger:${daggerVersion}"
    
    }
    

    最后,添加这个。这将为编译器添加路径并为生成的文件创建一个目录(此目录将被称为 apt_generated):

    android.applicationVariants.each { variant ->
    aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
    println "****************************"
    println "variant: ${variant.name}"
    println "manifest:  ${variant.processResources.manifestFile}"
    println "aptOutput:  ${aptOutput}"
    println "****************************"
    
    variant.javaCompile.doFirst {
        println "*** compile doFirst ${variant.name}"
        aptOutput.mkdirs()
        variant.javaCompile.options.compilerArgs += [
                '-processorpath', configurations.apt.getAsPath(),
                '-AandroidManifestFile=' + variant.processResources.manifestFile,
                '-s', aptOutput
        ]
    }
    }
    

    哦,是的,构建应用程序后,您需要转到项目根目录/build/apt_generated,右键单击文件夹并设置“标记为源根目录”

    【讨论】:

    • 这行得通!非常感谢!
    • 为什么需要Dagger?它与 Android Annotations 有某种关系?
    • 注解jar是自动下载的,还是我需要添加?我自己?
    • 它对我不起作用。相反,我使用了一个稍微不同的版本:stackoverflow.com/questions/19351168/…
    • compile "com.googlecode.androidannotations:androidannotations api:${androidAnnotationsVersion}" 应该是 compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}" .. 注意缺少的 '- ' 在你的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多