【问题标题】:Kotlin Android Extensions does not work in a library type moduleKotlin Android Extensions 在库类型模块中不起作用
【发布时间】:2019-06-03 17:36:03
【问题描述】:

我正在尝试实现 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

【问题讨论】:

  • “它对我不起作用”到底是什么意思?您的具体症状是什么?
  • 当我尝试使用注解@Parcelize 时它告诉我未解析的引用 Parcelize,如果我尝试手动导入,import kotlinx.android.parcel.Parcelize 也不起作用
  • 您的库build.gradle 文件未显示androidExtensions { experimental = true } 闭包。你有吗?
  • 当我将androidExtensions {experimental = true}添加到库的gradle时,gradle不同步,
  • Kotlin 版本 1.1.4 或更高版本?

标签: android gradle kotlin kotlin-android-extensions


【解决方案1】:

我刚刚排练了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 {
  ...
}

模型类

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2020-04-14
    • 2019-08-03
    相关资源
    最近更新 更多