Release build fails on Room generated classes: javax.annotation does not exist - android

I have a java project that builds fine in Android Studio and on the CI server.
Building a release version with gradle on the terminal fails though.
I get the following output:
Database_Impl.java:42: error: package javax.annotation does not exist
Database_Impl.java:44: error: cannot find symbol
#Generated("androidx.room.RoomProcessor")
Some similar issues with Dagger suggests that this is a problem with the Java version of the build system.
I'm using a Mac and my javac version is: 10.0.2

I had the same issue and adding implementation "javax.annotation:jsr250-api:1.0" to build.gradle dependencies helped.

This seems to be related to the Java version that is active on your machine.
There is a similar bug ticket with Dagger: https://github.com/google/dagger/issues/1449
The result of the discussion is that this is a bug in KAPT: https://youtrack.jetbrains.com/issue/KT-32804
The workaround is to put
if (project.hasProperty('kapt')) {
// Reference for 'kapt' DSL: https://kotlinlang.org/docs/reference/kapt.html#java-compiler-options
kapt {
// we expect this closure to run over a org.jetbrains.kotlin.gradle.plugin.KaptExtension
javacOptions {
option("-source", "8")
option("-target", "8")
}
}
}
at the end of the build file of the affected module.

Related

How to get rid of Kotlin DSL warnings

I have just started learning KotlinDSL recently.
And in Android added buildSrc. In this folder I have module plugins: AppModulePlugin, CommonModulePlugin, FeatureModulePlugin. All of this compiles and the application installs correctly, everything is fine. but in these files a warning is displayed:
Cannot access 'com.android.build.gradle.internal.dsl.Lockable'
which is a supertype of 'com.android.build.gradle.BaseExtension'.
Check your module classpath for missing or conflicting dependencies
Please tell me how to get rid of these warnings?
IntelliJ has had this issue for months unfortunately - it reports correct code as invalid. See KTIJ-3769. I believe it's dependent on ticket KTIJ-19669.
As a workaround you can either replace the helper methods that the plugins introduce with 'plain' Gradle Kotlin
// androidExtension { }
project.extensions.configure<BaseExtension> {
}
Or only create buildSrc plugins in Kotlin (*.kt files), not Kotlin Script (*.kts files). This requires using the java-gradle-plugin and defining the plugins in buildSrc/build.gradle.kts.

Upgrading Android kotlin version to 1.5.0 throwing error message on build

