栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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开发环境下的配置,下一篇再说吧。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号