Kotlin plugin should be enabled before kotlin android extensions

Содержание
  1. Kotlin Android Extensions deprecated. Что делать? Инструкция по миграции
  2. Кратко о Kotlin Android Extensions
  3. Проблемы Kotlin Android Extensions
  4. Альтернативные способы
  5. View Binding от Google
  6. Начало работы с View Binding
  7. Использование в Activity
  8. [Notifications 1.0.0] BUILD FAILURE: Kotlin plugin should be enabled before ‘kotlin-android-extensions’ #179
  9. Comments
  10. UsabilitySpace commented Aug 29, 2020 •
  11. EMULATOR AND OS:
  12. ISSUE DESCRIPTION:
  13. STEPS TO REPRODUCE:
  14. OTHER STEPS TO REPRODUCE:
  15. EXPECTED BEHAVIOUR:
  16. ACTUAL BEHAVIOUR:
  17. SCREENSHOTS:
  18. FLUTTER DOCTOR:
  19. THE AIM OF HANDLING THIS ISSUE:
  20. KT-22213 — Kotlin Android Extensions experimental mode doesn’t work #644
  21. Comments
  22. nobodysfault commented Dec 28, 2017 •
  23. Expected Behavior
  24. Current Behavior
  25. Steps to Reproduce
  26. Vendor issue
  27. mkobit commented Dec 28, 2017
  28. nobodysfault commented Dec 29, 2017
  29. nobodysfault commented Dec 29, 2017
  30. nobodysfault commented Dec 29, 2017
  31. iNoles commented Dec 29, 2017
  32. nobodysfault commented Jan 2, 2018 •
  33. nobodysfault commented Jan 2, 2018
  34. nobodysfault commented Jan 2, 2018 •
  35. eskatos commented Jan 10, 2018
  36. nobodysfault commented Jan 10, 2018
  37. toxxmeister commented Jan 10, 2018
  38. StefMa commented Jan 17, 2018
  39. eskatos commented Jan 17, 2018
  40. nobodysfault commented Jan 17, 2018
  41. eskatos commented Jan 17, 2018
  42. tadfisher commented Jun 19, 2018
  43. rodrigolfonseca commented Sep 18, 2018 •
  44. rodrigolfonseca commented Sep 18, 2018
  45. hereisderek commented Jan 15, 2019 •
  46. lukaszkalnik commented Jan 17, 2019
  47. hereisderek commented Jan 19, 2019
  48. tekinalper commented Mar 14, 2019
  49. SimonSchubert commented Apr 12, 2019
  50. eskatos commented Apr 12, 2019
  51. JakubNeukirch commented Apr 15, 2019
  52. mtrakal commented Apr 15, 2019
  53. hereisderek commented Apr 23, 2019 •
  54. steam0111 commented Jan 30, 2020
  55. neonwarge04 commented Feb 7, 2020 •
  56. VijayVilliers commented Mar 14, 2020 •
  57. langsmith commented Apr 11, 2020

Kotlin Android Extensions deprecated. Что делать? Инструкция по миграции

kotlinx.android.synthetic is no longer a recommended practice. Removing in favour of explicit findViewById

Кратко о Kotlin Android Extensions

Kotlin Android Extensions — это плагин для Kotlin, позволяющий восстанавливать view из Activities, Fragments, и Views без написания стандартного бойлерплэйт-кода типа findViewById.
Плагин генерирует дополнительный код, который позволяет получить доступ к view в виде XML, так же, как если бы вы имели дело с properties с именем id, который вы использовали при определении структуры.

Также он создаёт локальный кэш view. При первом использовании свойства, плагин выполнит стандартный findViewById. В последующем, view будет восстановлен из кэша, поэтому доступ к нему будет быстрее.

Если это всё так удобно, то зачем его сделали deprecated?

Проблемы Kotlin Android Extensions

Альтернативные способы

View Binding от Google

Итак, победителем в этом списке выглядит ViewBinding от Google (не путайте с DataBinding). Давайте кратко рассмотрим, что это такое.

View Binding — это инструмент, который позволяет проще писать код для взаимодействия с view. При включении View Binding в определенном модуле он генерирует binding классы для каждого файла разметки (layout) в модуле. Объект сгенерированного binding класса содержит ссылки на все view из файла разметки, для которых указан android:id

Главные преимущества View Binding — это Null safety и Type safety.

Начало работы с View Binding

Начать работать с ViewBinding достаточно просто. Нужно добавить опцию в build.gradle:

После этого можно уже использовать. Каждый сгенерированный binding класс содержит ссылку на корневой view разметки (root) и ссылки на все view, которые имеют id. Имя генерируемого класса формируется как «название файла разметки», переведенное в camel case + «Binding». Например, для файла разметки result_profile.xml:

Читайте также:  Какая сейчас версия инстаграм для андроид

Будет сгенерирован класс ResultProfileBinding, содержащий 2 поля: TextView name и Button button.

Использование в Activity

Например у вас вот такой layout:

Результат работы ViewBinding:

Использовать viewBinding можно так:

И теперь, после того, как получили ссылки на view:

Если вы используете ViewBinding во фрагменте и держите ссылку на binding во фрагменте (а не только в методе onCreateView()) то не забывайте очищать ссылки в методе onDestroyView().

Это необходимо делать из-за жизненного цикла фрагмента и view:

Источник

[Notifications 1.0.0] BUILD FAILURE: Kotlin plugin should be enabled before ‘kotlin-android-extensions’ #179

Comments

UsabilitySpace commented Aug 29, 2020 •

EMULATOR AND OS:

  • Emulator: Android Studio Emulator version Nexus 5X API 28 = qemu-System-x86_64
  • OS: iOs version 10.15.6

ISSUE DESCRIPTION:

After downloading, and when trying to build flutter-plugins/packages/notifications project, such a build failure occurs:

STEPS TO REPRODUCE:

In Android Studio, choose File > Open to open flutter-plugins/packages/notifications directory as an existing project.

Chose Run > Edit Configurations. and then add new Flutter configuration that targets to run example/lib/main.dart .

Run Nexus 5X API 28 = qemu-System-x86_64 Android Studio Emulator.

To start building project, press ⌃R = Ctrl+R key combination, or press Run button in the Android Studio toolbar:

OTHER STEPS TO REPRODUCE:

The issue persists even if trying the next slightly different steps 1′-3′ to reproduce:

1′. In Android Studio, choose File > Open to open flutter-plugins/packages/notifications/example directory as an existing project (but not flutter-plugins/packages/notifications/ directory).

2′. Run Nexus 5X API 28 = qemu-System-x86_64 Android Studio Emulator.

3′. To start building project, press ⌃R = Ctrl+R key combination, or press Run button in the Android Studio toolbar:

EXPECTED BEHAVIOUR:

Project should build & run successfully.

ACTUAL BEHAVIOUR:

Project doesn’t even build.

SCREENSHOTS:

FLUTTER DOCTOR:

THE AIM OF HANDLING THIS ISSUE:

What are reproducable and exact steps for Android Studio users to overcome this issue in Android Studio under iOS?

The text was updated successfully, but these errors were encountered:

Источник

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

Comments

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):

we could enable Kotlin Android Extensions experimental mode:

./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:

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

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

The text was updated successfully, but these errors were encountered:

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.

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 commented Dec 29, 2017

@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 commented Dec 29, 2017

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

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

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

nobodysfault commented Dec 29, 2017

@StefMa basically it’s just

but I’ve tried custom resolution strategy like this:

even with custom overridden versions, but no luck

iNoles commented Dec 29, 2017

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.

nobodysfault commented Jan 2, 2018

@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 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:

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

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 commented Jan 10, 2018

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

toxxmeister commented Jan 10, 2018

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.

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

nobodysfault commented Jan 17, 2018

@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 commented Jan 17, 2018

Yep, KT-20235 is unrelated to this issue.

tadfisher commented Jun 19, 2018

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’

Читайте также:  Donkey kong country returns андроид

apply plugin: ‘kotlin-android-extensions’

// apply plugin: ‘kotlin-android’

androidExtensions <
experimental = true
>

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

rodrigolfonseca commented Sep 18, 2018

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

hereisderek commented Jan 15, 2019 •

@Parcelize still not found

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

lukaszkalnik commented Jan 17, 2019

@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 commented Jan 19, 2019

@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 commented Mar 14, 2019

Any updates about this issue?

SimonSchubert commented Apr 12, 2019

The workaround is not longer needed for kotlin 1.3.30.

Works fine. Issue can be closed.

eskatos commented Apr 12, 2019

Thanks for the feedback @SimonSchubert!
Closing

JakubNeukirch commented Apr 15, 2019

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 , 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 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 commented Apr 23, 2019 •

androidExtensions < isExperimental = true >on kotlin 1.3.30

still doesn’t work for me

syntax for gradle (not kotlin dsl) should be androidExtensions < experimental = true >and it’s working

steam0111 commented Jan 30, 2020

Guys , good answer , work for me

Kotlin plugin should be enabled before ‘kotlin-android-extensions’

neonwarge04 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 commented Mar 14, 2020 •

apply plugin: ‘com.android.application’
apply plugin: ‘kotlin-android’
apply plugin: ‘kotlin-android-extensions’

androidExtensions <
experimental = true
features = [«parcelize»]
>
@neonwarge04 try the above with kotlin 1.3.70. Its worked for me..

langsmith commented Apr 11, 2020

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

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Источник

Оцените статью