对于Kotlin开发环境的配置,可查看 Kotlin配置AndroidAnnotations
对于app的module中使用AndroidAnnotations,配置使用都很简单,网上有很多文章。但是对于library中使用Androidannotations,需要使用resName指定资源或者使用butterknife生成R2
一:先说根build.gradle配置:AndroidAnnotations的maven仓库
// 此配置在新版中可能会放到settings.gradle中
allprojects {
repositories {
mavenCentral()
mavenLocal()
jcenter()
google()
//androidannotations仓库地址
maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
}
二:app的build.gradle配置:
def AAVersion = "4.9.0-SNAPSHOT"
//annotations配置
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation "org.androidannotations:androidannotations-api:$AAVersion"
annotationProcessor "org.androidannotations:rest-spring:$AAVersion"
implementation "org.androidannotations:rest-spring-api:$AAVersion"
implementation 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
后面三个是androidannotations网络请求的依赖,如果不使用网络请求功能可以只引入前两个。
三:使用
@EActivity(R.layout.activity_main)
public class MainActivity extends MyBaseActivity {
@ViewById
TextView tv_test;
@Bean
AndroidUtil util;
......
}
以上是在app的module中使用,如果需要在library中使用,需要配合butterknife。还需要增加如下配置:
一、在根build.gradle增加butterknife依赖
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
//butterknife用于生成R2,用于在library中使用androidannotations
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
.....
}
二、在app的build.gradle引入butterknife依赖
apply plugin: 'com.jakewharton.butterknife'
指定使用R2
android {
......
defaultConfig {
......
javaCompileOptions {
annotationProcessorOptions {
arguments = [
'resourcePackageName': "com.base.library",
// 防止找不到manifest文件
"androidManifestFile": "$projectDir/src/main/AndroidManifest.xml".toString(),
"useR2" : "true"//用于在library中使用androidannotations
]
}
}
}
......
}
三、在library中使用
@EActivity(R2.layout.activity_main)
public class MyBaseActivity extends AppCompatActivity {
@ViewById
TextView tv_test;
......
}
完整配置代码:
根build.gradle
buildscript {
ext.kotlin_version = "1.6.10"
repositories {
maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
}
maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/repositories/google'
}
maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin'
}
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
butterknife用于生成R2,用于在library中使用androidannotations
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
// 此配置在新版中可能会放到settings.gradle中
allprojects {
repositories {
mavenCentral()
mavenLocal()
jcenter()
google()
//androidannotations仓库地址
maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app的build.gradle:
plugins {
id 'com.android.application'
}
def AAVersion = "4.9.0-SNAPSHOT"
android {
......
}
dependencies {
//annotations配置
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation "org.androidannotations:androidannotations-api:$AAVersion"
annotationProcessor "org.androidannotations:rest-spring:$AAVersion"
implementation "org.androidannotations:rest-spring-api:$AAVersion"
implementation 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
......
}
library中的build.gradle:
apply plugin: 'com.android.library'
// butterknife用于生成R2类,防止aa在library中无法使用
apply plugin: 'com.jakewharton.butterknife'
def AAVersion = "4.9.0-SNAPSHOT"
android {
......
defaultConfig {
......
javaCompileOptions {
annotationProcessorOptions {
arguments = [
'resourcePackageName': "com.base.library",
// 防止找不到manifest文件
"androidManifestFile": "$projectDir/src/main/AndroidManifest.xml".toString(),
"useR2" : "true"//用于在library中使用androidannotations
]
}
}
}
......
}
dependencies {
//annotations配置
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation "org.androidannotations:androidannotations-api:$AAVersion"
annotationProcessor "org.androidannotations:rest-spring:$AAVersion"
implementation "org.androidannotations:rest-spring-api:$AAVersion"
implementation 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
......
}
在app的module中使用:
@EActivity(R.layout.activity_main)
public class MainActivity extends MyBaseActivity {
@ViewById
TextView tv_test;
@Bean
AndroidUtil util;
......
}
在library中的module使用,需要用R2来指定资源:
@EActivity(R2.layout.activity_main)
public class MyBaseActivity extends AppCompatActivity {
@ViewById
TextView tv_test;
......
}