Dagger-hilt error while compiling project - android

Getting error below while using dagger-hilt
Unsupported metadata version. Check that your Kotlin version is >=
1.0: java.lang.IllegalStateException: Unsupported metadata version. Check that your Kotlin version is >= 1.0
Please note that I already followed some topics from stackoverflow and other documentation
Hilt Unsupported metadata version in Kotlin
Unsupported metadata version. Check that your Kotlin version is >= 1.0: java.lang.IllegalStateException
https://github.com/google/dagger/issues/2379
Using below app gradle configuration
compileSdk 32
defaultConfig {
applicationId "com.test.plantdemo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
}
Android plugin
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
App level dependency
dependencies {
implementation "androidx.work:work-runtime-ktx:2.5.0"
kapt 'androidx.hilt:hilt-compiler:1.0.0'
implementation 'androidx.hilt:hilt-work:1.0.0'
kapt "com.google.dagger:hilt-android-compiler:2.35.1"
kapt "com.google.dagger:hilt-compiler:2.35.1"
implementation "com.google.dagger:hilt-android:2.35.1"
}
Top level dependency I used
dependencies {
// other plugins...
//classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40'
}
Top level gradle Plugin
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
id 'com.google.dagger.hilt.android' version '2.41' apply false
}
Error window shows below error
[Hilt] Processing did not complete.
See error above for details.
Execution failed for task ':app:kaptDebugKotlin'.
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
java.lang.reflect.InvocationTargetException (no error message)
step I followed after adding library
Step1: Android application class
#HiltAndroidApp
class PlantApplication: Application() {
}
Step2: Module calss
#Module
#InstallIn(SingletonComponent::class)
object MainModule {
}
Step3: View Model
#HiltViewModel
class PlantListBaseViewModel #Inject constructor(): ViewModel() {
}
Step4: fragment
#AndroidEntryPoint
class PlantListBaseFragment : Fragment() {
}

I recommend using the latest stable version of Dagger. Mine is working with these versions:
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
implementation "com.google.dagger:dagger-android-support:2.42"
implementation "com.google.dagger:hilt-android:2.42"
I hope it'll work
Update:
If you are curious about the reason, here:
Looks like Dagger needs to update the kotlin-metadata-jvm library to 0.4.0 which supports reading metadata from Kotlin 1.7, current version is 0.3.0, you might be able to work around the issue by forcing an update on the transitive dep, likely depending on it directly, something like this:
dependencies {
//Not a processor, but forces Dagger to use newer metadata lib
kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.4.2"
}
you can follow that here: https://github.com/google/dagger/issues/3383

Related

AgConnect Performance Monitoring - Version Conflict

With a build.gradle like this (simplified):
buildscript {
...
dependencies {
classpath "com.android.tools.build:gradle:7.3.1"
classpath "com.huawei.agconnect:agcp:1.7.3.302"
}
}
Module build.gradle (merely alike the Enabling APM tutorial):
plugins {
id "com.android.application"
id "com.huawei.agconnect"
}
agcp {
enableAPMS true
}
dependencies {
implementation "com.huawei.hms:base:6.7.0.300"
implementation "com.huawei.agconnect:agconnect-core:1.7.3.302"
implementation "com.huawei.agconnect:agconnect-apms:1.6.1.303"
}
I get this warning:
--W- [APMSClassVisitor] agconnect-apms version [1.6.1.303] is not same with agconnect-apms-plugin version [1.5.2.310], please update one of them, or invalidate AndroidStudio cache if these versions have been same.
Followed by further problems:
[Instrument] an error occurred while instrumenting an unknown class, skipping it: Module requires ASM6
[Instrument] an error occurred while instrumenting an unknown class, skipping it: NestHost requires ASM7
[Instrument] an error occurred while instrumenting an unknown class, skipping it: NestMember requires ASM7
How to upgrade the (bundled) agconnect-apms-plugin version to 1.6.1.303?
To make the versions match, one has to add agconnect-apms-plugin version 1.6.1.300:
buildscript {
...
ext {
agp_version = '7.3.1'
hms_version = '6.7.0.300'
agc_version = '1.7.3.302'
apms_version = '1.6.1.300'
}
dependencies {
classpath "com.android.tools.build:gradle:$agp_version"
classpath "com.huawei.agconnect:agcp:$agc_version"
classpath "com.huawei.agconnect:agconnect-apms-plugin:$apms_version"
}
}
And disable the integrated agconnect-apms-plugin 1.5.2.310, which is otherwise being preferred:
plugins {
id "com.android.application"
id "com.huawei.agconnect"
}
dependencies {
implementation "com.huawei.hms:base:$hms_version"
implementation "com.huawei.agconnect:agconnect-core:$agc_version"
implementation "com.huawei.agconnect:agconnect-apms:$apms_version"
}
Note: enableAPMS false is the default value; true is what caused the version conflict.

