【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
【Kotlin】Kotlin 中使用 ButterKnife ( 仅用于适配 Kotlin 语言 | 不推荐新项目使用 )相关推荐
- SRPG游戏开发(三十一)第八章 游戏中的数据 - 一 创建新项目(Create New Project)
返回总目录 第八章 游戏中的数据(Data in Game) 在之前的章节中,我们进行地图对象的生成,移动等操作. 这一章本来可以进行战斗的编写,不过数据缺失是一个问题. 所以这一章我们先来建立一些数 ...
- Kotlin 中Butterknife 和Android 中使用butterknife
因为不太熟悉kotlin 所以就想写一个混编的Android 的测试 既有kotlin 又有android 的原生 目的:想了解下Kotlin ,和使用下Kotin中传说中的语法糖,别以后遇到大神写 ...
- kotlin 循环_Kotlin控制流–否则,用于循环,同时,范围
kotlin 循环 In this tutorial, we'll be covering an important aspect of programming, namely Kotlin Cont ...
- gradle kotlin DSL中引用项目下libs文件夹的jar及在gradle配置启动主程序
gradle kotlin DSL中引用项目libs下的jar buildscript下引用方式 build.gradle.kts的dependencies下引用方式 开发语言为java且使用grad ...
- Kotlin开发中的一些Tips
如果你开始使用Kotlin,我个人的建议是多关注编译后字节码或是反编译后的java代码,这样你会发现更多的细节.单纯只学习语法会让你忽略一些细节,而这可能会是性能问题或bug的来源. 下面我举一些我在 ...
- 《Kotlin极简教程》第三章 Kotlin基本数据类型
正式上架:<Kotlin极简教程>Official on shelves: Kotlin Programming minimalist tutorial 京东JD:https://item ...
- kotlin半生对象_Kotlin单一对象,Kotlin伴侣对象
kotlin半生对象 In this tutorial, we'll look at how Kotlin deals with Singleton Pattern and static proper ...
- 《Kotlin极简教程》第1章 Kotlin简介
第1章 Kotlin简介 最新上架!!!< Kotlin极简教程> 陈光剑 (机械工业出版社) 可直接打开京东,淘宝,当当===> 搜索: Kotlin 极简教程 http://ww ...
- Kotlin:所有的一切还是从Hello Kotlin开始
Kotlin和Java也一样可以有多种的开发方式,我们可以根据自己的实际需求进行选择.如下: 1. 终端命令行进行Kotlin的编写.编译和运行,类似与Java中的javac编译.java运行: 2. ...
最新文章
- Java基础:异常机制
- Promise入门详解和基本用法 我来教你
- @Autowired使用
- python 安装easy_install和pip
- kubernetes1.8.4 安装指南 -- 9. calico
- C#使用 System.Net.Mail发送邮件功能
- myysql 不能远程访问的解决办法
- PP视频怎么关闭PP视频虚拟键盘显示
- OpenGL 的渲染流水线
- 【2019.09.01】2019南京网络赛
- linux 文件读写锁,linux下的简单文件读写锁的操作
- Wonderware Intouch 2014R2 SP1授权教程
- 各大物联网通信技术对比
- php体检管理系统,学生健康体检信息管理系统
- 人到中年,沉默寡言(深度好文)
- SQLSERVER大小写转换方法
- epub to mobi转换器
- nacos启动成功无法访问
- 用计算机用u盘怎么切换,u盘上的东西换个电脑就不见了怎么办啊
- Havok和Physx对比