Search code examples
androidgradlekotlinkotlin-android-extensions

Kotlin Android Extensions does not work in a library type module


I'm trying to implement kotlin android extensions to use the @Parcelize annotation, if I implement it in the application's module ('com.android.application') it works without problems, but when I try to use it in a library-type module ('com.android.library') it does not work for me.

The configuration of my gradle is the following:

Build.gradle // app

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 // library

    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
    }

To make the configuration of kotlin android extensions I base myself in the following guide: Kotlinlang

After making the previous configuration I try to implement the @Parcelize annotation and it does not work I get the message of unresolved reference Parcelize.

adding

androidExtensions {
   experimental = true
} 

At the gradle of the library, the following error occurs:

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

For now, the implementation of Parcelable is done manually.

UPDATE

recently kotlin extension is deprecated in favor of kotlin-parcelize to make the project work the following must be changed:

STEP 1: Update to latest kotlin version(1.4.20) or newer

STEP 2: replace apply plugin: 'kotlin-android-extensions' with this apply plugin: 'kotlin-parcelize'

Step 2: Remove this code from the android {}

androidExtensions {
    experimental = true
}

Step 3: Replace old import import kotlinx.android.parcel.Parcelize with new import import kotlinx.parcelize.Parcelize


Solution

  • I just rehearsed with the version of Kotlin 1.3.50 and it works correctly with the following configuration:

    The project must be of type com.android.application orcom.android.library. In my case I am using a project of type android.library.

    build.gradle (library)

    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    android {
      ...
    }
    
    androidExtensions {
      experimental = true
    }
    
    dependencies {
      ...
    }
    

    Model Class

    import android.os.Parcelable
    import kotlinx.android.parcel.Parcelize
    
    @Parcelize
    data class Test(val prop: String) : Parcelable