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

举报
韩曙亮 发表于 2022/01/11 00:11:02 2022/01/11
【摘要】 文章目录 I . 特别注意 : ButterKnife 已停止维护 ( 新项目禁止使用该框架 )II . Android Studio 中配置 Kotlin 和 ButterKnife 步骤III...



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'

  
 
  • 1
  • 2
  • 3

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

apply plugin: 'kotlin-kapt'

  
 
  • 1

3 . 注解使用 :


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

@BindView(R.id.tv_1)
lateinit var tv_1 : TextView

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5


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
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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'
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

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())
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27


IV . Kotlin 注解错误使用



1 . 报错内容 :

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

  
 
  • 1
  • 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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6


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.

  
 
  • 1
  • 2
  • 3

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

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

  
 
  • 1
  • 2

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'
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

② 新项目 : 如果使用了 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'
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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


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


文章来源: hanshuliang.blog.csdn.net,作者:韩曙亮,版权归原作者所有,如需转载,请联系作者。

原文链接:hanshuliang.blog.csdn.net/article/details/105518881

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。