Dagger throwing errors after Kotlin integration in a java code

Android project Code base is full java and dagger 2 is implemented in java as well. I am integrating kotlin in the code and each time I rebuild, Dagger gives errors. (Added below)
code:
build.gradle (project)
buildscript {
ext {
kotlin_version = "1.7.10"
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
classpath 'com.android.tools.build:gradle:7.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle (app)
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 32
defaultConfig {
applicationId "co.xyz.abc"
minSdkVersion 21
targetSdkVersion 32
versionCode 15
versionName "1.1.1"
multiDexEnabled true
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
vectorDrawables.useSupportLibrary = true
}
buildTypes {
// ...
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
// AndroidX
// ....
//kotlin
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.7.10'
implementation 'androidx.core:core-ktx:1.9.0'
// ViewModel
def lifecycle_version = "2.4.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// Dagger2
def daggerVersion = "2.35.1"
api "com.google.dagger:dagger:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
// Dagger Android
api "com.google.dagger:dagger-android-support:$daggerVersion"
api "com.google.dagger:dagger-android:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVersion"
// AssistedInject
def assistedInject = '0.6.0'
compileOnly "com.squareup.inject:assisted-inject-annotations-dagger2:$assistedInject"
annotationProcessor "com.squareup.inject:assisted-inject-processor-dagger2:$assistedInject"
androidTestImplementation 'junit:junit:4.13.2'
}
gradle.properties
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m
kotlin.code.style=official
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
Errors after Rebuild:
FAILURE: Build failed with an exception.
What went wrong: Execution failed for task ':app:checkStagingLocalDevAarMetadata'. A failure occurred while
executing
com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
3 issues were found when checking AAR metadata:
Dependency 'androidx.core:core-ktx:1.9.0' requires libraries and applications that
depend on it to compile against version 33 or later of the
Android APIs.
:app is currently compiled against android-32.
Also, the maximum recommended compile SDK version for Android Gradle
plugin 7.2.1 is 32.
Recommended action: Update this project's version of the Android Gradle
plugin to one that supports 33, then update this project to use
compileSdkVerion of at least 33.
Note that updating a library or application's compileSdkVersion (which
allows newer APIs to be used) can be done separately from updating
targetSdkVersion (which opts the app in to new runtime behavior) and
minSdkVersion (which determines which devices the app can be installed
on).
Dependency 'androidx.core:core:1.9.0' requires libraries and applications that
depend on it to compile against version 33 or later of the
Android APIs.
:app is currently compiled against android-32.
Also, the maximum recommended compile SDK version for Android Gradle
plugin 7.2.1 is 32.
Recommended action: Update this project's version of the Android Gradle
plugin to one that supports 33, then update this project to use
compileSdkVerion of at least 33.
Note that updating a library or application's compileSdkVersion (which
allows newer APIs to be used) can be done separately from updating
targetSdkVersion (which opts the app in to new runtime behavior) and
minSdkVersion (which determines which devices the app can be installed
on).
Dependency 'androidx.annotation:annotation-experimental:1.3.0' requires libraries and applications that
depend on it to compile against version 33 or later of the
Android APIs.
:app is currently compiled against android-32.
Also, the maximum recommended compile SDK version for Android Gradle
plugin 7.2.1 is 32.
Recommended action: Update this project's version of the Android Gradle
plugin to one that supports 33, then update this project to use
compileSdkVerion of at least 33.
Note that updating a library or application's compileSdkVersion (which
allows newer APIs to be used) can be done separately from updating
targetSdkVersion (which opts the app in to new runtime behavior) and
minSdkVersion (which determines which devices the app can be installed
on).
When I change compileSdk to 33 :
Task :app:compileStagingLocalDevJavaWithJavac
error: cannot find symbol import
co.abc.client.di.components.DaggerABCApplicationComponent;
^ symbol: class DaggerABCApplicationComponent location: package
co.abc.client.di.components
Now, if I downgrade the core-ktx version to 1.8.0 and change annotationProcessor to kapt :
Task :app:kaptStagingLocalDevKotlin
/StudioProjects/project/app/src/main/java/co/abc/client/di/modules/account/AssistedModule.java:8:
error: cannot find symbol #Module(includes =
AssistedInject_AssistedModule.class)
^ symbol: class AssistedInject_AssistedModule /StudioProjects/project/app/src/main/java/co/abc/client/di/modules/account/AssistedModule.java:9:
error: [ComponentProcessor:MiscError]
dagger.internal.codegen.ComponentProcessor was unable to process this
interface because not all of its dependencies could be resolved. Check
for compilation errors or a circular dependency with generated code.
public interface AssistedModule {}
^ /StudioProjects/project/app/src/main/java/co/abc/client/di/components/ABCApplicationComponent.java:40:
error: [ComponentProcessor:MiscError]
dagger.internal.codegen.ComponentProcessor was unable to process this
interface because not all of its dependencies could be resolved. Check
for compilation errors or a circular dependency with generated code.
public interface ABCApplicationComponent extends
AndroidInjector {
^
Task :app:kaptStagingLocalDevKotlin FAILED
FAILURE: Build failed with an exception.
What went wrong: Execution failed for task ':app:kaptStagingLocalDevKotlin'. A failure occurred while executing
org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
java.lang.reflect.InvocationTargetException (no error message)
What could be the issue here? Should the Dagger-related code be written in Kotlin? Is there a source I could refer to for kotlin integration in java code base for android when dagger is there?
So far, I have tried these which didn't work:
upgrading and downgrading versions for kotlin and dagger.
changing all annotationProcessors to kapt
adding this to gradle - -Pandroid.incrementalJavaCompile=false
android {
compileOptions.incremental = false
}
restart ide
Also, I have another project(java code base and kotlin integration setup) which is working with these versions so the issue doesn't seem to be with that. only difference is, dagger is not in my other project and everything runs fine there.
try using the latest version of the dagger in both Gradle
//Hilt
implementation 'com.google.dagger:hilt-android:2.42'
kapt 'com.google.dagger:hilt-android-compiler:2.42'
kapt "androidx.hilt:hilt-compiler:1.0.0"
in App level
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
Maybe it is a version issue
downgrade your kotlin version and then check it
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.30'
implementation 'androidx.core:core-ktx:1.8.0'

Why Hilt version 2.38.1 not found?

When I am trying to sync my build.gradle file, I am facing some error like this: Plugin [id: 'com.google.dagger.hilt.android', version: '2.38.1', apply: false] was not found in any of the following sources:
I am getting this error only when I am using the Hilt version 2.38.1 which is also currently listed on android official documentation, but when I am using the version '2.41' without changing any other thing, it works! My question is to ask why and how changing the version affects the sync and why version 2.38.1 was not found and 2.41 was.
My guess is something related to repositories kind of thing or the scope where we are looking for these things but I am not sure about any of these, so can anyone explain these?
My project-level build.gradle looks like this:
buildscript {
ext {
compose_ui_version = '1.1.1'
}
}
plugins {
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'com.google.dagger.hilt.android' version '2.38.1' apply false
}
In my app level build.gradle:
I added these 2 plugins
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
And these dependencies:
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"

Android Hilt won't work with plugins added

I have questions about android hilt.
I have added hilt plugin.
//build.gradle(:project)
buildscript {
ext.hilt_version = '2.37'
dependencies {
...
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
//build.gradle(:app)
plugins {
...
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
}
//MyApplication.kt
#HiltAndroidApp
class MyApplication : Application() {...}
When I build the project,
I get the error message saying
"Expected #HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?"
Do you have any idea?
I am also having same problem in my new projects. This error had gone after lowering kotlin version to 1.5.10. I think hilt has compatibility issues with latest kotlin plugin.
As mentioned by others, Kotlin 1.5.20 has a bug in Kapt which causes this issue.
It is fixed in 1.5.21. Just increase the version and you're good.
There is a bug in 1.5.20
To avoid this bug there is a workaround.
in build.gradle (app) file paste this code below.
generally, in the hilt Gradle plugin, these options are set automatically. in 1.5.20 it is not. if you set it manually it will resolve the issue.
kapt {
javacOptions {
option("-Adagger.fastInit=ENABLED")
option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}}

Expected #AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin?

When I am using Hilt in android with Room I got this kinda error.
The full log is here:
home/someone/Desktop/Calculator/app/build/tmp/kapt3/stubs/debug/com/hamidjonhamidov/calculator/MyApplication.java:7: error: [Hilt]
public class MyApplication extends android.app.Application {
^
Expected #HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
[Hilt] Processing did not complete. See error above for details./home/someone/Desktop/Calculator/app/build/tmp/kapt3/stubs/debug/com/hamidjonhamidov/calculator/ui/main/MainActivity.java:7: error: [Hilt]
Anyone knows solution for this?
I had this issue after upgrading Kotlin to 1.5.20.
Adding kapt.use.worker.api=false in gradle.properties worked for me the problem
Checkout dagger issue Support for Kotlin 1.5.20
Fortunately, there is simple solution.
In build.gradle in database scheme, we should use arguments += instead of arguments = .
defaultConfig{
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
Or/And in buld.gradle
You should apply plugin like:
apply plugin 'dagger.hilt.android.plugin'
This solved the problem)
UPDATE
Upgrade Hilt to v28.1.0 and Kotlin to v1.5.21 should fix this issue
OLD ANSWER
If you are on kotlin 1.5.20, answer of Mr-wil will decrease build speed as said in official doc:
To improve the speed of builds that use kapt, you can enable the
Gradle worker API for kapt tasks. Using the worker API lets Gradle run
independent annotation processing tasks from a single project in
parallel, which in some cases significantly decreases the execution
time.
Instead, set:
kapt {
javacOptions {
// These options are normally set automatically via the Hilt Gradle plugin, but we
// set them manually to workaround a bug in the Kotlin 1.5.20
option("-Adagger.fastInit=ENABLED")
option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}
}
source
This generic error message can also appear in many circumstances. As a more generic check, ensure that your module's build.gradle file, ensure that you has:
apply plugin: 'dagger.hilt.android.plugin'
at the top.
This was due to a bug in Kotlin 1.5.20. It is fixed in Kotlin 1.5.21.
So all you need to do is to upgrade to the latest version of Kotlin.
I had the same problem and it seems like there is a problem in kotlin-kapt plugin. If you guys have tried out all the above answers and didn't get resolved, please try the below code in your build.gradle(module-level) outside the dependencies{} block
kapt {
javacOptions {
option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}
}
in the build.gradle of your Android Gradle modules apply the plugin:
apply plugin: 'com.android.application'
apply plugin: 'dagger.hilt.android.plugin'
android {
// ...
}
see detail here
I'm still facing the same issue in 2022
I have solved the problem by adding
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
to build.gradle project and adding
id 'dagger.hilt.android.plugin'
to plugins in build.gradle app
In my case it solved by declaring plugin:
plugins {
id("dagger.hilt.android.plugin")
}
Check if all these are added.In my case I had missed adding one of the below line.
In project level build.gradle
buildscript {
dependencies {
classpath "com.google.dagger:hilt-android-gradle-plugin:2.44"
}
}
In app level build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:2.44"
implementation "androidx.hilt:hilt-common:1.0.0"
kapt "com.google.dagger:hilt-android-compiler:2.44"
}
-In app level build.gradle---add the following in newer versions
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
In project level build.gradle
buildscript {
ext.kotlin_version = "1.7.21"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

Categories

Resources