Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

KT-22213 - Kotlin Android Extensions experimental mode doesn't work #644

Closed
nobodysfault opened this issue Dec 28, 2017 · 34 comments
Closed

Comments

@nobodysfault
Copy link

nobodysfault commented Dec 28, 2017

After applying Kotlin Android Extensions plugin I wasn't able to enable its experimental mode.

Expected Behavior

After applying Kotlin/Android plugins in build.gradle (Groovy version):

apply plugin: "com.android.application"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"

we could enable Kotlin Android Extensions experimental mode:

androidExtensions {
    experimental = true
}

./gradle dependencies shows that there is a org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.2.10 runtime dependency.

Current Behavior

After applying plugins in build.gradle.kts:

plugins {
    id("com.android.application")
    kotlin("android")
    id("kotlin-android-extensions")
}
androidExtensions {
    isExperimental = true
}

experimental mode actually is not enabled e.g. I wasn't able to import kotlinx.android.extensions.LayoutContainer class and there is no org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.2.10 runtime dependency

Steps to Reproduce

build.gradle.kts:

plugins {
    id("com.android.application")
    kotlin("android")
    id("kotlin-android-extensions")
}
androidExtensions {
    isExperimental = true
}

After that try to import any experimental Kotlin Android Extensions classes e.g. kotlinx.android.extensions.LayoutContainer

Update: I've created a sample project demonstrating the issue: https://github.com/nobodysfault/hello-android
It's just a cloned hello-android sample with added Kotlin Android Extensions and experimental = true.

Vendor issue

https://youtrack.jetbrains.com/issue/KT-22213

@mkobit
Copy link
Contributor

mkobit commented Dec 28, 2017

Does your script compile on the command line? I would expect some sort of failure messages to pop up regarding plugin resolution.

One issue may be that the plugin id("com.android.application") isn't resolving to the correct plugin for - see this example https://github.com/gradle/kotlin-dsl/blob/4719d5c5af97a826335f97f9fdba4d8d43601a91/samples/hello-android/settings.gradle.kts#L1-L13

Other issue may be the wrong Id for id("kotlin-android-extensions") - should probably be kotlin("kotlin-android-extensions") or id("org.jetbrains.kotlin.android.extensions").

Another issue may be you haven't specified versions in any of your plugins like id("com.android.application") version "3.0.0" - see this example https://github.com/gradle/kotlin-dsl/blob/master/samples/hello-android/build.gradle.kts#L2

@nobodysfault
Copy link
Author

@mkobit yes, it compiles. Basic Kotlin Android Extensions functionality works (like synthetic package etc.), but not the experimental one. It looks like this experimental flag just isn't resolved somehow.
I'll try other things you've suggested, thanks!

@nobodysfault
Copy link
Author

@mkobit I wasn't able to specify plugin version:

Error resolving plugin [id: 'org.jetbrains.kotlin.android.extensions', version: '1.2.10']
> Plugin request for plugin already on the classpath must not include a version

and I'm sure I don't have this plugin on the classpath already.

As for com.android.application - yes, it resolves correctly.

@StefMa
Copy link
Contributor

StefMa commented Dec 29, 2017 via email

@nobodysfault
Copy link
Author

@StefMa basically it's just

include ':project'

but I've tried custom resolution strategy like this:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "com.android.application") {
                useModule("com.android.tools.build:gradle:${requested.version}")
            }
            if (requested.id.id == "kotlin-android-extensions") {
                useModule("org.jetbrains.com:kotlin-android-extensions:${requested.version}")
            }
        }
    }
}

even with custom overridden versions, but no luck

@iNoles
Copy link

iNoles commented Dec 29, 2017

kotlin("android.extensions") version "1.2.10"

@nobodysfault
Copy link
Author

nobodysfault commented Jan 2, 2018

@iNoles build script compiles, but with isExperimental = true kotlin-android-extensions-runtime is still not on the classpath.

I've cloned hello-android sample and added Kotlin Android Extensions:
https://github.com/nobodysfault/hello-android
I've added kotlinx.android.extensions.LayoutContainer import into MainActivity.kt to check if experimental mode works. Hope it helps.

@StefMa
Copy link
Contributor

StefMa commented Jan 2, 2018 via email

@nobodysfault
Copy link
Author

