Gradle配置:
Project: build.gradle
只需要在dependencies中加:
classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’
如:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
//下面这一行就是需要加的
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app: build.gradle**(看注释)**
apply plugin: 'com.android.application'
//下面这两行都要加
apply plugin: 'com.neenbedankt.android-apt'
def AAVersion = '3.0.1' //最新版本号
android {
compileSdkVersion 24
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.example.baiyuanwei.test"
minSdkVersion 18
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
//下面这两行也要加
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
//apt 也是要加的,注意有个“ ?“号
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
//com.example.baiyuanwei.test是你的包名
resourcePackageName "com.example.baiyuanwei.test"
}
}
坑1:
build会出现此异常;
原因:可能是你添加的依赖有问题;
我的解决方案:就在app:build.gradle中添加apt,如上;
注意:凡是annotations 注解过的自定义view、fragment、activity,在用的时候都要用annotations自动生成的替代类,即”……_” ;
如:TagHotUserShowsView,则其annotations自动生成的类就是TagHotUserShowsView_
踩过的坑:自定义view: TagHotUserShowsView ,此view用annotations注解过,而我用的时候,用的是TagHotUserShowsView,导致我@ViewById结果都是null。