AndroidStudio2021版,Gradle7.0+,Kotlin使用AndroidAnnotations4.9.0最新配置,需配合butterknife使用

随着Studio和Gradle版本更新,包括AndroidAnnotations自己的更新,配置AndroidAnnotations方法一直在变。

目前使用Studio2021版+Gradle7.0以上,AndroidAnnotations4.9.0使用原来的配置,在Kotlin开发环境下会出问题,需要配合butterknife才能正常使用。

最后有完整代码。

一:先说根build.gradle配置:butterknife依赖和AndroidAnnotations的maven仓库

dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
        //butterknife用于生成R2,配合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'
        }
    }
}

二:app的build.gradle配置:

使用kotlin的kapt配置,用于引入androidannotations依赖,使用annotationProcessor会失败

apply plugin: "kotlin-kapt"

加入butterknife依赖

// butterknife用于生成R2类,配合androidannotations
apply plugin: 'com.jakewharton.butterknife'

def AAVersion = "4.9.0-SNAPSHOT"

androidannotations依赖

kapt "org.androidannotations:androidannotations:$AAVersion"
implementation "org.androidannotations:androidannotations-api:$AAVersion"
kapt "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网络请求的依赖,如果不使用网络请求功能可以只引入前两个。

最后是kapt开启R2:

// 开启r2,用于配合androidannotations
kapt {
    arguments {
        arg("resourcePackageName", android.defaultConfig.applicationId)
        arg("useR2", true)
    }
    correctErrorTypes = true
}

三:使用,使用资源需要用R2引入

@EActivity(R2.layout.activity_main)
open class TestActivity :Activity() {
    @ViewById
    lateinit var tv_test : TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    @AfterViews
    fun init(){
        tv_test.setText("111111111112222222")
    }

}

完整代码:

根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,配合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()
        maven {
            url = 'https://oss.sonatype.org/content/repositories/snapshots'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app的build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}
apply plugin: "kotlin-android"
apply plugin: "kotlin-allopen"
apply plugin: "kotlin-kapt"
// butterknife用于生成R2类,配合androidannotations
apply plugin: 'com.jakewharton.butterknife'

def AAVersion = "4.9.0-SNAPSHOT"

android {
    ......
}

dependencies {
    //annotations配置
    kapt "org.androidannotations:androidannotations:$AAVersion"
    implementation "org.androidannotations:androidannotations-api:$AAVersion"
    kapt "org.androidannotations:rest-spring:$AAVersion"
    implementation "org.androidannotations:rest-spring-api:$AAVersion"
    implementation 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
    
    ........
}

// 开启r2,用于配合androidannotations
kapt {
    arguments {
        arg("resourcePackageName", android.defaultConfig.applicationId)
        arg("useR2", true)
    }
    correctErrorTypes = true
}

activity的使用:

package com.fs.kotlin

import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import org.androidannotations.annotations.AfterViews
import org.androidannotations.annotations.EActivity
import org.androidannotations.annotations.ViewById

@EActivity(R2.layout.activity_main)
open class TestActivity :Activity() {
    @ViewById
    lateinit var tv_test : TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    @AfterViews
    fun init(){
        tv_test.setText("111111111112222222")
    }

}

java开发环境下的配置,下一篇再说吧。

你可能感兴趣的:(android,kotlin)