android-apt

groovy 插件 翻译

[TOC]

原文

android-apt是什么?

android-apt 是一个Gradle插件,协助Android Studio 处理annotation processors, 它有两个目的:

  1. 允许配置只在编译时作为注解处理器的依赖,而不添加到最后的APK或library
  1. 设置源路径,使注解处理器生成的代码能被Android Studio正确的引用

此插件依赖项目中配置androidandroid-library(version 0.9.x or up),

添加android-apt到构建脚本中

使用该插件,添加如下到你的构建脚本中:

//配置在Project下的build.gradle中
buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
        //替换成最新的 gradle版本
        classpath 'com.android.tools.build:gradle:1.3.0'
        //替换成最新android-apt版本
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}
//配置到Module下的build.gradle中
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

传递处理器参数

有些注解处理器需要传递参数,你能通过使用apt.arguments达到此目的,如下是一个向AndroidAnnotations传递参数的例子

apt {
    arguments {
            resourcePackageName android.defaultConfig.applicationId
            androidManifestFile variant.outputs[0]?.processResources?.manifestFile
    }
}

AndroidAnnotations 需要知道AndroidManifest.xml文件所在的位置,检索variant获取不同flavor的AndroidManifest.xml文件。然而并非所有变量都有这个属性(例如,单元测试就没有),因此使用groovy操作符?.区分不能属性的不同的情况。
取参数以也通过以下定义一个函数,相关Groovy语法不翻译(暂时会Groovy)一看就明白。

def getManifestByVariant(variant) {
    // return the value based on the variant
    if (variant.name == 'release') {
        return '/my/path/to/the/manifest.xml'
    }
    return variant.outputs[0]?.processResources?.manifestFile
}

apt {
     arguments {
         if (variant.name == 'debug') {
            resourcePackageName "com.myapp.package.name.debug"
            // more options
         }             
         androidManifestFile project.getManifestByVariant(variant)             
     }
}

配置不同编译风格的依赖

注解处理器一般包含API和使用API生成代码的processor。项目依赖可能分为多个部分。例如Dagger有两个组件Dagger-compiler和dagger。dagger-commpiler仅用于编译时,运行时必需使用dagger。
配置此类组件,apt能这样使用

dependencies {
 apt 'com.squareup.dagger:dagger-compiler:1.1.0'
 compile 'com.squareup.dagger:dagger:1.1.0'
}

如果是instrumentation test并需要在Android Studio 中显示processor生成的代码,使用androidTestApt

dependencies {
 androidTestApt 'com.github.frankiesardo:android-auto-value-processor:0.1'
 androidTestCompile 'com.github.frankiesardo:android-auto-value:0.1'
}

配置 unit test 使用 testApt (注:要求Android Studio 是1.4以上版本)

dependencies {
 testApt 'com.github.frankiesardo:android-auto-value-processor:0.1'
 testCompile 'com.github.frankiesardo:android-auto-value:0.1'
}

其它配置

它可以指定编译时处理器的类名,并禁用javac默认的发现机制,需要配置这些类到apt块中:

apt {
    processor "my.class.name"
    processor "another.processor.class.name"
    // 仅运行上面的声名的处理器类
    disableDiscovery true
}

配置其它注解处理器

添加其它注解器不需要额外的配置,你只需要使用apt添加相关组件到compile模块中。另外,处理器所有生成资源都在代码正常引用。

FAQ

Q:什么时候需要这个插件?

A:当你需要引入Processors 生成的源代码到你的代码中时。例如,当你使用Dagger 2 或 AndroidAnnotaition.该插件使得Android Studio可以配置生成资源的build path,避免IDE报错。当使用 apt添加添加依赖,它将不会被包含到最终的APK里。

Q:provided vs apt 使用注解处理器的不同?

A:provided 将会导入注解处理器的classes和它的依赖到IDE的类路径下。这意味着你可以附带的引入并使用这些classes。例如,当注解处理器使用Guava,你可能错误的import其相关代码到你的Android 代码中。当运行时将导致crash。而使用apt,注解处理器的classes将不会添加到你当前的类路径下,仅仅用于注解处理过程。并且会把所有注解处理器生成的source放在IDE的类路径下,方便Android Studio引用。

    原文作者:总是说下次
    原文地址: https://www.jianshu.com/p/2494825183c5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