Difficulty using Moshi's Kotlin codegen - android

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").

Related

Can you use Hilt in a Java-only Android project?

According to the official integration guide, you need to add
plugins {
id 'kotlin-kapt'
...
}
and
dependencies {
implementation "com.google.dagger:hilt-android:{hilt_version}"
kapt "com.google.dagger:hilt-compiler:{hilt_version}"
}
to your build.grade file. However, when I do a gralde sync, I get the following error:
Plugin [id: 'kotlin-kapt'] was not found in any of the following
sources:
is it possible to use Dagger-Hilt in a pure java project? Or do you have to either use plain Dagger or use Kotlin?
Yes, you can use the following instead of kotlin-kapt:
dependencies {
implementation "com.google.dagger:hilt-android:{hilt_version}"
annotationProcessor 'com.google.dagger:hilt-compiler:{hilt_version}'
}
plugins {
id 'dagger.hilt.android.plugin'
}

My Android app builds failed Hilt, WorkManager

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...

Unresolved reference: databinding

I'm using Android Studio Beta 1 with Android Gradle Plugin 3.0.0-beta1 and Kotlin Plugin 1.1.3-2. I also have data binding enabled:
dataBinding {
enabled = true
}
But unfortunately Kotlin classes don't see generated classes from layouts, cause I get errors like this one:
Error:(17, 31) Unresolved reference: databinding
Error:(39, 36) Unresolved reference: MyFragmentBinding
Of course Java classes see these generated classes.
Try adding kapt and apply the plugin in your module level .gradle file
apply plugin: 'kotlin-kapt'
//..
dependencies {
//..
kapt 'com.android.databinding:compiler:2.3.2'
//..
}
Add this dependency in gradle for Kotlin to work with databinding
kapt "com.android.databinding:compiler:2.3.3"

Android Dagger 2.11 with Kotlin, ContributesAndroidInjector Annotation issue