@StefMa it should log this error: "'kotlin-android' plugin should be enabled before 'kotlin-android-extensions'", but no error is logged.
As you could see in https://github.com/nobodysfault/hello-android/blob/master/build.gradle.kts kotlin-android plugin is loaded before kotlin-android-extensions, but still no luck.

@nobodysfault
Copy link
Author

nobodysfault commented Jan 2, 2018

I've ran hello-android build with --debug, and it looks like this function works:
https://github.com/JetBrains/kotlin/blob/e2306ecf94d179074b4d0e2fc5098e039a22fa59/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/AndroidSubplugin.kt#L142
cause there are -P plugin:org.jetbrains.kotlin.android:experimental=true,plugin:org.jetbrains.kotlin.android:defaultCacheImplementation=hashMap in compiler args, but Android Extensions runtime is not added to the runtime dependencies.

For me it looks like a problem with the following code:

        extension.setEvaluatedHandler { evaluatedExtension ->
            if (evaluatedExtension.isExperimental) {
                addAndroidExtensionsRuntimeIfNeeded(project)
            }
        }

but I'm not sure how could I check it. Could it behave differently with the old Groovy build.gradle and kotlin-dsl?

@StefMa
Copy link
Contributor

StefMa commented Jan 2, 2018 via email

@eskatos
Copy link
Member

eskatos commented Jan 10, 2018

The way the kotlin-android plugin handle the isExperimental flag seems a bit convoluted to me.

First, it relies on org.gradle.util.Configurable which is not API, it is internal.
Moreover, a plugin implemented in, say, plain Java doing extensions.getByType(AndroidExtensionsExtension.class).experimental = true won't trigger this "evaluated handler" either.
It will only work in Groovy scripts.

@nobodysfault, could you report this issue on the Kotlin gradle plugin?

@nobodysfault
Copy link
Author

@eskatos thanks for the investigation and explanation. I've reported the issue: https://youtrack.jetbrains.com/issue/KT-22213

@toxxmeister
Copy link

I've been able to circumvent this by creating a Groovy Gradle script which just enables the experimental mode, and applying the said script in my Kotlin Gradle script.

experimentalExtensions.gradle:

androidExtensions { experimental = true }

build.gradle.kts:

// ...
apply { from("experimentalExtensions.gradle") }
// ...

@StefMa
Copy link
Contributor

StefMa commented Jan 17, 2018

Kotlin "released" 1.2.20 yesterday and according to their changelog they have fixed

  • KT-20235 Error, can't use plugin kotlin-android-extensions

I think you can give it another try with 1.2.20 ;)

@eskatos eskatos changed the title Kotlin Android Extensions experimental mode doesn't work KT-22213 - Kotlin Android Extensions experimental mode doesn't work Jan 17, 2018
@eskatos
Copy link
Member

eskatos commented Jan 17, 2018

Kotlin 1.2.20 Gradle plugins are not deployed to the plugin portal yet, latest available version is 1.2.10: https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm

#664 (comment)

@nobodysfault
Copy link
Author

@StefMa @eskatos Nope, it still doesn't work. I've updated Kotlin in my test project and checked it: nobodysfault/hello-android@c8f23a1
And KT-22213 is still in "Open" state.

@eskatos
Copy link
Member

eskatos commented Jan 17, 2018

Yep, KT-20235 is unrelated to this issue.

@bamboo bamboo added this to the 2.0.0 milestone Jan 19, 2018
@tadfisher
Copy link

Another workaround:

androidExtensions {
    configure(delegateClosureOf<AndroidExtensionsExtension> {
        isExperimental = true
    })
}

@rodrigolfonseca
Copy link

rodrigolfonseca commented Sep 18, 2018

Good night. In my case i only coment the apply plugin: 'kotlin-android' just like that:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android-extensions'

// apply plugin: 'kotlin-android'

androidExtensions {
experimental = true
}

and it works. Does it will cause any trouble to me?

@rodrigolfonseca
Copy link

Good morning i solved this issue changing the Sync project that was in off-line mode to on-line mode. to me that was my problem. Thanks guys

@eskatos eskatos removed this from the 2.0.0 milestone Nov 6, 2018
@hereisderek
Copy link

hereisderek commented Jan 15, 2019

@parcelize still not found

plugins {
    id("com.android.library")
    id("kotlin-android")
    id("kotlin-android-extensions")
    kotlin("android")
    kotlin("kapt")
    kotlin("android.extensions")
}
    
