【Kotlin】Kotlin 中使用 ButterKnife ( 仅用于适配 Kotlin 语言 | 不推荐新项目使用 )

简介: 【Kotlin】Kotlin 中使用 ButterKnife ( 仅用于适配 Kotlin 语言 | 不推荐新项目使用 )

文章目录

I . 特别注意 : ButterKnife 已停止维护 ( 新项目禁止使用该框架 )

II . Android Studio 中配置 Kotlin 和 ButterKnife 步骤

III . Android Studio 中配置 Kotlin 和 ButterKnife 示例

IV . Kotlin 注解错误使用

V . 错误处理 导入库冲突 ( 与 androidx 冲突 )



I . 特别注意 : ButterKnife 已停止维护 ( 新项目禁止使用该框架 )


1 . 情况说明 : ButterKnife 已经停止维护 , 新项目直接使用 视图绑定 , 数据绑定 进行开发 , 本篇博客只是为了适配老版本项目 ;



2 . 当前需求 : 目前的需求是保证之前的 Java 代码能平稳运行 , 基本框架不变 , 在 Kotlin 中使用 ButterKnife 进行视图绑定操作 ;




II . Android Studio 中配置 Kotlin 和 ButterKnife 步骤


1 . Kotlin 配置 : 不再详细说明 , 创建项目时 , 选择支持 Kotlin 即可 ;



2 . ButterKnife 配置 : ButterKnife 只需要在 Module 下的 build.gradle 构建脚本中配置 ,



① 配置依赖库 : 在 Module 下的 build.gradle 的 dependencies 中进行如下配置 ;


/** androidx 依赖与老版本的 butterknife 冲突 */
implementation 'com.jakewharton:butterknife:10.0.0'
kapt 'com.jakewharton:butterknife-compiler:10.0.0'


② 应用插件 : 在 Module 下的 build.gradle 顶部添加如下配置 ;


apply plugin: 'kotlin-kapt'



3 . 注解使用 :



① 组件定义 : 这里注意 , 只能使用下面的两种方式 , 其它方式会报错 ;


@BindView(R.id.tv_1)
lateinit var tv_1 : TextView
@JvmField
@BindView(R.id.tv_2)
var tv_2 : TextView? = null


② 视图绑定 : 使用 ButterKnife.bind(this) 绑定定义的组件 , 与 Java 中操作一样 ;


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    ButterKnife.bind(this)
}



III . Android Studio 中配置 Kotlin 和 ButterKnife 示例


GitHub 示例 : https://github.com/han1202012/Kotlin_ButterKnife



1 . 工程下的 build.gradle 构建脚本 :


// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.3.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        google()
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}



2 . Module 下的 build.gradle 脚本 :


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "kim.hsl.kb"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    /** androidx 依赖与老版本的 butterknife 冲突 */
    implementation 'com.jakewharton:butterknife:10.0.0'
    kapt 'com.jakewharton:butterknife-compiler:10.0.0'
}



3 . Kotlin 代码的 Activity 中使用 ButterKnife 注解 : 注意只能使用下面的两种方式 ;


package kim.hsl.kb
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
class MainActivity : Activity() {
    @BindView(R.id.tv_1)
    lateinit var tv_1 : TextView
    @JvmField
    @BindView(R.id.tv_2)
    var tv_2 : TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ButterKnife.bind(this)
        Log.i("TAG", tv_1?.text.toString())
        Log.i("TAG", tv_2?.text.toString())
    }
}



IV . Kotlin 注解错误使用


1 . 报错内容 :


@BindView fields must not be private or static. (kim.hsl.kb.MainActivity.tv_2)
    private android.widget.TextView tv_2;


2 . 报错原因 : 使用了如下声明方式 , @BindView(R.id.tv_2) var tv_2 : TextView? = null ;



3 . 只能使用下面两种声明方式 : ① 使用 lateinit 修饰变量 , ② 使用 @JvmField 注解 ;


@BindView(R.id.tv_1)
lateinit var tv_1 : TextView
@JvmField
@BindView(R.id.tv_2)
var tv_2 : TextView? = null



V . 错误处理 导入库冲突 ( 与 androidx 冲突 )


1 . 报错信息如下 :


The given artifact contains a string literal with a package reference 'android.support.v4.content' 
that cannot be safely rewritten. Libraries using reflection such as annotation processors need to 
be updated manually to add support for androidx.


2 . 报错原因 : 引入了 androidx 包依赖 , 与低版本的 butterknife 产生了冲突 , 二者不能同时使用 ;


Static interface methods are only supported starting with Android N (--min-api 24): 
void butterknife.Unbinder.lambda$static$0()

