【问题标题】:Android setting AndroidAnnotations using gradle at AndroidStudio not show layout在 Android Studio 中使用 gradle 设置 AndroidAnnotations 不显示布局
【发布时间】:2015-06-27 02:41:41
【问题描述】:

我在 android studio 中学习 gradle 和 AndroidAnnotations 框架。 但是我一开始就遇到了问题...= = 然后我使用 android studio 创建了一个新项目,并添加了关于 AndroidAnnotations 的依赖项。

我的 app.gradle 如下:

 apply plugin: 'com.android.application'
 android {
     compileSdkVersion 22
     buildToolsVersion "22.0.1"
     defaultConfig {
         applicationId "yu.idv.androidannotationtest"
         minSdkVersion 15
         targetSdkVersion 22
         versionCode 1
         versionName "1.0"
     }
     buildTypes {
         release {
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
         }
     }
 }
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:22.2.0'
     compile 'org.androidannotations:androidannotations-api:3.3.1'
 }

然后我把Activity上面的common注解如下:

 import org.androidannotations.annotations.EActivity;

 @EActivity(R.layout.activity_main)
 public class MainActivity extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 //        setContentView(R.layout.activity_main);
     }
 

但是当我编译我的程序时,我看不到所需的布局。显示的布局是 XML 文件中带有“Hello world”TextView 的默认布局。

我的 gradle 或 AndroidStudio 中的其他设置发生了什么?

非常感谢。

【问题讨论】:

  • 删除 setContentView 调用,@EActivity 将生成它。还有一个提示:不要依赖 Gradle 中的本地 jar,使用适当的依赖管理。

标签: android gradle android-annotations


【解决方案1】:

您还必须将处理器添加为 apt 依赖项和处理参数,该参数使用 android-apt 插件告诉 AA 清单的位置。另外不要忘记将清单更改为指向生成的活动(下划线结尾)。

查看 Gradle 说明:https://github.com/excilys/androidannotations/wiki/Building-Project-Gradle

或者从示例项目开始:https://github.com/excilys/androidannotations/tree/develop/examples/gradle

所以基本上也将这些添加到您的脚本中:

buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apply plugin: 'android-apt'
def AAVersion = '3.3.1'

dependencies {
    compile 'com.android.support:appcompat-v7:22.2.0'
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}

【讨论】:

  • 您好,我正在按照您的指导实施,但在 AS - 3.3.1、gradle - 3.0.1 错误android-apt plugin is incompatible with the Android Gradle plugin use Please use 'annotationProcessor' configuration instead 中出现此错误。我已经在 AS 的默认设置中配置了 AP,但它仍然给出了与注释相关的错误。任何帮助将不胜感激 PS:我完全是 android 的新手,只了解 java :(
【解决方案2】:
dependencies {
    def aaVersion = "4.7.0"

    annotationProcessor "org.androidannotations:androidannotations:${aaVersion}"
    implementation "org.androidannotations:androidannotations-api:${aaVersion}"
} 

如果您使用的是implementation,则annotationProcessor 有效,而不是apt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2013-05-10
    • 2016-04-02
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多