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

时间:2019-01-09 00:41:52

标签: android gradle kotlin kotlin-android-extensions

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

在库中,发生以下错误:

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的实现是手动完成的。

1 个答案:

答案 0 :(得分:2)

我只是练习了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