I'm using Dagger 2.11 with Kotlin. Everything is fine with Dagger but when i add ContributesAndroidInjector annotation to project i get this error:
e:
...build/tmp/kapt3/stubs/devDebug/com/raqun/android/di/AppComponent.java:6: error: 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.
e:
e: public abstract interface AppComponent extends dagger.android.AndroidInjector<MyApp> {
e:
Here're the dependencies i use:
$rootProject.ext.daggerVersion = 2.11
compile "com.google.dagger:dagger-android:$rootProject.ext.daggerVersion"
compile "com.google.dagger:dagger-android-support:$rootProject.ext.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
I already added:
kapt {
generateStubs = true
}
and
apply plugin: 'kotlin-kapt'
What i'm missing or doing wrong?
Thanks for your help.
Not: I already tried cleaning gradle and re-building project.
The problem is about my dependencies. Here're the working dependencies for Dagger 2.11 and Kotlin.
compile "com.google.dagger:dagger-android-support:$rootProject.ext.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"
Thanks all for help.

Dagger 2 component not generated

In my module, in my base Application class
component = DaggerCompClassComponent.builder()
.classModule(new ModuleClass()).build();
it can not find DaggerCompClassComponent.
I have on module build.gradle
apply plugin: 'com.neenbedankt.android-apt'
.........................
apt 'com.google.dagger:dagger-compiler:2.8'
compile 'com.google.dagger:dagger:2.8'
provided 'javax.annotation:jsr250-api:1.0'
and in Project build.gradle,
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
I have done build / rebuild / clean / restart project. I have a Component class where I inject objects and a ModuleClass where I provide objects to inject.
What can be the cause for not generating Dagger Component . class ?
EDIT:
This is my ModuleClass, adnotated with #Module:
#Provides
#Singleton
public Interceptor provideInterceptor() {
return new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("AppName-Android", BuildConfig.VERSION_NAME + "-" + BuildConfig.VERSION_CODE)
.addHeader("Content-Type", "application/json");
return chain.proceed(builder.build());
}
};
}
#Provides
#Singleton
OkHttpClient provideOkHttpClient(Interceptor interceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(interceptor);
return builder.build();
}
#Provides
#Singleton
Retrofit provideRetrofit(OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(BaseApplication.getRes().getString(R.string.api_base_url))
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
#Provides
#Singleton
WebServiceCall provideWebService(Retrofit retrofit) {
return retrofit.create(WebServiceCall.class);
}
And this is my Component Class:
#Component(modules = ModuleClass.class)
#Singleton
public interface ComponentClass {
void inject(Interceptor o);
void inject(OkHttpClient o);
void inject(Retrofit o);
void inject(WebServiceCall o);
}
When developing on Kotlin, you should add the following lines next to their annotationProcessor counterparts:
kapt 'com.google.dagger:dagger-android-processor:2.15'
kapt 'com.google.dagger:dagger-compiler:2.15'
and add apply plugin: 'kotlin-kapt' at the start of the same file.
That section looks like this for me:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt' // <- Add this line
apply plugin: 'io.fabric'
Update (March 29, 2020)
Inside your app-level build.gradle inside dependencies block, add these lines:
//dagger2
api 'com.google.dagger:dagger:2.24'
api 'com.google.dagger:dagger-android:2.24'
api 'com.google.dagger:dagger-android-support:2.24'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
kapt 'com.google.dagger:dagger-compiler:2.24'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'
kapt 'com.google.dagger:dagger-android-processor:2.24'
compileOnly 'javax.annotation:jsr250-api:1.0'
implementation 'javax.inject:javax.inject:1'
Inside android block of app-level build.gradle,
kapt {
generateStubs = true
}
At the top of the app-level build.gradle, Do this in exactly below order.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
Finally, You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Settings for New Projects>search"Annotation processor"
After this, do from Menu Build > Rebuild. You are done!
Test:
#Component
public interface ApplicationComponent {
}
Now, you can use DaggerApplicationComponent that was generated at compile-time for your ApplicationComponent interface.
public class MyApplication extends Application {
ApplicationComponent applicationComponent = DaggerApplicationComponent.create();
}
Maybe you forgot to annotate ModuleClass with #Module ?
If you have several modules in your AndroidStudio (modules in terms of Android Studio, not Dagger), another possible reason of fail is that you've forgot to put annotation processors into the all modules' build.gradle.
We've divided our app into several modules, updated dependencies from using implementation to using api but forgot to handle annotation processors accordingly.
So, you can have this lines only in a root module:
api 'com.google.dagger:dagger-android:2.16'
// if you use the support libraries
api 'com.google.dagger:dagger-android-support:2.16'
But this ones should be specified in all modules dependencies:
annotationProcessor 'com.google.dagger:dagger-compiler:2.16'
// if you use injections to Android classes
annotationProcessor 'com.google.dagger:dagger-android-processor:2.16'
If Dagger2 can not generate its components it means that your code have some errors with Scopes/Modules. Check our your #Provides/Inject methods.
UPD:
You should inject your components into cases where you need instances of classes provided by module.
like
inject(MainActivity main);
In build.gradle's app, you need to add necessary dependencies for Dagger to generate corresponding classes, as below:
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
you should change dagger version which you are using.
refer full dagger document in this
There are some minor misconceptions/faults in your code above, here's a working implementation:
Application.java:
component = DaggerComponentClass.builder().classModule(new ModuleClass()).build();
The generated class will be named DaggerComponentClass, not DaggerCompClassComponent. If you can't run your app in Android Studio to get it built, try Build->Clean project and Build->Rebuild project in the menu. If everything is OK Dagger will have compiled DaggerComponentClass which will be located in the same package as ComponentClass.
ComponentClass.java:
#Component(modules = ModuleClass.class)
public interface ComponentClass {
void inject(AClassThatShouldGetInstancesInjected instance);
}
A Component in Dagger2 has methods named inject that receive the instance to get instances injected into it, not the other way around. In the code above the class AClassThatShouldGetInstancesInjected will typically call componentClass.inject(this); to get instances injected into itself.
ModuleClass.java:
#Module
public class ModuleClass {
#Provides
#Singleton
public Interceptor provideInterceptor() {/*code*/}
//Your Providers...
}
The Module is correct in your code, make sure its annotated.
If you are using Kotlin, make sure to add kapt (Kotlin annotation processor) Gradle plugin to your build script and use kapt Gradle dependency type instead of annotationProcessor for Dagger Compiler.
apply plugin: 'kotlin-kapt
kapt deps.daggercompiler
implementation deps.dagger
For anyone doing this in the year 2022 on Android Studio, if you want to just use some Dagger capabilities, you only need to add this in your Gradle dependencies:
implementation 'com.google.dagger:dagger:2.41'
kapt 'com.google.dagger:dagger-compiler:2.41'
then this on your plugins:
id 'kotlin-kapt'
You don't need to add anything else.
Now here's the key step: You need to do a clean (either in Gradle or just select Clean Project under Build menu) because for some reason incremental compilation is getting in the way.
Then rebuild your project.
If you created your Dagger components correctly you should see compilation errors at this point, or if you created them correctly the auto-completion with your component(s) should now work in the code editor.
api 'com.google.dagger:dagger-android:2.28.3'
api 'com.google.dagger:dagger-android-support:2.28.3'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.28.3'
Replace above with below dependencies in app level dependencies, if you have used above
api 'com.google.dagger:dagger:2.28.3'
annotationProcessor 'com.google.dagger:dagger-compiler:2.28.3'
I solved it by editing my dagger dependencies to this:
implementation "com.google.dagger:dagger:${versions.dagger}"
kapt "com.google.dagger:dagger-compiler:${versions.dagger}"
As Accepted Answer does NOT work for me:
This issue is related to Android Studio (V4.1.2 and up) and you can fix it by doing the below steps:
Pref -> Editor -> File Types -> Ignore Files And Folders -> Remove "Dagger*.java;"
Also If you see other Dagger regexs, remove them too.
Now android studio will find generated component class.
In my case it wasn't being created only in Instrumentation Testing, because I was missing
kaptAndroidTest "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
Of course I'm using Kotlin and $rootProject.daggerVersion is a property on my base build file. You can replace for whatever version or gradle dependency you desire and are missing.
Try this worked for me with Kotlin. Just this much in build.gradle (Module YourProject.app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
dependencies {
.....
implementation 'com.google.dagger:dagger-android:2.35.1'
kapt 'com.google.dagger:dagger-compiler:2.15'
implementation 'javax.inject:javax.inject:1'
.....
}
//add all the dependencies otherwise component class will not be generated.
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'

Categories

Resources