6

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

8
  • 1
    What exactly does "it does not work for me" mean? What are your precise symptoms? Commented Jan 9, 2019 at 0:47
  • when I try to use the annotation @Parcelize it tells me unresolved reference Parcelize, and if I try to import manually, import kotlinx.android.parcel.Parcelize does not work either Commented Jan 9, 2019 at 0:53
  • 1
    Your library build.gradle file does not show the androidExtensions { experimental = true } closure. Do you have it? Commented Jan 9, 2019 at 1:05
  • when I add androidExtensions {experimental = true} to the gradle of the library, gradle not sync, Commented Jan 9, 2019 at 1:29
  • Kotlin version 1.1.4 or newer? Commented Jan 9, 2019 at 9:18

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.