Android Data Binding and Kotlin - android

I am converting my Android application from Java to Kotlin. It is working fine, except when I try to convert a file that is using Android Data Binding Library.
In that case, Android Studio complains at compile time about unresolved reference:
Error:(10, 44) Unresolved reference: AdapterHistoriesListBinding
Where AdapterHistoriesListBinding is the name of a file that should be generated by the Data Binding Library. It was working correctly in Java, so I guess it is an issue with Kotlin.
I am using Android Studio 2.0.0-beta6, Android Gradle Plugin 2.0.0-beta6 and Kotlin 1.0.
Is there something to do to make the Data Binding Library work with Kotlin?

Few steps to setup databinding in your Kotlin project.
Tell kapt to use databinding compiler in module dependencies:
dependencies {
kapt 'com.android.databinding:compiler:2.0.0-beta6'
}
As Shintaro Katafuchi mentioned, you should tell kapt to generate stubs.
kapt {
generateStubs = true
}

Have you tried adding following setting in your build.gradle?
kapt {
generateStubs = true
}

Related

Could not get unknown property 'implementation' for object of type Dependency

Hello there after adding dependency of paging
val paging_version = "3.1.0"
implementation("androidx.paging:paging-runtime:$paging_version")
I'm getting this error
Could not set unknown property 'paging_version' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
Note: I have switched my project from java to kotlin ( because there
is some issue the entire project cant be converted so I convert every
java file one by one)
There's 2 different kinds of migrating to kotlin for android projects.
What you did is migrate your code from java to kotlin. This is what people usually mean when they want to migrate their project to kotlin.
This by itself doesn't change the gradle file to kotlin. The code you tried is applying kotlin to the gradle file. It is possible to change to kotlin (instead of groovy, which you probably have now) but that's a different process.
The groovy equivalent of
val paging_version = "3.1.0"
implementation("androidx.paging:paging-runtime:$paging_version")
would be
def paging_version = "3.1.0"
implementation "androidx.paging:paging-runtime:${paging_version}"
I have added it like this and it works:
implementation 'androidx.paging:paging-runtime-ktx:3.1.0'
Use this :
def paging_version = "3.1.0"
implementation "androidx.paging:paging-runtime:$paging_version"

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution > java.lang.reflect.InvocationTargetException (no error message)