Running with kotlin version '1.4.32' my Android project runs and builds. Trying to upgrade to kotlin '1.5.0' and my build throws:
Execution failed for task ':app:kaptDefaultsDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)
I am not even sure where to start looking. Anyone else have problems upgrading to kotlin 1.5.0?
I experienced the same issue today. Ran the gradle build command with --stacktrace and got a helpful stacktrace which narrowed down the issue to Moshi library.
Caused by: java.lang.IllegalStateException: Could not parse metadata! This should only happen if you're using Kotlin <1.1.
at com.squareup.moshi.kotlinpoet.metadata.KotlinPoetMetadata.readKotlinClassMetadata(KotlinPoetMetadata.kt:70)
at com.squareup.moshi.kotlinpoet.metadata.KotlinPoetMetadata.toImmutableKmClass(KotlinPoetMetadata.kt:50)
at com.squareup.moshi.kotlin.codegen.MoshiCachedClassInspector.toImmutableKmClass(MoshiCachedClassInspector.kt:22)
at com.squareup.moshi.kotlin.codegen.MetadataKt.targetType(metadata.kt:109)
at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.adapterGenerator(JsonClassCodegenProcessor.kt:136)
at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.process(JsonClassCodegenProcessor.kt:110)
at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt:90)
at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:175)
at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:980)
... 41 more
Had to update Moshi to 1.12.0 and that did the trick.
Here's a changelog for Moshi 1.12.0 that mentions the fix for 1.5.0: https://github.com/square/moshi/blob/master/CHANGELOG.md#version-1120
Issue when it was first reported here: https://github.com/square/moshi/issues/1324
Another issue which mentions the fix here: https://github.com/square/moshi/issues/1337
So I'd suggest you run gradle build command with --stacktrace and figure out which library causes the incompatibility with the kotlinx-metadata-jvm library and update it.
This is due to Dagger's use of older version of kotlinx-metadata-jvm.
See https://youtrack.jetbrains.com/issue/KT-45885
Update your dagger to 2.34
In my case dagger caused the issue.
Updating the dependency worked out.
implementation 'com.google.dagger:dagger:2.37'
kapt 'com.google.dagger:dagger-compiler:2.37'
This issue resurfaced for me after upgrading my project to Kotlin 1.6.0. In my case it's because of the Moshi JSON serialization lib. They have already included a fix in their next release. In the meantime, I have reverted back to 1.5.31.
https://github.com/square/moshi/issues/1433
I had this issue while upgrading Kotlin version to 1.6.10
To fix, I had to update the dagger version to 2.36 or higher and I used 2.40.5
implementation "com.google.dagger:dagger:2.40.5”
kapt "com.google.dagger:dagger-compiler:2.40.5"
To find latest dagger versions - https://github.com/google/dagger/releases
If other answers didn't help you, and especially if you're on alpha or beta version of Android Studio, make sure that you using JDK embedded with Android Studio. It was my case, i did use external downloaded JDK.
To change JDK go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle and select Embedded JDK from dropdown Gradle JDK.
You should check which of your dependencies probably depends on new kotlin changes in version 1.5.0 and try to update them.
For me the room and dagger was causing problems, i've upgraded it to the latest versions and now i have no problems:
room to : 2.3.0
dagger to : 2.35.1
It might not necessarily be dagger or moshi. It can be any library that uses kapt (kotlin annotation processor). Look for kapt in your build.gradle and try to update everything that uses it.
For me it was a library which was using an older version of kotlinpoet.
You should update hilt to 2.42 for kotlin 1.7
I had same issue during build:
KaptWithoutKotlincTask$KaptExecutionWorkAction
I had a data binding issue in one of my XML, which threw this error.
This was my case:
abc.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
    </data>
    <TextView
android:text="#{object.name}"
... />
</layout>
object variable was not declared. After declaration, it resolved the build issue.
The error message did not point to the correct location in my case.
yes in my case recently i solved the problem by updating dagger to latest version and gradle update to latest version as well as using latest jdk
I have same issue, and finally solved by update moshi to 1.12.0
My dagger error message looked as follows:
error: [androidprocessor:miscerror]
dagger.android.processor.androidprocessor was unable to process this
class because not all of its dependencies could be resolved. check for
compilation errors or a circular dependency with generated code.
I also had this kapt error: java.lang.reflect.InvocationTargetException (no error message)
I was given which file dagger was not able to properly process, but no exact information on missing dependency. I applied all the fixes I found in this so. I had to carefully examine all the dependencies I could see in the generated by dagger file for which error was shown.
In my case it was dagger not able to find org.jetbrains.annotations.NotNull. I noticed this dependency was upgraded, and due to some fixes I moved to use androidx.annotation.NonNull in my code. I actually didnt depend on org.jetbrains.annotations - it was being included from some other dependency. The error looked as follows:
found in modules jetified-annotations-12.0
(com.intellij:annotations:12.0) and jetified-annotations-16.0.1
(org.jetbrains:annotations:16.0.1)
I was resolving conflicts which ended up so that dependency was not visible by dagger.
As usual in such cases it was very usefull to see dependecy tree and where dependencies are comming from. Some dependencies were outdated and used org.jetbrains:annotations in older version, and I had to either upgrade them or block this dependency from being included in my code with exclude, for example:
implementation ("io.reactivex.rxjava2:rxkotlin:$rxKotlinVersion") {
exclude group: 'org.jetbrains', module: 'annotations'
}
for dependency tree listing see here: How do I show dependencies tree in Android Studio?
Got this error due to Gradle not recognizing some of my modules. Re-syncing settings.gradle fixed this for me.
Go to https://mvnrepository.com/artifact/com.google.dagger/hilt-android-gradle-plugin to get the latest hilt version.
Then, update the project build.gradle and update to the latest version:
dependencies {
classpath('com.google.dagger:hilt-android-gradle-plugin:2.43.2')
}
Do the same for the app build.gradle
dependencies {
implementation("com.google.dagger:hilt-android:2.43.2")
kapt("com.google.dagger:hilt-android-compiler:2.43.2")
}

