【问题标题】:AndroidAnnotations Nothing Generated, Empty ActivityAndroidAnnotations 未生成任何内容,为空 Activity
【发布时间】:2013-05-22 05:16:03
【问题描述】:

我正在尝试在 Android Studio 中使用 AndroidAnnotations 创建一个项目。当我构建并运行该项目时,一切似乎都可以正常编译,但我得到的只是应用程序的空白活动。此外,AndroidAnnotations 似乎没有生成任何内容。

我已将androidannotations-api-2.7.1.jar 添加为我的项目的依赖项,并使用处理器路径启用注释处理androidannotations-2.7.1.jar,它位于与androidannotations-api-2.7.1.jar 不同的文件夹中。我检查了相对于模块内容根目录生成的存储源,并尝试了许多不同的源目录——从generated,到gen/aa,到(当前)build/source/aa,以匹配生成文件的创建位置安卓工作室。没有任何效果。我已将清单中的活动名称更改为Activity_,并将配置设置为在项目运行时启动它。

我拥有的唯一其他依赖项是 android-support-v4 和 ActionBarSherlock。我已经尝试过禁用这两个,但没有结果。我最初计划将 Roboguice 与 AndroidAnnotations 结合使用,但暂时禁用了它以尝试专注于这个问题。

我也在使用或尝试使用 Gradle。这是目前我的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 中,以便项目能够正确编译。我知道这可能不是正确的使用方式。

我的活动及其布局相当标准,我只是从官方指南中复制它们以开始使用 AndroidAnnotations。

更新:所以我只是回到 Maven 来测试构建,我发现了一些奇怪的东西。似乎即使我在 Maven 中进行了设置,也没有生成任何内容。但是,通过 Maven 构建,我可以运行项目,而无需将清单中的活动名称更改为 Activity_,并且项目将正确编译和运行。这很奇怪,似乎它可能会进一步混淆问题,或者如果它也表明 Gradle 的某些东西,它会简化它。

【问题讨论】:

  • 也许这会有所帮助->link

标签: android gradle android-studio android-annotations


【解决方案1】:

这类似于robotoaster的响应,但它在0.4.1中工作,并将生成的java源文件放在一个新目录中(与其他生成的源文件夹一致),这允许Android Studio查看源并停止抱怨.它还适用于更多注释处理器。只需将注释处理器添加到“apt”配置即可。

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
        ]
    }
}

更新:这仍然可以编译,但在 Android Studio 0.1.1 中,您无法再使用 UI 编辑项目结构,因此您无法告诉 AS 查看新的源文件夹。我想将文件夹添加到 sourceSet,但变体似乎实际上并没有自己的 sourceSet,所以我不确定将它放在哪里

更新 2:您可以通过在项目浏览器中右键单击 build/source/apt_generated/debug 并选择 来让 Android Studio 0.1.1 识别 apt 生成的源文件将目录标记为->源根目录

更新 3:由于 gradle 插件 0.5.5,android.applicationVariants.each 不再工作。请改用android.applicationVariants.all。请参阅android.com 的更新日志: 访问变体容器不会强制创建任务。 这意味着 android.[application|Library|Test]Variants 在评估阶段将为空。要使用它,请使用 .all 而不是 .each

【讨论】:

  • 这是一个很好的解决方案,特别是因为它允许多个注释处理器。因此,我已将其更改为标记为已接受的答案,但是此解决方案和 robotoaster 的解决方案都运行良好。感谢所有的帮助。
  • 难道不能将生成的文件也添加到 src 路径中,以便 Android Studio 获取吗?摆弄项目设置会破坏构建文件的目的......
  • 另外,如果你的 gradle 构建文件使用packageNameSuffix 属性,你需要添加参数-AresourcePackageName=yourPackage 否则你的 R 类不会被 AA 处理器找到(需要最新的 AA 3.0)
  • 我在使用 android studio 0.1.8 和 android gradle 0.4.2 时遇到问题,有什么提示吗?
  • @Shockwave Android Studio 无法识别 Activity_(下划线类)并且应用因 java.lang.RuntimeException 崩溃:无法实例化活动 ComponentInfo (...MainActivity_)
【解决方案2】:

有了这里的答案和+Hugo Visser 的帮助,他在我的Google+ Question 上回答了我,我得到了这个 build.grade 配置,它允许gradew 构建并且还在 Android Studio 中添加了 apt 输出路径作为源目录。

buildscript {
    repositories {
        mavenCentral()
    }
    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 'com.android.support:support-v4:13.0.+'

    apt "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "org.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 17
    }
}

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()
    }
}

更新:为 gradle android 插件 0.5.5 更新;将android.applicationVariants.each 更改为android.applicationVariants.all

【讨论】:

  • 注意使用此配置的其他人(这是我使用的配置)。如果您决定稍后将 Flavors 添加到此配置并重命名包(例如 com.mycompany.myapp 到 com.mycompany.myapp.pro),您可能会遇到解决“pro”中的 R.Java 类文件的问题版本。要解决此问题,请使用“-AresourcePackageName=MyBasePackageName”参数将 androidannotations 重定向到正确的 R.Java 文件。如需更深入的解释,请参阅stackoverflow.com/questions/19351168/…
【解决方案3】:

添加依赖项不会做任何事情。必须通知 JavaCompile 任务有关注释处理器的信息。在 0.3 版本中可以访问 JavaCompile 任务:

configurations {
    compile
    androidannotations.extendsFrom(compile) 
}

dependencies {
    compile 'org.androidannotations:androidannotations-api:3.0-SNAPSHOT'
    androidannotations 'org.androidannotations:androidannotations:3.0-SNAPSHOT'
}

android.buildVariants.each {
  variant ->
    variant.javaCompile.options.compilerArgs += [
        '-classpath', configurations.compile.asPath,
        '-processorpath', configurations.androidannotations.asPath,
        '-processor', 'org.androidannotations.AndroidAnnotationProcessor',
        '-AandroidManifestFile=' + variant.processResources.manifestFile
    ]
}

对于 0.4 以后的 gradle 插件,编译任务必须以不同的方式附加。感谢Artiom @ Android Project ,这是整个 build.gradle 的样子

buildscript {
 repositories {
    maven { url 'http://repo1.maven.org/maven2' }
 }
 dependencies {
    classpath 'com.android.tools.build:gradle:0.4'
 }
}
apply plugin: 'android'

repositories {
 mavenCentral()
 maven {
    url 'https://oss.sonatype.org/content/repositories/snapshots/'
 }
}

configurations {
 compile
 androidannotations.extendsFrom(compile)
}

dependencies {
 compile files('libs/android-support-v4.jar')
 compile 'org.androidannotations:androidannotations-api:3.0-SNAPSHOT'
 androidannotations 'org.androidannotations:androidannotations:3.0-SNAPSHOT'
}

android {
 compileSdkVersion 17
 buildToolsVersion "17.0.0"

 defaultConfig {
    minSdkVersion 7
    targetSdkVersion 16
    packageName "org.labaa.aa"
    testPackageName "org.labaa.aa.test"
    testInstrumentationRunner "org.labaa.aa.test.Runner"

 }
}

afterEvaluate { project ->
 android.applicationVariants.each { variant ->
    variant.javaCompile.options.compilerArgs += [
            '-classpath', configurations.compile.asPath,
            '-processorpath', configurations.androidannotations.asPath,
            '-processor', 'org.androidannotations.AndroidAnnotationProcessor',
            '-AandroidManifestFile=' + variant.processResources.manifestFile
    ]
 }
}

这不会更新 Android Studio 依赖项。手动添加 androidannotations-api。从命令行运行构建 gradle installDebug

从 Andoid Studio 编译时禁用应用启动,否则启动器会抱怨缺少带注释的活动。

