Android gradle中的apt依赖范围 - 它用于什么? [英] apt dependency scope in Android gradle - what is it used for?
问题描述
一个例子看起来像这样吗?
apply plugin:'com.android.application'
apply plugin:'com.neenbedankt.android-apt'
android {
compileSdkVersion 20
buildToolsVersion '20 .0.0'
defaultConfig {
applicationIdorg.ligboy.test.card.module1
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName1.0
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard- rules.pro'
}
}
}
final DAGGER_VERSION ='2.0.2'
依赖项{
编译com.google.dagger:dagger:$ {DAGGER_VERSION}
aptcom.google.dagger:dagger-compiler:$ {DAGGER_VERSION}//这个范围是什么
提供'org.glassfish:javax.annotation:10.0-b28'
}
和in最高级别的build.gradle文件具有这种全局依赖性:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
请注意,依赖项部分有一个适用范围?我只知道编译,封装和提供的范围。编译
包括编译时和包中的依赖关系,只要在编译时包含库并将其丢弃在
包时,所以它不包含在最终版本中。和Package是相反的,它包含了包中的依赖关系,而不是在编译时。
但是什么是apt依赖范围,我们显然需要com.neenbedankt.android-apt来运行它,所以我知道它的基于android的。
update:
为什么我不能使用提供的依赖范围而不是 apt 范围?他们有什么不同?
android- apt插件协助与Android Studio结合使用注释处理器。它有两个目的:
允许将仅编译时注释处理器配置为依赖项,不包括最终的工件APK或library
- 设置源代码路径,以便从注释处理器生成的代码可以被Android Studio正确地获取。
您正在使用Dagger,它使用注释处理来生成代码。注释处理代码不应包含在最终APK中,并且您希望生成的代码对Android Studio可见。 android-apt
启用此行为。
这听起来非常类似于 code> scope,但是 apt
与提供
的几个关键方式不同。第一个区别是由IDE提供的 apt
依赖项生成的代码可用于IDE,而由生成的代码提供了
依赖项不是。
另一个重要区别是,使用提供的
范围的库中的代码位于IDE类路径中(即你可以导入类并尝试使用它们),而 apt
依赖项中的代码不是。使用提供的
,如果您实际上没有提供引用的依赖关系,那么您的代码将在运行时崩溃,而且
您可以找到关于 apt
vs 的讨论在这个
android- apt
问题。
在Dagger的情况下,应该没有理由将注释处理器和代码生成器包含在的代码(提供的
范围允许)。因此, apt 范围更合适。
2016年10月更新:
您可能不再需要 apt
和 android-apt
插件。 Android Gradle插件的2.2版有一个 annotationProcessor
配置,您应该使用它。
更多< a href =http://www.littlerobots.nl/blog/Whats-next-for-android-apt/ =noreferrer> android-apt的下一步是什么?
What is the apt dependency scope in android gradle files i see sometimes ?
An example looks like this?
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
applicationId "org.ligboy.test.card.module1"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
final DAGGER_VERSION = '2.0.2'
dependencies {
compile "com.google.dagger:dagger:${DAGGER_VERSION}"
apt "com.google.dagger:dagger-compiler:${DAGGER_VERSION}"//what is this scope
provided 'org.glassfish:javax.annotation:10.0-b28'
}
and in the top level build.gradle file it has this global dependency:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
Notice in the dependencies section there is a apt scope ? i only know of compile, package and provided scope. compile includes the dependency at compile time and in your package, provided says only include the library at compile time and discard it at package time so its not included in final build. and Package is the reverse, it includes the dependency in the package and not at compile time. But what is apt dependency scope which we obviously need the com.neenbedankt.android-apt for it to work so i know its android based.
update: why cant i use provided dependency scope instead of apt scope ? How do they differ ?
i created a tutorial on dagger dependency scopes for those who need more info.
From the android-apt
project page:
The android-apt plugin assists in working with annotation processors in combination with Android Studio. It has two purposes:
Allow to configure a compile time only annotation processor as a dependency, not including the artifact in the final APK or library
Set up the source paths so that code that is generated from the annotation processor is correctly picked up by Android Studio.
You are using Dagger, which uses annotation processing to generate code. The annotation processing code shouldn't be included in the final APK, and you want the generated code to be visible to Android Studio. android-apt
enables this behavior.
This sounds very similar to the provided
scope, but apt
differs from provided
in a few key ways. The first difference is that code generated by an apt
dependency is available to the IDE, whereas code generated by a provided
dependency is not.
Another important difference is that the code in a library using the provided
scope is on the IDE classpath (i.e. you can import the classes and attempt to use them), whereas code in an apt
dependency is not. With provided
, your code will crash at runtime if you don't actually provide the referenced dependencies with a compile
scoped counterpart.
You can find a discussion about apt
vs provided
on this android-apt
issue.
In the case of Dagger, there should be no reason to include the annotation processor and code generator in any of your code (which the provided
scope would allow). Thus the apt
scope is more appropriate.
Update for October 2016:
You probably don't need apt
and the android-apt
plugin anymore. Version 2.2 of the Android Gradle plugin has an annotationProcessor
configuration that you should be using instead.
See more at What's next for android-apt?
这篇关于Android gradle中的apt依赖范围 - 它用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!