How would you troubleshoot dependency errors that arise in Release mode only in Android?

Context
We have an Android app built in Kotlin and we've split our algorithms layer into a separate Android Project. We import that as a dependency using Gradle using the following code:
In build.gradle:
dependencies {
...
implementation project(':algos-android')
}
This algos-android project has its own dependency - koma - defined in the algos-android project's build.gradle file as follows:
dependencies {
...
implementation group: "com.kyonifer", name:"koma-core-ejml", version: "0.12"
}
Issue
This works completely fine when running the app in a debug mode (defined in buildTypes in the build.gradle). However, when running in release mode, we are seeing an error at runtime when our algorithms layer uses its sub-dependency - koma.
The following error shows up in the console:
java.lang.IllegalStateException: No double matrix factories available. (Did you forget to import a koma-core implementation?)
My team was under the assumption that Gradle handles sub-dependencies for us. Has anyone run into issues like this before? Are there any good debug approaches?
Things We've Tried
Moving all of the files from our algos-android project directly into the main project and importing koma directly in the main project's gradle.build file.
Environment
Android Studio 4.0.1
Kotlin Version 1.3.72
Gradle Version 6.3
Subdependency Repo:
https://github.com/kyonifer/koma
We were finally able to track down the issue and resolve this with the following addition to the proguard-rules.pro file:
-keep class koma.** { *; }
We were able to find that since the Matrix factory classes are inflated in koma sources using reflection, proguard considered them as unused and simply deleted them from the release build.
Note: If you see this and make this fix, don't forget to Clean Project before running.

Firebase libraries with conflicting library versions fail to run my app

So i'm using the following firebase library version in my app:
Project's build.gradle:
firebase_version = '15.0.0'
And the following library dependencies:
App's build.gradle:
implementation "com.google.firebase:firebase-firestore:$firebase_version"
It looks like these versions were released on the 10th of April, however when compiling my app with the updated libraries, it fails to run with the following error:
error: cannot access zzbgl
class file for com.google.android.gms.internal.zzbgl not found
When checking all my library versions, my build.gradle has the following error:
not sure if anyone has experienced this by any chance since the latest updates? Before updating from 12.0.1, everything was working.
Thanks
Simply override (add to your gradle file) the conflicting library, updating the version to match the ones you already have in your gradle file. Somewhere in your dependencies someone is using an older version of this library and it's crashing with your version:
implementation "com.google.android.gms:play-services-auth:$firebase_version"

Android gradle JAR dependency ends up declared as native

In my Android projects build.gradle file I have:
dependencies {
compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
compile 'org.apache.commons:commons-collections4:4.0'
}
When I run aapt dump badging I see:
native-code: 'commons-io-2.4.jar'
Clearly commons-io is not native code but the Android Gradle plugin thinks it is. How can I tell the Android Gradle plugin this library does not contain native code? Also curious how did it decide what architecture this library is? It seems to have picked some variety of arm since the APK won't install on a intel-based Android emulator, it gives the error: INSTALL_FAILED_CPU_ABI_INCOMPATIBLE.
You may be using the incorrect Commons IO library. Try:
http://mvnrepository.com/artifact/commons-io/commons-io/2.4
The version you're using seems to be specific to Apache Directory Studio 2.4 which seems to be a desktop app.
I once had troubles introducing a dependency on commons-io:2.4 into my build code. The problem is that Gradle itself comes with a version of commons-io. In my case it could be fixed with this:
sourceSets {
main {
compileClasspath = configurations.compile.minus files("$gradle.gradleHomeDir/lib/commons-io-1.4.jar")
}
}
I took this snippet from My Gradle project depends on commons-io 2.4, but Gradle puts $GRADLE_HOME/commons-io-1.4.jar into the classpath, causing failures
But be aware that it is not recommended to use different versions of libraries to come with Gradle.
You can find more information about this here: https://discuss.gradle.org/t/unable-to-use-commons-io-2-4-because-gradle-forces-the-loading-of-commons-io-1-4/8021
This might aswell be completely unrelated :)

Categories

Resources