【讨论】:

  • 似乎在 0.4 android.buildVariants 被更改为 android.applicationVariants。使用它我可以很好地构建项目,但我仍然没有生成任何东西。
  • 嗨,谢谢@robotoaster,这段代码在命令行的 gradle 中工作。但是我不能在 Android Studio 中使用带下划线的类,因为它们是生成到一个目录中的,Android Studio 不知道。你有什么帮助吗?
  • 在 Android Studio 打开项目结构并在模块源中选择 /build/classes/debug/ 作为源。
  • 使用 gradle 0.5.0 运行我得到 Annotation processor 'org.androidannotations.AndroidAnnotationProcessor' not found
  • 你是从命令提示符“gradle assembleDebug”还是类似的命令提示符下运行它?
【解决方案4】:

该项目现在有 up to date documentation 用于这种情况。 full example project 也存在。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        // replace with the current version of the Android plugin
        classpath 'com.android.tools.build:gradle:0.7.3+'
        // the latest version of the android-apt plugin
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
    }
}

apply plugin: 'android'
apply plugin: 'android-apt'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile

        // If you're using Android NBS flavors you should use the following line instead of hard-coded packageName
        //resourcePackageName android.defaultConfig.packageName
        resourcePackageName "com.example.your.package.name"

        // You can set optional annotation processing options here, like these commented options:
        // logLevel 'INFO'
        // logFile '/var/log/aa.log'
    }
}

dependencies {
    apt "org.androidannotations:androidannotations:3.0+"
    compile "org.androidannotations:androidannotations-api:3.0+"
}

【讨论】:

  • 他们刚刚恢复了我对 wiki 所做的更改。我想弄清楚我做错了什么。
【解决方案5】:

这是对执行以下操作的答案的更新:

  • 适用于 Android Studio 0.1.9
  • 从 IDE 中删除语法错误(即使在重建时)
  • 使用没有匕首的android注解

为了实现这一点,我将输出复制到 build/source/r 女巫将在重建期间保持标记为源。

import groovy.io.FileType

buildscript {
}

apply plugin: 'android'

repositories {
    mavenCentral()
}

ext.androidAnnotationsVersion = '2.7.1';

configurations {
    apt
}

dependencies {
    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 16
    }
}

android.applicationVariants.each { variant ->
    aptOutput = file("${project.buildDir}/source/r/${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()

        aptOutput.eachFileRecurse FileType.FILES, {
            if (it.name.equals('R.java')) {
                return
            }
            it.delete()
        }

        variant.javaCompile.options.compilerArgs += [
                '-processorpath', configurations.apt.getAsPath(),
                '-AandroidManifestFile=' + variant.processResources.manifestFile,
                '-s', aptOutput
        ]
    }
}

欢迎提出非常老套的改进建议

编辑 android-apt 方式

最近发布了一个名为 android-apt https://bitbucket.org/hvisser/android-apt/overview 的包,它使事情变得更容易(1.1 版支持 android 注释需要)

像这样简单地更新你的 build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "com.neenbedankt.gradle.plugins:android-apt:1.1"
    }
}

apply plugin: 'android-apt'

apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile
    }
}

dependencies {
    apt 'com.googlecode.androidannotations:androidannotations:2.7.+'
}

非常感谢 android-apt 的创建者 hvisser

【讨论】:

  • 是的,有点老套,但它确实有效。我有一个奇怪的行为,它构建了两次。
  • @RomainPiel 试试gradle clean && gradle build 有时会有所帮助
【解决方案6】:

一个可能的问题是未找到 R 类。默认情况下,Android Studio 不会像 eclipse 那样将 R.java 放入 gen 目录。解决方案是进入 Project Settings -> Facets -> 为您的项目选择 Android facet -> Compiler 选项卡,然后将“R.java 和 Manifest.java 文件”从“Run process-resources Maven task before Make”更改为“由 IDE 生成”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多