Kotlin Android 扩展在库类型模块中不起作用

Kotlin Android Extensions does not work in a library type module

我正在尝试实现 kotlin android 扩展以使用 @Parcelize 注释,如果我在应用程序的模块 ('com.android.application') 中实现它,它可以正常工作,但是当我尝试在库类型模块中使用它 ('com.android.library') 它对我不起作用。

我的gradle配置如下:

Build.gradle // 应用

apply plugin: 'com.android.application'
apply plugin: 'jacoco'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs'

allOpen {
  annotation 'com.juanlondono.app.soldi.testing.OpenClass'
}

androidExtensions {
  experimental = true
}

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])

  // ------------------------- KOTLIN ------------------------- //
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11"

 ... // Other Dependencies

}

Build.gradle // 图书馆

    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'

    dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
    
      // ------------------------- KOTLIN ------------------------- //
      implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11"
   
    ... // Other Dependencies
    }

为了配置 kotlin android 扩展,我将自己置于以下指南中:Kotlinlang

完成之前的配置后,我尝试实现 @Parcelize 注释,但它不起作用我收到 未解析引用 Parcelize 的消息。

添加

androidExtensions {
   experimental = true
} 

在库的gradle处,出现如下错误:

Cannot invoke method get() on null object

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:prodDebugRuntimeClasspath'.
   > A problem occurred configuring project ':model'.
      > Failed to notify dependency resolution listener.
         > Cannot invoke method get() on null object
         > Cannot invoke method get() on null object
         > Cannot invoke method get() on null object
         > Cannot invoke method get() on null object

目前,Parcelable 的实现是手动完成的。

更新

最近弃用了 kotlin 扩展,取而代之的是 kotlin-parcelize 以使项目正常工作必须更改以下内容:

第 1 步:更新到最新的 kotlin 版本 (1.4.20) 或更高版本

第 2 步: 替换 apply plugin: 'kotlin-android-extensions' 有了这个 apply plugin: 'kotlin-parcelize'

步骤 2: 从 android {}

中删除此代码
androidExtensions {
    experimental = true
}

步骤 3: 替换旧导入 import kotlinx.android.parcel.Parcelize 新导入 import kotlinx.parcelize.Parcelize

我刚刚排练了 Kotlin 1.3.50 的版本,它可以在以下配置下正常工作:

项目必须是 com.android.applicationcom.android.library 类型。就我而言,我使用的是 android.library.

类型的项目

build.gradle(图书馆)

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
  ...
}

androidExtensions {
  experimental = true
}

dependencies {
  ...
}

型号Class

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Test(val prop: String) : Parcelable