迁移废弃的Kotlin Android Extensions插件

本文介绍了如何在Kotlin中移除不再需要的`kotlin-android-extensions`插件,转而使用`viewBinding`功能进行视图绑定。同时,针对`Parcelable`序列化,讲解了如何从`kotlin-android-extensions`迁移到`kotlin-parcelize`插件,并指出`kotlin-parcelize`自1.4.20-M2版本起才可用。确保你的Android Studio、Gradle及Gradle Plugin版本符合要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、视图绑定

删除:

apply plugin: 'kotlin-android-extensions'

使用:

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

Tips:

  1. viewBinding 不同于 DataBinding
  2. 如果要配置 buildFeatures{} , 必须使用 4.1 以上的 Android Studio 版本 , 这是支持 buildFeatures 的最低版本 ;
  • AS:4.1+
  • Gradle:6.6.1+
  • Gradle Plugin:4.1.0+

2、Parcelable 序列化

Kotlin中的Parcelize功能是kotlin-android-extensions编译器插件的一部分,所以如果你的Parcelable类依赖于Parcelize注解,那么移除该插件将使它们无法编译。

JetBrains将Kotlin Android Extensions中的Parcelize提取到一个新的插件kotlin-parcelize中。

你需要在你的模块中添加kotlin-parcelize插件:

plugins {
    ..
    id 'kotlin-parcelize'
}

注意:kotlin-parcelize插件只能 kotlin1.4.20-M2版本开始使用。

参考:
https://weilu.blog.youkuaiyun.com/article/details/109557820
https://blog.youkuaiyun.com/shulianghan/article/details/109046085

安卓报错Could not resolve all files for configuration ':gst_doctor:debugRuntimeClasspath'. > Failed to transform kotlin-android-extensions-1.8.20.jar (org.jetbrains.kotlin:kotlin-android-extensions:1.8.20) to match attributes {artifactType=enumerated-runtime-classes, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}. > Execution failed for JetifyTransform: C:\Users\admin\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-android-extensions\1.8.20\6f25b7cf95167fbf487125a71758de4c0aa15da2\kotlin-android-extensions-1.8.20.jar. > Failed to transform 'C:\Users\admin\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-android-extensions\1.8.20\6f25b7cf95167fbf487125a71758de4c0aa15da2\kotlin-android-extensions-1.8.20.jar' using Jetifier. Reason: AmbiguousStringJetifierException, message: The given artifact contains a string literal with a package reference 'android.support.v4' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.. (Run with --stacktrace for more details.) This is a known exception, and Jetifier won't be able to jetify this library. Suggestions: - If you believe this library doesn't need to be jetified (e.g., if it already supports AndroidX, or if it doesn't use support libraries/AndroidX at all), add android.jetifier.blacklist = {comma-separated list of regular expressions (or simply names) of the libraries that you don't want to be jetified} to the gradle.properties file. - If you believe this library needs to be jetified (e.g., if it uses old support libraries and breaks your app if it isn't jetified), contact the library's authors to update this library to support AndroidX and use the supported version once it is released.
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值