androidExtensions {
        isExperimental = true
        configure(delegateClosureOf<AndroidExtensionsExtension> {
            isExperimental = true
        })
    }

it recongnized kotlinx.android.parcel as a package name however it's empty

@lukaszkalnik
Copy link

@hereisderek you have some duplicate lines:
id("kotlin-android") is the same as kotlin("android"). And id("kotlin-android-extensions") is the same as kotlin("android.extensions").

@hereisderek
Copy link

@hereisderek you have some duplicate lines:
id("kotlin-android") is the same as kotlin("android"). And id("kotlin-android-extensions") is the same as kotlin("android.extensions").

I understand that. it was just me playing around and added the lines.
however it still doesn't explain why experimental is not enabled

@tekinalper
Copy link

Any updates about this issue?

@SimonSchubert
Copy link

The workaround is not longer needed for kotlin 1.3.30.

androidExtensions { isExperimental = true }

Works fine. Issue can be closed.

@eskatos
Copy link
Member

eskatos commented Apr 12, 2019

Thanks for the feedback @SimonSchubert!
Closing

@eskatos eskatos closed this as completed Apr 12, 2019
@JakubNeukirch
Copy link

I've looked into the Kotlin gradle plugin code to find out what happened under the hood. As you can see in this file (especially the class AndroidExtensionsSubpluginIndicator): https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/AndroidSubplugin.kt You have to apply the kotlin-android. Otherwise it just ignore "everything"... TL/DR: Just apply the kotlin-android plugin before the kotlin-android-extensions and it should work... 👍

On Jan 2, 2018 9:21 AM, "nobodysfault" @.***> wrote: @iNoles https://github.com/inoles build script compiles, but with isExperimental = true kotlin-android-extensions-runtime is still not on the classpath. I've cloned hello-android sample and added Kotlin Android Extensions here: https://github.com/nobodysfault/hell-android I've added kotlinx.android.extensions.LayoutContainer import into MainActivity.kt to check if experimental mode works. Hope it helps. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#644 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AJwYe4TXB9azRRSVhKbrL5ZKi5XOPvxMks5tGebugaJpZM4ROVsL .

It is worth mentioning that plugin order is important - it is what was causing this problem for me

@mtrakal
Copy link

mtrakal commented Apr 15, 2019

maybe related... https://youtrack.jetbrains.com/issue/KT-31043 app start crashing for our users after apply #644 (comment) and Kotlin 1.3.30

@hereisderek
Copy link

hereisderek commented Apr 23, 2019

androidExtensions { isExperimental = true } on kotlin 1.3.30

still doesn't work for me

Creating configuration testReleaseCompile
Creating configuration testReleasePublish
Creating configuration testReleaseProvided
Creating configuration testReleaseApi
Creating configuration testReleaseImplementation
Creating configuration testReleaseRuntimeOnly
Creating configuration testReleaseCompileOnly
Creating configuration testReleaseWearApp
Creating configuration testReleaseAnnotationProcessor
Parsing the SDK, no caching allowed
SDK initialized in 0 ms

FAILURE: Build failed with an exception.

* Where:
Build file '/Volumes/Persistence/Data/Workspace/frsipMobile-android/FrSipProject/virtualmeeting/build.gradle' line: 26

* What went wrong:
A problem occurred evaluating project ':virtualmeeting'.
> Could not set unknown property 'isExperimental' for object of type org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension.

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.

P.S.

syntax for gradle (not kotlin dsl) should be androidExtensions { experimental = true } and it's working

@steam0111
Copy link

Guys , good answer , work for me

Kotlin plugin should be enabled before 'kotlin-android-extensions'

https://stackoverflow.com/a/55803307

@galacticappster04
Copy link

galacticappster04 commented Feb 7, 2020

Please do not close, it still showing up on Android 3.5.3 Kotlin 1.3.61. I stilll can't get @parcelize to be found.

@VijayVilliers
Copy link

VijayVilliers commented Mar 14, 2020

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

android {
.........................
}

androidExtensions {
experimental = true
features = ["parcelize"]
}
@neonwarge04 try the above with kotlin 1.3.70. Its worked for me..

@langsmith
Copy link

The stackoverflow advice in @steam0111's comment above, worked for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests