Cant set buildFeatures databinding with Kotlin DSL - android

I am migrating to Kotlin dsl and I can't set buildFeatures property databinding any more. It is missing from AppExtension class. It is happening in dynamic feature module
Any suggestions?
Before in Groovy worked
android {
buildFeatures {
dataBinding = true
}
}
Kotlin is not working either the same syntax or
buildFeatures.dataBinding = true
Attaching some screenshots.
The error is
Unresolved reference: dataBinding

After submitting a ticket to Google the answer is to use
https://issuetracker.google.com/issues/193452960
android.dataBinding.isEnabled = true
Based on the dev reply marking it as deprecated is a bug and should not be thee case in kts.
From the ticket:
Sorry for the mixed messages about build features, we realized that it made more sense to put the enable flag for features next to the feature itself. The plan is that the supported path will be:android.dataBinding.isEnabled = true
Which works today in kts, but is currently incorrectly marked as deprecated.

You could try to upgrade your AGP.

Related

Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?

I'm trying to migrate some Java library from 'normal' JVM to android and stuck with some Java11 APIs used in the code.
The first thing I already got - Java11 language features seems to work only with Canary build of Android Studio, see answer here
Now I need to get understanding about which APIs can be really used. Here are two use-cases which do not work for me and I can't get if I'm doing something wrong or it never should work:
List.copyOf() - introduced in Java11, method copyOf is not available on android. Methods 'List.of()', introduced with Java 9, work OK.
class java.lang.invoke.LambdaMetafactory - introduced with Java 1.8 - to be used for programmatic creation of lambdas for usage instead for reflection, is not visible on Android.
I see both of them in sources of desugar_jdk_libs here:
https://github.com/google/desugar_jdk_libs/blob/master/jdk11/src/java.base/share/classes/java/lang/invoke/LambdaMetafactory.java
https://github.com/google/desugar_jdk_libs/blob/master/src/share/classes/java/util/List.java
So - the question is: how can I identify if some Java API is supposed to be available in 'desugared' android build or no? What really can be expected from 'desugaring'?
Steps to reproduce:
Using Android Studio Canary generate a dummy "Basic Activity" project
Make sure following is provided in build.gradle
android {
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
Add following lines somewhere in code
List<Integer> ints1 = List.of(1, 2, 3);
Supplier<List<Object>> listSupplier = () -> new ArrayList<>();
List<Object> alist = listSupplier.get();
List<Integer> ints2 = List.copyOf(ints1);
LambdaMetafactory.metafactory(null,null,null,null,null,null);
Last 2 lines fail to compile for me.
PS: final application is supposed to work on Android 10+.
Contrary to the other answer, desugaring is totally possible.
The dependency to add is
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
You can find more information at the official Android Java 8 desugaring documentation.
Desugaring lib is considered unofficial. We can't expect an exact answer. We get the feature when it is ready. Now List.copyOf() method now working with the latest Gradle version.
About the LambdaMetafactory class, It is not included in Android Javadoc. This means we assume we don't have LambdaMetafactory at all. Google stripped down some java API for being lightweight.
In general, We should check android Javadoc first. If android Javadoc has no mention about some API. We can be sure we won't get that feature anytime soon.

Error with Android View Binding library after updating Android Studio to 4.0

Last night I upgrade my Android Studio 4.0, it shows error in View Binding. I have tried the below code in my Gradle. But it shows error like this,
Could not find method buildFeatures() for arguments [build_99kmfmmumw2bmow3xjrnbxlx1$_run_closure1$_closure5#4155ecc3] on extension 'android' of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
android {
buildFeatures {
viewBinding = true
}
}
Try with:
dataBinding{
enabled = true
}

Android viewbinding deprecation warning persists in Android Studio 4 after making appropriate update

After updating to Androis Studio 4.0 I received a warning that android.viewBinding.enabled was deprecated and shoud lbe replaces with android.buildFeatures.viewBinding.
I therefoe changed the appropraiet part of my build.gradle (app) from:
android {
...
viewBinding {
enable = true
}
...
}
to:
android {
...
buildFeatures {
viewBinding {
enabled true
}
}
...
}
I no longer get the warning, but still get an information box in my build window as follows:
build.gradle: DSL element 'android.viewBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.viewBinding'.
It will be removed in version 5.0 of the Android Gradle plugin.
Is this normal? I knows it's not a warning or an error, but it seems odd to tell me about something that has been fixed - or have I not fixed it correctly (my app still works using viewBinding as expected).
(Note also that when adding the buildFeatures section to the file, none of the required entires, including buildFeatures pops up in teh auto-complete prompt.)
Solved it!
Although it was working - i.e. the View bindings were working fine - the correct new syntax appears to be:
android
...
buildFeatures {
viewBinding true
}
...
}
...still not popping up in the auto-complete, though.
Update: After a few buids and rebuilds I checked again and now buildFeatures IS popping up in teh auto-complete... Guess I wasn't being patient enough for everything to catch up. Just a bit odd that invalidating the caches didn't fix the auto-complete straight away.
#Fat Monk
Please add this line to your app build.gradle file.
android.buildFeatures.viewBinding = true
I had also this warning.
To solve it, I had just to remove from my build gradle (module app) this old line :
viewBinding.enabled = true
and I replaced it with :
android.buildFeatures.viewBinding = true

Android and the Fabric (Crashlytics) plugin always generates a UUID (Gradle Kotlin DSL)

I want Fabric to stop generating a UUID on each build. What used to work with Gradle's Groovy DSL does not work with the newer Kotlin DSL. How can I achieve my goal with the Kotlin DSL?
(Gradle version 4.10.2, Fabric 1.25.4)
According to Fabric's documentation, you can add the following to your app's build script
android {
buildTypes {
debug {
// Only use this flag on builds you don't proguard or upload
// to beta-by-crashlytics
ext.alwaysUpdateBuildId = false
and this works. It prevents Fabric from generating a UUID on each debug build. However, if I convert my build script to Kotlin DSL, the following doesn't work
android {
buildTypes {
getByName("debug") {
// Only use this flag on builds you don't proguard or upload
// to beta-by-crashlytics
ext.set("alwaysUpdateBuildId", false)
Fabric ignores this value, now.
I have tried variations, such as the following:
project.ext.set("alwaysUpdateBuildId", false)
rootProject.ext.set("alwaysUpdateBuildId", false)
val alwaysUpdateBuildId by extra(false)
val alwaysUpdateBuildId by project.extra(false)
val alwaysUpdateBuildId by rootProject.extra(false)
None work.
For further reference, the Gradle task generating this value appears to be named :app:fabricGenerateResourcesDebug, and has type DefaultTask.
As Martin Rajniak mentioned, you can only call extra on ExtensionAware objects, with BuildType not being declared as one.
However, during runtime, build types actually are ExtensionAware, which is why this works in Groovy due to its dynamicity, but not in Kotlin where extra in this scope will reference the Project's extensions.
In order to achieve this without Groovy, we can simply cast the build type to ExtensionAware:
android {
buildTypes {
getByName("debug") {
(this as ExtensionAware).extra["alwaysUpdateBuildId"] = false
}
}
}
I have found a workaround to this problem. Create a file, fabric.gradle (Groovy build script!) and place it in your project structure somewhere. It will have the following contents:
// or "com.android.library"
project.pluginManager.withPlugin("com.android.application") {
android.buildTypes.debug.ext.alwaysUpdateBuildId = false
}
Now, in the build script for your module (let's call it app/build.gradle.kts), apply this script plugin:
apply(from = "path/to/fabric.gradle")
This workaround is based on the advice here, in the Kotlin DSL primer.

How to use AndroidAnnotation #SharedPref with Kotlin

I'm trying to use AndroidAnnotations #SharefPref within kotlin, but Iget following error
org.androidannotations.annotations.sharedpreferences.Pref can only be used on an element that extends org.androidannotations.api.sharedpreferences.SharedPreferencesHelper
What am I doing wrong?
//Interface
#SharedPref(SharedPref.Scope.APPLICATION_DEFAULT)
open interface MyPreferences {
#DefaultInt(-1)
fun someIntValue():Int
}
//Fragment
#Pref
lateinit open var sharedPref:CongressPreferences_
//usage within fragment
val get: Int = sharedPref.selectedEventId().get()
This is due to a bug in the Kotlin annotation processor.
To fix this, you must add correctErrorTypes = true to your kapt block.
kapt {
correctErrorTypes = true
}
Also make sure you are using the latest Kotlin version (as of this moment: 1.1.3).
I just wanna extend on #WonderCsabo 's answer.
His answer almost saved me, but not fully.
After adding this to my app label build gradle.
kapt {
correctErrorTypes = true
}
I wasn't able to run my app.
Then I closed my android studio and then run Android studio again as administrator.
Voila! it works like charm.
Thank you #WonderCsabo

Categories

Resources