3 . 总结 : 坑有点多 , 新应用能不用 ButterKnife 就不用 , 10.0.0 版本的 butterknife 必须要求 android-24 以上最低兼容版本 , 对于商业项目来说 , 这是不可接受的 ;



4 . 推荐用法 : 老版本应用 ( 没有使用 androidx ) 继续使用老版本的 ButterKnife , 新版本的应用就别用这个框架了 , 使用 JetPack 中的 视图 / 数据 绑定 ;



① 老项目 : 没有使用 androidx 依赖 , 可以使用低版本的 ButterKnife , 这也是唯一的途径了 ;


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    /** androidx 依赖与老版本的 butterknife 冲突 */
    implementation 'com.jakewharton:butterknife:8.8.1'
    kapt 'com.jakewharton:butterknife-compiler:8.8.1'
}



② 新项目 : 如果使用了 androidx 依赖 , 必须使用高版本的 ButterKnife , 只能兼容 24 以上的最小版本 ; ( 商业项目用了就废了 )


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    /** androidx 依赖与老版本的 butterknife 冲突 */
    implementation 'com.jakewharton:butterknife:10.0.0'
    kapt 'com.jakewharton:butterknife-compiler:10.0.0'
}



目前使用了 ButterKnife 的应用 , 无法迁移到 JetPack ;



GitHub 示例 : https://github.com/han1202012/Kotlin_ButterKnife


目录
相关文章
|
13天前
|
Java Android开发 C++
Kotlin vs Java:选择最佳语言进行安卓开发
【4月更文挑战第13天】Java曾是安卓开发的主流语言,但Kotlin的崛起改变了这一局面。Google在2017年支持Kotlin,引发两者优劣讨论。Java以其成熟稳定、强大生态和跨平台能力占优,但代码冗长、开发效率低和语言特性过时是短板。Kotlin则以简洁语法、空安全设计和高度兼容Java脱颖而出,但社区和生态系统仍在发展中,可能存在学习曲线和性能问题。选择语言应考虑项目需求、团队熟悉度、维护性、性能和生态系统。无论选择哪种,理解其差异并适应新技术至关重要。
|
10月前
|
XML 安全 Java
使用Kotlin构建Android应用:现代化的开发语言
随着移动应用开发的不断发展,开发人员有了更多选择来构建功能强大、高效和可维护的Android应用程序。其中一种备受推崇的选择就是使用Kotlin作为开发语言。Kotlin是一种现代化的编程语言,它为Android开发带来了许多优势和便利。
107 0
|
11月前
|
算法 安全 Java
00. Kotlin 安装和语言的基本组成
00. Kotlin 安装和语言的基本组成
199 0
|
SQL 存储 NoSQL
JVM 上数据处理语言的竞争:Kotlin, Scala 和 SPL
JVM 上数据处理语言的竞争:Kotlin, Scala 和 SPL
198 0
|
SQL 存储 NoSQL
JVM 上数据处理语言的竞争:Kotlin, Scala 和 SPL
基于JVM的开源数据处理语言主要有Kotlin、Scala、SPL,下面对三者进行多方面的横向比较,从中找出开发效率最高的数据处理语言。本文的适用场景设定为项目开发中常见的数据处理和业务逻辑,以结构化数据为主,大数据和高性能不作为重点,也不涉及消息流、科学计算等特殊场景。......
192 0
JVM 上数据处理语言的竞争:Kotlin, Scala 和 SPL
|
SQL 安全 Java
Java 近期新闻:Loom 和 Panama 项目相关 JEP、JobRunr 5.1.0、Kotlin 1.7.0 预览
Java 近期新闻综述,内容主要涉及 OpenJDK、JDK 19 相关 JEP、JobRunr 5.1.0、Quarkus 2.8.3.Final、Hibernate ORM 6.0.1.Final、Kotlin 1.7.0 预览、 Apache Camel 3.14.3 和 3.11.7 版本、Apache Tika 2.4.0 和 1.28.2 版本、Micronaut 最小 JDK 版本调查和 JFokus 2022。 OpenJDK
290 0
|
SQL Java 关系型数据库
在 Kotlin 中使用 WebFlux + R2DBC 开发 Web 项目
在 Kotlin 中使用 WebFlux + R2DBC 开发 Web 项目
240 0
在 Kotlin 中使用 WebFlux + R2DBC 开发 Web 项目
DHL
|
存储 算法 安全
如何在项目中封装 Kotlin + Android Databinding
如何在项目中封装 Kotlin + Android Databinding
DHL
278 0
如何在项目中封装 Kotlin + Android Databinding
|
Java Android开发 C++
【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )(二)
【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )(二)
172 0
【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )(二)
|
Java Android开发 C++
【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )(一)
【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )(一)
317 0
【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )(一)