My Android app builds failed Hilt, WorkManager - android

I am trying to migrate from Dagger2 to Hilt following the reference: https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager
But when I build my app, it fail with below error log:
/Users/.../myapp/build/tmp/kapt3/stubs/devDebug/.../MyWorker.java:13: error: incompatible types: NonExistentClass cannot be converted to Annotation
#error.NonExistentClass()
^
And here is the line 13:
line 11 #error.NonExistentClass()
line 12 public MyWorker(#org.jetbrains.annotations.NotNull()
line 13 #error.NonExistentClass()
line 14 android.content.Context appContext, #org.jetbrains.annotations.NotNull()
line 15 #error.NonExistentClass()
line 16 androidx.work.WorkerParameters params, #org.jetbrains.annotations.NotNull()
I don't know how can I fix it...
What I using versions are:
// kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.5.21"
// Hilt
implementation "com.google.dagger:hilt-android:2.35"
kapt "com.google.dagger:hilt-android-compiler:2.35"
implementation 'androidx.hilt:hilt-work:1.0.0'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
// WorkManager
implementation "androidx.work:work-runtime-ktx:2.5.0"
implementation "androidx.work:work-rxjava2:2.5.0"
These versions are the latest and stable versions.
I tried to add the below Gradle setting but it's same error...
kapt {
correctErrorTypes true
}

Whooooooooo.....
I found the cause...
I'm sorry for all...
I am Korean, so I have been watching this guidance document for the Korean version: https://developer.android.com/training/dependency-injection/hilt-jetpack
But I found it is too old!
It guides to use #WorkerInject in the Korean version,
But it guides to use #AssistedInject in the English version...
When I change it, the error disappeared...

Related

Cause: lateinit property _buildFeatureValues has not been initialized

When I Upgrade Gradle from 4.2.2 to 7.3.0 I got Below Error
Cause: lateinit property _buildFeatureValues has not been initialized
So How to solve this error?
I have same error as you facing but as Andrey suggested I did build using command line and full error which I didn't get when I directly build don't know why?
But It's through error related flat dir and aar file so I did removed flat dir code and change aar file implementation code now It's started to build and my app started to install.
If you are use dagger hilt you should change your dependency as below to avoid Exception
java.lang.IllegalArgumentException: CreationExtras must have a value by SAVED_STATE_REGISTRY_OWNER_KEY error in runtime.
kapt "com.google.dagger:dagger-compiler:2.42"
kapt "com.google.dagger:hilt-android-compiler:2.43.2"
implementation "com.google.dagger:dagger:2.42"
implementation "com.google.dagger:hilt-android:2.43.2"
implementation 'androidx.hilt:hilt-work:1.0.0'
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation "androidx.navigation:navigation-compose:2.5.1"

Difficulty using Moshi's Kotlin codegen

I am trying to use Moshi's Kotlin codegen to get annotation support in Kotlin. Despite following the instructions carefully in the moshi codegen documentation, the annotation JsonClass in #JsonClass(generateAdapter = true) is not recognized and I am getting the following error:
error: incompatible types: NonExistentClass cannot be converted to Annotation#error.NonExistentClass()
My app build.gradle file is as follows:
...
apply plugin: 'kotlin-kapt'
android {
...
}
dependencies {
...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0'
}
The #JsonClass annotation is recognized when I add
implementation("com.squareup.moshi:moshi-kotlin:1.8.0").
However, the moshi reflection documentation indicates that this dependency is only required if using reflection instead of codegen.
Any idea what I'm missing? Thanks!
#JsonClass is a type in the standard Moshi artifact. Retrofit's converter-moshi artifact brings in Moshi transitively but does not have the latest version of Moshi. Specify implementation("com.squareup.moshi:moshi:1.8.0").

ViewModel in Kotlin: Unresolved Reference

I am trying to implement ViewModel in a 100% Kotlin app. Every piece of documentation I can find says I want to use this to get the ViewModel instance:
ViewModelProviders.of(this).get(CustomViewModel::class.java)
According to the docs, I should be able to import this with:
import android.arch.lifecycle.ViewModelProviders
This import is unresolved though. I am using the following in my build file:
def androidArchVersion = '1.1.1'
implementation "android.arch.lifecycle:viewmodel:$androidArchVersion"
implementation "android.arch.lifecycle:livedata:$androidArchVersion"
annotationProcessor "android.arch.lifecycler:compiler:$androidArchVersion"
testImplementation "android.arch.core:core-testing:$androidArchVersion"
Why can't I access ViewModelProviders?
Include the following as a dependency:
implementation "android.arch.lifecycle:extensions:1.1.1"
The equivalent AndroidX dependency is:
"androidx.lifecycle:lifecycle-extensions:VERSION"
in where VERSION can be replaced for Current Stable, Beta or Alpha values given in this official link
This dependency is for both ViewModel and LiveData and thus would not require you to give separate dependencies for the same either; i.e. the first two dependencies indicated by you can be replaced by the aforementioned lifecycle extensions dependency.
In my case I was missing :
implementation "androidx.fragment:fragment-ktx:1.1.0"
In addition to what Sup suggested, you'll have to correct lifecycler:compiler to lifecycle:compiler - the Gradle sync shouldn't even complete successfully with this typo.
Secondly, the standard android annotation processing ("annotationProcessor") doesn't really work with Kotlin. Instead, use Kotlin's kapt.
At the top of your build.gradle file, add the following:
apply plugin: 'kotlin-kapt'.
And in your dependencies section, replace occurences of annotationProcessor (like the above one) with kapt, e.g.
kapt "android.arch.lifecycle:compiler:1.1.1"
ViewModelProviders is deprecated in 1.1.0
Check this android docs
https://developer.android.com/topic/libraries/architecture/viewmodel
I am using 2.3.1 and add below dependency in build.gradle
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
Declare ViewModel in your Kotlin code as per below:
private val cartViewModel: CartViewModel by viewModels()
Still, if you facing below error like
Unresolved reference: viewModels
Then need to add below below dependency in build.gradle
implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.2.0'
implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.fragment:fragment-ktx:1.3.6'
OR
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
I solve this by doing like this.
implement this dependency
implementation "android.arch.lifecycle:extensions:1.1.1"
then call my ViewModel class
homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
I faced this kind of problem in AndroidStudio 3.0.1 and solved by adding
following dependencies in the proper build.gradle:
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
version code may be different as AndroidStudio tells you if it differs.
If the above "adding/updating dependecies" did not resolve your issue then try to restart your android studio. It´s just to root, I do not see any major issue with it. Hope it helps.
Resolved with this dependency for me
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
If someone want androidx, then, first - in build.gradle (app):
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
Second - in file with code:
import androidx.lifecycle.ViewModelProvider
And third - use it:
ViewModelProvider(this).get(CustomViewModel::class.java)
Or use it like this (if needed ViewModelFactory):
ViewModelProvider(this, CustomViewModelFactory).get(CustomViewModel::class.java)
Not really sure why there are soo many solutions to this problem however while none of these worked for me, I did find a solution in my case.
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
This can also be resolved by targeting minSdkVersion to 21.
If you have minSdkVersion 21 or higher, you won't need implementation "android.arch.lifecycle:extensions:1.1.1".
android {
kotlinOptions {
jvmTarget = '1.8'
}
}
project e app level

Storio using rx instead of reactivex

as title says, one of the imports in a file of the Storio dependency defined in my build.gradle is the following:
import rx.Single
and Android Studio 3.0 shows an error that says:
Cannot resolve symbol 'rx'
First of all, this is the method I'm having problems with:
private void demo1(Demo demo) {
StorIOFactory.get(this)
.put()
.object(demo)
.prepare()
.asRxSingle()
.subscribe();
}
because I'm getting the error 'Cannot resolve method "subscribe"', so when I search the method 'asRxSingle' in 'PreparedPutObject.java' I found this import:
import rx.Single;
when according to this, it should be:
import io.reactivex.Single;
So I guess the issue could be with my app build.gradle
dependencies {
//other things
implementation group: 'io.reactivex.rxjava2', name: 'rxandroid', version: '2.0.1'
implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.5'
implementation 'com.pushtorefresh.storio2:sqlite:2.1.0'
implementation 'com.pushtorefresh.storio2:content-resolver-annotations:2.1.0'
annotationProcessor 'com.pushtorefresh.storio2:content-resolver-annotations-processor:2.1.0'
}
But I keep getting the version of StorIO that imports 'rx.Single'.
Is there a problem with the StorIO version? or with RxJava?
Thanks in advance
RxJava and RxJava2 use different packages to allow mixing them together. Find more here - https://github.com/ReactiveX/RxJava/wiki/What%27s-different-in-2.0#maven-address-and-base-package
So apparently StorIO is using RxJava (version 1).

Android Room Database - Unresolved reference #Entity and other annotations

I am using Android Room Persistence library (v.1.0.0-alpha1) in my app.
Although it is working fine, when I open model class (Kotlin Data class) in Android studio, it shows Unresolved reference for all annotations used for Room database like #Entity, #ColumnInfo etc. I tried changing version of arch library to 1.0.0-alpha5 but result was same.
In Lint inspection it is showing Remove deprecated symbol import for all imported annotations.AS was not showing this error previously.
How can I resolve this issue
Edit
Following are imports I have added in my build.gradle
compile "android.arch.persistence.room:runtime:1.0.0-alpha5"
compile "android.arch.persistence.room:rxjava2:1.0.0-alpha5"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha5"
kapt "android.arch.persistence.room:compiler:1.0.0-alpha5"
Here you have an example.
https://github.com/jsperk/PocRoom
Remember, you need add:
Gradle (Project)--> maven
Gradle (Module App) dependencies -->
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
testImplementation "android.arch.persistence.room:testing:1.0.0"
implementation "android.arch.persistence.room:rxjava2:1.0.0"
In my project, I have this question cause I'm using
android.arch.lifecycle:livedata:1.1.1
than no matter using room with version 1.1.1 or 1.0.0, it still can't find the android.arch.persistence.room.Entity.
I've searched for a long time, till I found that, when I delete the LiveData implementation, the problem solved. Then I notice that, the version of these two libraries conflict. At last, I use the same version of 1.1.0 for livedata and room (since livedata have no version of 1.0.0), and solved it.
def arch_version = "1.1.0"
implementation "android.arch.persistence.room:runtime:$arch_version"
annotationProcessor "android.arch.persistence.room:compiler:$arch_version"
implementation "android.arch.persistence.room:rxjava2:$arch_version"
implementation "android.arch.persistence.room:common:$arch_version"
implementation "android.arch.lifecycle:livedata:$arch_version"
implementation "android.arch.lifecycle:extensions:$arch_version"
Adding these dependencies to your Gradle(Module App) file will solve it:-
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
testImplementation "android.arch.persistence.room:testing:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"

Categories

Resources