问题描述
我有一个摇篮构建是使用批注处理器(Android的注解)生成code脚本。建筑是罚款,直到我添加了一个新的临香精。我可以建立在免费的味道,但是当我建立专业味注释处理器未运行。这将导致丢失的code和构建失败。
I have a Gradle build script that is using an annotations processor (Android Annotations) to generate code. Building was fine until I added a new Pro Flavor. I can build the Free flavor but when I build the Pro flavor the annotations processor isn't run. This causes missing code and the build fails.
下面是我的脚本:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
ext.androidAnnotationsVersion = '3.0-SNAPSHOT';
configurations {
apt
}
dependencies {
compile files('libs/android-support-v13.jar')
compile fileTree(dir: 'libs', include: '*.jar')
apt "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
compile "org.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
versionCode 29
versionName "2.0.3"
packageName "com.MyCompany.MyApp"
}
productFlavors {
free {
buildConfig "final public static boolean PRO_VERSION = false;"
}
pro {
packageName "com.MyCompany.MyApp.Pro"
versionName (versionName + ".Pro")
buildConfig "final public static boolean PRO_VERSION = true;"
}
}
buildTypes {
release {
buildConfig "final public static String BASE_URL = \"http://data.MyCompany.com/\";", \
"final public static String APP_NAME = \"com.MyCompany.MyApp\";"
}
debug {
buildConfig "final public static String BASE_URL = \"http://192.168.1.15/GDM/\";", \
"final public static String APP_NAME = \"com.MyCompany.MyApp\";"
}
}
}
def getSourceSetName(variant) {
return new File(variant.dirName).getName();
}
android.applicationVariants.all { variant ->
def aptOutputDir = project.file("build/source/apt")
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()
}
}
在建立免费的变种,注释处理器运行由摇篮输出如下所示:
When building the free variants, the annotations processor is run as indicated by the following in the gradle output:
Note: Starting AndroidAnnotations annotation processing
在构建失败在临的变种,注释处理器不运行,所以引用到生成的code。
When building the Pro variants, the annotations processor doesn't run, so the references to the generated code fails.
关于这个奇怪的是,我发现(真正的意外),如果我删除软件包名com.MyCompany.MyApp.Pro
从脚本.. ..the批注处理器运行,它会建立正确。我需要更新谷歌播放的包名。
The curious thing about this is, I found (truly by accident) that if I remove the packageName "com.MyCompany.MyApp.Pro"
from the script....the annotations processor runs and it will build correctly. I need to update the the package name for Google Play.
当看在Android的工作室,你可以看到,APT(注释处理工具)是显示在临版本为有效,即使我在 FreeDebug 建设变种选择。我不知道这是说明一个问题,或者如果这仅仅是一个测试版的Android工作室的问题(安卓工作室版本:0.2.13)。因此,采取与一粒盐。
When looking in android studio, you can see that the apt (the Annotation Processing Tool) is showing the Pro version as being active even when I have the FreeDebug build variant selected. I am not sure this is indicative of a problem or if this is just a problem with the beta android studio (Android Studio version: 0.2.13). So take that with a grain of salt.
我是新来的摇篮构建系统,但我的认为的我得到了它的窍门。我已经看过剧本了一遍又一遍,我不明白为什么批注处理器未运行在 PRO 变异。而除了运行与--info并且--debug参数的包装,我还不知道如何调试这些问题。
I'm new to the gradle build system but I thought I was getting the hang of it. I have looked at the script over and over again and I don't see why the annotations processor is not running for the pro variant. And aside from running the wrapper with the --info and --debug arguments, I don't know yet how to debug these problems.
我已经运行的摇篮包装用的 -info 和 -debug ,以获得更多的输出,但里面空空如也,表明任何其他错误(或缺少项)直到它到达所造成的缺失产生的code中的错误。因此,这使我相信这只是该androidannotations没有被运行与变体是根本问题的事实。 (即我不认为这是什么引起的上游和被误报后来一个错误。我可能是错的,虽然)
I've run the gradle wrapper with the -info and -debug to get extended output but there is nothing there that indicates any other error (or missing item) until it reaches the error caused by the missing generated code. So this leads me to believe it's just the fact that the androidannotations are not being run with that variant that is the root problem. (i.e. I don't think this is an error caused by something upstream and being misreported later on. I could be wrong though)
我真的很茫然,并一直坚持这个现在2天。
I'm really at a loss and have been stuck with this for 2 days now.
推荐答案
我是能够解决的问题。细算一点点接近-info 的摇篮包装的输出,我发现androidAnnotations试图运行。该错误输出并没有按照正确的顺序为批注处理的消息传来,被引用不存在的code(不存在的,因为批注处理失败)造成的错误后
I was able to solve the problem. After looking a little closer at the -info output of the gradle wrapper, I found that androidAnnotations was attempting to run. The error output was NOT in the correct order as the annotations processing message came AFTER the errors that were caused by referencing the non-existent code (that didn't exist because annotations processing failed).
下面是日志:
:MyCompany:compileProDebug
....\src\main\java\com\MyCompany\MyApp\Activities\activityMain.java:14: error: cannot find symbol
import com.MyCompany.MyApp.Notifications.NotificationSetupActivity_;
^
symbol: class NotificationSetupActivity_
location: package com.MyCompany.MyApp.Notifications
....\src\main\java\com\MyCompany\MyApp\Activities\activityMain.java:24: error: cannot find symbol
import com.MyCompany.MyApp.FantasyScores.ActivityScores_;
^
symbol: class ActivityScores_
location: package com.MyCompany.MyApp.Scores
....\src\main\java\com\MyCompany\MyApp\Activities\activityMain.java:32: error: cannot find symbol
import com.MyCompany.MyApp.Team.activityTeamSelect_;
^
symbol: class activityTeamSelect_
location: package com.MyCompany.MyApp.Team
Note: Starting AndroidAnnotations annotation processing
Note: AndroidManifest.xml file found: ....\build\manifests\pro\debug\AndroidManifest.xml
error: The generated com.MyCompany.MyAppPro.R class cannot be found
Note: Time measurements: [Whole Processing = 190 ms], [Extract Manifest = 129 ms], [Extract Annotations = 49 ms],
....\src\main\java\com\MyCompany\MyApp\Activities\activityMain.java:14: error: cannot find symbol
import com.MyCompany.MyApp.Notifications.NotificationSetupActivity_;
最重要的线路是这些:
The important lines are these:
注意:启动AndroidAnnotations注释处理
注:AndroidManifest.xml文件中发现:.... \建立\清单\ PRO \调试\ AndroidManifest.xml中
Note: AndroidManifest.xml file found: ....\build\manifests\pro\debug\AndroidManifest.xml
错误:生成的com.MyCompany.MyAppPro.R类不能被找到
这些的应该在错误日志是第一个的作为注解处理器运行完整的编译步骤之前,但出于某种原因,他们被深埋(在androidannotations处理器??也许是齐平的问题)
These should have been first in the error log as the annotations processor runs before the full compile step, but for some reason they were buried deep (maybe a flush problem in the androidannotations processor??)
在任何情况下,第三行的地方无法找到 com.MyCompany.MyAppPro.R
是关键。这些资源实际上是 com.MyCompany.MyApp.R
(不专业)。挖了一下后,我发现这个帖子表明这是AndroidAnnotations一个已知的问题。
At any rate, the third line where it can't find the com.MyCompany.MyAppPro.R
is the key. The resources are actually in com.MyCompany.MyApp.R
(no Pro). After digging a bit, I found this post which indicates this is a known issue with AndroidAnnotations.
我可以通过添加来解决这个问题 - AresourcePackageName = MyBasePackageName',
参数来构建脚本。 注意:如果您使用的是3.0快照这仅适用。 AndroidAnnotations最新发布的版本不支持的 -AresourcePackageName 的选项。
I was able to resolve that problem by adding the '-AresourcePackageName=MyBasePackageName',
parameter to the build script. NOTE: this only works if you are using the 3.0 snapshot. The latest released version of AndroidAnnotations does not support the -AresourcePackageName option.
添加参数后,所有变量建立正确。
After adding the parameter, all variants build correctly.
构建脚本的compilerArgs部分现在看起来是这样的:
The compilerArgs section of the build script now looks like this:
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-AresourcePackageName=MyBasePackageName',
'-s', aptOutput
]
希望这将帮助其他人避免将来出现此问题。
Hopefully this will help others avoid this problem in the future.
这篇关于摇篮+注释+香精=将无法运行批注处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!