Android studio gave the error:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
I want to add in my project Kotlin Coroutines and use it with Room database. But after added all libraries I got this error. This is all information from the compiler.
I have identified, This is because of the annotation #Database. If I removed this annotation, the error don't appear, but Room is not working too.
My gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
//apply plugin: 'androidx.navigation.safeargs'
kotlin {
experimental {
coroutines 'enable'
}
}
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.bestcred.coursetthree"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
//vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Enables data binding.
buildFeatures {
dataBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Support libraries
implementation "androidx.appcompat:appcompat:1.2.0"
implementation 'com.google.android.material:material:1.2.0'
implementation "androidx.fragment:fragment:1.2.5"
implementation "androidx.constraintlayout:constraintlayout:2.0.0"
// Android KTX
implementation 'androidx.core:core-ktx:1.3.1'
// Room and Lifecycle dependencies
implementation "androidx.room:room-runtime:$room_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
kapt "android.arch.persistence.room:compiler:$room_version"
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
}
kotlin_version = "1.4.0"
room_version = "2.2.5"
coroutine_version = '1.3.9'
I update Room version and add Kotlin Coroutines. What's problem?
Android Studio's UI was just hiding the error...
when the error occurred, it highlighted the item in the list view, and showed an unhelpful message in the terminal view.
to find the real error, select the root item in the list view so that Android Studio would display the whole build output in the terminal view, then scroll to find error.
I develop in Apple Silicon Macbook M1.
use room_version 2.2.4,
fails in 2.2.5
def room_version = "2.2.4"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
You need change:
kapt "android.arch.persistence.room:compiler:$room_version"
to
kapt "androidx.room:room-compiler:$room_version"
This exception occurs when you have done some mistake on Room database or Doa or entity class
for example I had done mistakes in the entity class
I had made the autogenerated field of Entity class val instead of var
I had put delete annotation on two functions with a different name but they were deleting the same data
so I would suggest to check the entity,dao or database class carefully if you imported the right dependency.
#Entity(tableName = "user_table")
data class User(
val firstName: String,
val lastName: String,
val age: Int
) {
#PrimaryKey(autoGenerate = true)
var id: Int = 0 //**do not made it val**
}
I'm currently having this error on m1 Mac with Room version 2.3.0.
I fixed it by changing it to 2.4.0-alpha04.
This has been reported to Google (issue tracker).
Today I faced this error. In my case, I have both java 11 and java 16 installed in my laptop, and java 16 was the default. But the gradle in my project uses java 11 to build it. So when I invoked
$ gradle build
from my terminal, it would use java 16 and so the build failed. I know, the proper solution is to make my code compatible with java 16, but for now, I am explicitly telling gradle to use java 11 using the following command:
$ gradle build -Dorg.gradle.java.home=/usr/lib/jvm/java-11-openjdk-amd64 # warning: use te path to your java 11
Since it is a large command, I saved it in a script file and so simply running the script solves the problem for me.
Try to add this in your app/build.gradle file and run the program again. It helped me to find the real cause of the issue.
kapt {
correctErrorTypes true
}
I might be late but only following solution worked for me.
Open gradle.properties file in Android studio.
Add following lines:
kapt.use.worker.api=false
kapt.incremental.apt=false
Clean & Re-build the application.
Done
I had faced the same issue. The problem was because I was using coroutine suspend functions in the Room DAO and had not implemented the necessary implementations. The following worked for me, try replacing the existing Room dependencies with:
def room_version = "2.2.5" //Use latest version
"androidx.room:room-runtime:$room_version"
"androidx.room:room-compiler:$room_version"
"androidx.room:room-ktx:$room_version"
If the above does not work, go to Analyze->Inspect Code and check for code-breaking warnings or errors(Specifically in Room)
I had to update Room to latest 2.2.5 and it has resolved the issue.
This issue occurred on package renaming
I've dragged and dropped all files from one package to other.
Objectbox it created the cursor files and entity files (auto-generated files)
Had to delete them and re-built.
It ate up 1 hour straight -_-
Make sure Gradle JDK is set to the correct version (it was set to JDK 8 even though I've been using JDK 11 for quite some time now).
Open Preferences -> Builds Tools -> Gradle -> Set Gradle JDK.
Image of Gradle JDK in Android Studio
You have to use gradlew assemble --stacktrace in terminal to get more detailed error log.
If you're on Mac, then run this instead ./gradlew assemble --stacktrace
Make sure to include all entities inside the Database annotation.
I updated the Room version from 2.2.5 to 2.3.0 or to current version in the build.gradle script file. The build was successful.
Update the gradle JDK to java 11.
There's an error that is in your files, but the editor cannot show you because the file is closed and hasn't been analysed.
This mostly occurs when you amend some data classes/POJOs and your app has several layers that depend on each other.
As the dependency goes up, some functions cannot infer data types and returns this kind of an error.
Command+Click on the modified data classes and see where
they are being used, you may possibly find the error there.
Open your files one by one, you don't have to hang on
there and wait for the editor to finish analysing(If your editor
takes some time to), as the editor analyses, the errors will be
displayed and you can fix them and have a successful build.
There's the option of Code -> Analyse code on android studio but sometimes it doesn't work as expected, you may try this - just incase.
For Apple M1 processor :
Kindly update the room version to the latest one.
implementation "androidx.room:room-runtime:2.4.3"
implementation "androidx.room:room-rxjava2:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
In my case I gave the argument type of insertAll() and delete() function as Entity ofyour database, the Error Solved
#Dao
interface NotesDao {
#Insert
fun insertAll(INotesEntity: NotesEntity) // argument type should be entity of your database
#Delete
fun delete(DNotesEntity: NotesEntity) // argument type should be entity of your database
}
While it's rather strange, I had onDestory defined two times in one file and caused this error when trying to run the app, once i deleted one of them everything worked normally.
When I faced this issue, it was about data-binding in an XML file.
I deleted a code using backticks `` and changed it into string interpolation.
suspend not being compatible with LiveData, So delete suspend from your DAO function that returns LiveData
The only thing that worked for me -
In gradle.properties replace the following line -
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
by
org.gradle.jvmargs=--illegal-access=permit

What is generateStubs configuration in Kotlin?

What is generateStubs for Kotlin? Here is my configuration in build.gradle.
I can not find it in public document here : http://kotlinlang.org/docs/reference/kapt.html
kapt {
generateStubs = true
}
If I'm using Java and Kotlin(1.2) in my project, it is still needed to add?
EDIT:
If I'm using Java and Kotlin(1.2) in my project, it is still needed to
add
No, With the version 1.0.4 introduces a new experimental implementation of the annotation processing API. Now there is no need to configure this generateStubs in build.gradle.
Add the following to your build.gradle if you want to enable it:
apply plugin: 'kotlin-kapt'
You will also have to remove the generateStubs config from build.gradle
But as your question about the Details on generateStubs I keep my old Post as it is.
Use :
Using kapt with: generatestubs = true, in order to use libraries like dagger 2 or dbflow,This will enable the compiler to generate stub classes required for interoperability between Java and Kotlin. Unless generateStubs = true is enabled, "bootstrap" (A custom annotation processor, which is passed to javac, loads the annotation data and calls other annotation processors.) Java code is required to reference generated sources.pulled that from
Note : Generated codes are always in Java not in Kotlin.
When to Include:
Generating stubs requires relatively much work, because all declarations must be resolved, and sometimes knowing return types requires analysis of expression (bodies of functions or property initializers after the = sign). So, using stubs in kapt slows your build down somewhat. That’s why stubs are off by default, and to enable them you need to write the following in your build.gradle file:
kapt {
generateStubs = true
}
How this works:
Stubs, compiler generated intermediate classes, allows "generated" sources to be referenced from Kotlin otherwise the compiler will not be able to reference the missing sources.
Generated source is created in "build/generated/source/kapt/main", as this is under "build", normally excluded from IntelliJ's project sources, this source root will be marked in the build script itself.
sourceSets {
main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
}
Example :
Dagger2-example with Kotlin (1.1.50) annotation processor support Gradle build

Kotlin build cannot get classes created by SquidDatabase

In my project I use SquidDatabase almost everywhere to access the database.
I want to transform my project to Kotlin but when I build there are lots of "Unresolved reference: DepartmentInfo" errors.
That DepartmentInfo class was created by SquidDatabase while building so I think this problem is caused by Kotlin building before other libraries.
I failed to find any way to change Kotlin to the end of buildlist.
How can I solve this problem?
I posted this issue on Github and got reply from developers.
The solotion is here:
build.gradle of your app
//If using kotlin language,please add this
apply plugin: 'kotlin-kapt'
......
dependencies {
compile 'com.yahoo.squidb:squidb:3.2.3'
compile 'com.yahoo.squidb:squidb-annotations:3.2.3'
compile 'com.yahoo.squidb:squidb-android:3.2.3' // For Android projects only
//annotationProcessor 'com.yahoo.squidb:squidb-processor:3.2.3'
// If using the android-apt plugin, this becomes
// apt 'com.yahoo.squidb:squidb-processor:3.2.3'
// If using kotlin language, this becomes
kapt 'com.yahoo.squidb:squidb-processor:3.2.3'
}
It may cause errors if you use DataBinding for Andriod,and the solution is to add one more kapt in your dependencies:
kapt 'com.android.databinding:compiler:$gradle_version'

Does butterknife 7.x work with Kotlin M14?

I'm trying to use Butterknife with some Kotlin code and also Java code. I know that before M12, there was bad or no support for annotation processing that ButterKnife required. So I have kept my activities in Java. It was working at least in Java with Butterknife 6.x and preM12 Kotlin.
I'm trying now butterknife 7.x with M13 and M14. It should have even annotation processing support, but it's not working for me. bind() function doesn't bind anything in my adapter which is written in Java nor in activity written in Kotlin.
I'm using this in build.gradle (tried latest version on Github):
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
provided files('libs/butterknife-annotations.jar')
kapt files('libs/butterknife-compiler-8.0.0-SNAPSHOT.jar')
compile 'com.jakewharton:butterknife:8.0.0-SNAPSHOT#aar'
}
This doesn't compile. I tried also 'com.neenbedankt.android-apt' which does compile but binding is not working.
I know that there is probably not support in butterknife for that yet. But is there any hack to get it working?
It does work with the current version of Kotlin (1.0.0-beta-3595), I suggest you to take a look at the android-butterknife project which can be found inside the JetBrains's kotlin-examples repo. In short all you need to do is:
Add the following to your app/build.gradle file:
kapt {
generateStubs = true
}
Put the following line inside the dependencies block of the same build.gradle file (assuming you already added compile 'com.jakewharton:butterknife:7.0.1' to your dependencies):
kapt 'com.jakewharton:butterknife:7.0.1'
And that should be it.
Butterknife is supported. Use kapt: [1], [2].
Note that Butterknife does not support private Java fields, so you can use the lateinit modifier to make it public.
Also, if you use kapt,
apply plugin: 'com.neenbedankt.android-apt'
line is not needed anymore.

Categories

Resources