日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/6/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 29buildToolsVersion "29.0.2"defaultConfig {applicationId "kim.hsl.kb"minSdkVersion 24targetSdkVersion 29versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles 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.kbimport android.app.Activity import android.os.Bundle import android.util.Log import android.widget.TextView import butterknife.BindView import butterknife.ButterKnifeclass MainActivity : Activity() {@BindView(R.id.tv_1)lateinit var tv_1 : TextView@JvmField@BindView(R.id.tv_2)var tv_2 : TextView? = nulloverride 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


《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的【Kotlin】Kotlin 中使用 ButterKnife ( 仅用于适配 Kotlin 语言 | 不推荐新项目使用 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。