问题描述
我正在尝试在 Android Studio 中使用 AndroidAnnotations 创建一个项目.当我构建并运行项目时,一切似乎都编译得很好,但除了应用程序的空白活动外,我什么也没有.此外,AndroidAnnotations 似乎没有生成任何内容.
I am trying to create a project using AndroidAnnotations in Android Studio. When I build and run the project, everything seems to compile fine, yet I get nothing but a blank activity for the app. In addition, it does not appear that anything is generated by AndroidAnnotations.
我已经添加了 androidannotations-api-2.7.1.jar
作为我项目的依赖项,并启用了带有处理器路径的注释处理 androidannotations-2.7.1.jar 的路径
,它与 androidannotations-api-2.7.1.jar
位于单独的文件夹中.我检查了相对于模块内容根的存储生成源,并尝试了许多不同的源目录——从 generated
,到 gen/aa
,到(当前)build/source/aa
以匹配在 Android Studio 中似乎生成的文件的位置.没有任何效果.我已将清单中的活动名称更改为 Activity_
,并将配置设置为在项目运行时启动它.
I have added androidannotations-api-2.7.1.jar
as a dependency for my project, and enabled annotation processing with the processor path the path to androidannotations-2.7.1.jar
, which is in a separate folder from androidannotations-api-2.7.1.jar
. I have checked store generated sources relative to module content root, and tried many different directories for the sources -- from generated
, to gen/aa
, to (currently) build/source/aa
to match where it seems the generated files are created in Android Studio. Nothing has worked. I have changed the activity name in the manifest to Activity_
, and set the configuration to launch this when the project is run.
我仅有的其他依赖项是 android-support-v4 和 ActionBarSherlock.我已经尝试过禁用这两个功能,但没有结果.我最初计划将 Roboguice 与 AndroidAnnotations 结合使用,但暂时禁用了它,以尝试专注于这个问题.
The only other dependencies I have are android-support-v4 and ActionBarSherlock. I have tried with both of these disabled, to no result. I initially planned to use Roboguice in conjunction with AndroidAnnotations, but have disabled it for the time being to try to focus on this issue.
我也在使用或尝试使用 Gradle.这是目前我的 build.gradle
:
I am also using, or trying to use, Gradle. This is currently my build.gradle
:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/actionbarsherlock-4.3.1.jar')
compile files('libs/androidannotations-api-2.7.1.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
}
}
然而,我还没有真正弄清楚 Gradle 是如何工作的,所以我主要只是像普通项目一样手动添加依赖项,然后将编译行放在 Gradle 中,以便项目能够正确编译.我知道这可能不是正确的使用方式.
However, I haven't really figured out how Gradle works, so I mostly just manually added the dependencies as you would a normal project, then put the compile lines in Gradle so the project would compile properly. I know this is probably not the correct way to use it.
我的活动及其布局相当标准,我只是从官方指南中复制它们以开始使用 AndroidAnnotations.
My activity and its layout are fairly standard, I just copied them from the official guide to get started with AndroidAnnotations.
更新:所以我只是回到 Maven 来测试构建,我注意到一些奇怪的事情.似乎即使我在 Maven 中设置它,也没有生成任何内容.但是,使用 Maven 构建,我可以在不将清单中的活动名称更改为 Activity_
的情况下运行该项目,并且该项目将正确编译和运行.这很奇怪,似乎它可能会进一步混淆问题,或者如果它也表明 Gradle 存在某些问题,则会简化它.
UPDATE: So I just went back to Maven to test the build with that, and I noticed something strange. It seems that even with how I have it set up in Maven, nothing is generated. However, with the Maven build I can run the project without changing the activity name in the manifest to Activity_
and the project will compile and run correctly. This is very odd and seems like it could either further confuse the problem, or simplify it if it is indicative of something with Gradle as well.
推荐答案
这个类似于robotoaster的响应,但是它在0.4.1中工作,并将生成的java源文件放在一个新目录中(与其他生成的源一致文件夹),它允许 Android Studio 查看源代码并停止抱怨.它还适用于更多注释处理器.只需将您的注释处理器添加到apt"配置中即可.
This is similar to robotoaster's response, but it works in 0.4.1 and it places the generated java source files in a new directory (consistent with the other generated source folders), which allows Android Studio to see the source and stop complaining. It also works with more annotation processors. Just add your annotation processors to the "apt" configuration.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.1'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
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}"
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 17
}
}
android.applicationVariants.all { 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
]
}
}
UPDATE:这仍然可以编译,但是在 Android Studio 0.1.1 中您不能再使用 UI.
UPDATE: This still works to compile, but with Android Studio 0.1.1 you can no longer edit your project structure with the UI.
UPDATE 2:您可以通过在项目浏览器中右键单击 build/source/apt_generated/debug 并选择 来让 Android Studio 0.1.1 识别 apt 生成的源文件将目录标记为->源根目录
UPDATE 2: You can get Android Studio 0.1.1 to recognize the apt-generated source files by right-clicking on build/source/apt_generated/debug in the project browser and selecting Mark Directory As->Source Root
UPDATE 3:从 gradle 插件 0.5.5 开始,android.applicationVariants.each
不再起作用.请改用 android.applicationVariants.all
.请参阅 android.com 上的变更日志:访问变体容器不会强制创建任务.这意味着 android.[application|Library|Test]Variants 在评估阶段将为空.要使用它,请使用 .all 而不是 .each
UPDATE 3: Since gradle plugin 0.5.5 the android.applicationVariants.each
does not work anymore. Use android.applicationVariants.all
instead. See the changelog at android.com:access to the variants container don't force creating the task.This means android.[application|Library|Test]Variants will be empty during the evaluation phase. To use it, use .all instead of .each
这篇关于AndroidAnnotations 未生成任何内容,活动为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!