Dagger "cannot access SomeComponent" during build - android

I started a new project using Dagger 2. The module structure is as following (→ meaning depends on with implement):
app → data → network
Each module has it's own Dagger component (e.g. AppComponent, DataComponent and NetworkComponent).
#Component(modules=[...])
interface NetworkComponent
#Component(dependencies=[NetworkComponent::class], modules=[...])
interface DataComponent
#Component(dependencies=[DataComponent::class], modules=[...])
interface AppComponent
The idea behind this setup is that the app module has no access to network but only knows data. The real structure is more complicated but this is the main principle.
While Android Studio is very happy with this setup, gradle won't compile it.
The error is:
> Task :app:kaptReleaseKotlin FAILED
error: cannot access NetworkComponent
class file for com.somepackage.network.di.NetworkComponent not found
* What went wrong:
Execution failed for task ':app:kaptReleaseKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
So while compiling the app module it fails to resolve the class it has no access to, but this class is nowhere referenced in the source of the app module. Dagger seems to internally trying to access it.
If I add a depency app → network the app compiles and runs fine.
Any idea how to solve this WHILE keeping app from accessing network? This separation is quite important for us.

Related

After merging two branch and then rebuilding I am getting these errors related to Dagger/missing binding

I am working on a client project via GitHub, there is two branches let's say branch 1 and branch 2, and both are working perfectly separately, but when I merge branch 1 into branch 2 and resolve all conflicts and then after resolving all conflicts, i clean the project, sync it and then try to rebuild it then it is throwing some dagger missing error, i have tried everything but unable to solve it, as its a multi-module project, these are the following errors :-
D:\3.4)clientmainworkgithub\04)github_holedo_dev9_1pm_15may2022\matrix-sdk-android\build\tmp\kapt3\stubs\debug\org\matrix\android\sdk\internal\session\SessionComponent.java:8: error: [Dagger/MissingBinding] error.NonExistentClass cannot be provided without an #Inject constructor or an #Provides-annotated method.
public abstract interface SessionComponent {
^
error.NonExistentClass is injected at
org.matrix.android.sdk.internal.crypto.crosssigning.DefaultCrossSigningService(…, outgoingKeyRequestManager, …)
org.matrix.android.sdk.internal.crypto.crosssigning.DefaultCrossSigningService is injected at
org.matrix.android.sdk.internal.crypto.crosssigning.UpdateTrustWorker.crossSigningService
org.matrix.android.sdk.internal.crypto.crosssigning.UpdateTrustWorker is injected at
org.matrix.android.sdk.internal.session.SessionComponent.inject(org.matrix.android.sdk.internal.crypto.crosssigning.UpdateTrustWorker)
The following other entry points also depend on it:
org.matrix.android.sdk.internal.session.SessionComponent.session()
org.matrix.android.sdk.internal.session.SessionComponent.syncTask()
org.matrix.android.sdk.internal.session.SessionComponent.inject(org.matrix.android.sdk.internal.session.room.send.SendEventWorker)
org.matrix.android.sdk.internal.session.SessionComponent.inject(org.matrix.android.sdk.internal.session.room.send.MultipleEventSendingDispatcherWorker)
org.matrix.android.sdk.internal.session.SessionComponent.inject(org.matrix.android.sdk.internal.session.content.UploadContentWorker)
org.matrix.android.sdk.internal.session.SessionComponent.inject(org.matrix.android.sdk.internal.session.sync.job.SyncWorker)
org.matrix.android.sdk.internal.session.SessionComponent.inject(org.matrix.android.sdk.internal.crypto.verification.SendVerificationMessageWorker)

Failed to find code-generated model provider - AWS Amplify

I'm getting the below error:
Failed to find a code-generated model provider.
AWS amplify code which throwing this error:
Amplify.addPlugin(new AWSApiPlugin());
Amplify.addPlugin(new AWSDataStorePlugin());
Amplify.configure(context);
I am following the below tutorials:
https://docs.amplify.aws/start/getting-started/generate-model/q/integration/android
https://docs.amplify.aws/cli/graphql-transformer/overview
I have tried generating models and models get generated successfully but still while running the app I am getting above exception.
When you generate models, you should expect to find various code-generated files in your application project. One of them will be app/src/main/java/com/amplifyframework/datastore/generated/model/AmplifyModelProvider.java.
When you build your app, Android Studio will compile that java file into a class file, and include it into your binary.
At runtime, on the phone, the AWSDataStorePlugin() constructor will attempt to find that same AmplifyModelProvider by means of reflection.
I would verify that:
You do actually have the code-generated AmplifyModelProvider;
It is being built successfully;
It is not being stripped out by ProGuard/R8 minification.
If you're still not able to get it working, just use the one-argument version of the AWSDataStorePlugin(...) constructor, instead. That version allows you to explicitly specify the model provider, and does not use runtime reflection.
Amplify.addPlugin(AWSDataStorePlugin(AmplifyModelProvider.getInstance()))
Your datasource needs to be updated:
Try running modelGen, then amplifyPush tasks:
Category
Resource name
Operation
Provider plugin
Api
amplifyDatasource
Update
awscloudformation

Dagger 2.21+ unable to generate UnitTest Component in android

Let's start with an issue:
> Task :app:kaptAppDebugUnitTestKotlin FAILED
/app/build/tmp/kapt3/stubs/appDebugUnitTest/com/pckg/TestAppComponent.java:77: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor 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.
public abstract class TestAppComponent extends com.pckg.AppComponent {
^warning: The following options were not recognized by any processor: '[room.schemaLocation, kapt.kotlin.generated, room.incremental]'[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: android.databinding.annotationprocessor.ProcessDataBinding (DYNAMIC).
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptAppDebugUnitTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
I'm trying to update our huge project to run with incremental kapt. One of the essentials was to update dagger. I tried a lot version, but the last one working is 2.20, everything above gives the mentioned 'error'.
To be honest, I don't see any error. The build works fine, when I assemble only app, but when I try to run the UnitTest task, it shows me that error. But I'm unable to find any issue, nor the AS code inspections in AppComponent. The TestAppComponent is not even generated.
I believe, we use completely regular setup of local unit tests for android.
Setup:
/core/src/main/com/pckg/core/Provisions.kt
interface Provisions {
....
}
/app/src/main/com/pckg/AppComponent.kt
#Component(modules=[....])
#Singleton
interface AppComponent : Provisions {
....
}
/app/src/test/com/pckg/TestAppComponent.kt
#Component(modules=[....])
#Singleton
interface TestAppComponent : AppComponent {
....
}
I also tried to make the Components to be abstract classes instead of interface (because the error says that class extends the interface, but without luck - same issue, just with abstract classes).
Of course I did try to run with --stacktrace, but it's just longer pointless exception.
Questions:
Do you know what needs to be changed when updating to dagger 2.21 and
above?
Do you know how to force dagger/kapt/gradle to say more than
(no error message)?
PS: Whatever library version you might think of is the latest. AGP 3.5.0, Gradle 5.5.1, Kotlin 1.2.50,...
I remember seeing something like this when I was trying to add the test component to the android tests. After adding the following to build.gradle the component was generated (originally I only had dagger in the kapt directive):
kaptAndroidTest "com.google.dagger:dagger-android-processor:$dagger_version"
kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger_version"
If you're doing that for unit tests, adjust accordingly.
Note: If you're only seeing it when using incremental processing, then maybe it's some other configuration option you need to enable specifically for the unit test?
Turns out, the problem was in missing dependencies in tests.
In our case it was due to conflict with FindBugs.
We have defined FB as:
compileOnly "com.google.code.findbugs:annotations:3.0.1"
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
So if we have a class with suppress:
public class JavaClass {
#Inject
#SuppressFBWarnings(value = "THIS_CRASHES_DAGGER",
justification = "Since this annotation is not available in test classpath, dagger will fail.")
Context mInjectedContext;
}
The #SuppressFBWarnings is not available in tests. This was ok up to dagger 2.20. But every version after is failing on that, because the annotation cannot be resolved. And dagger is trying to read other annotations to report you badly used annotations, etc.
The fix is easy:
testCompileOnly "com.google.code.findbugs:annotations:3.0.1"
testCompileOnly "com.google.code.findbugs:jsr305:3.0.2"
or making it implementation might ensure propagation to the tests also.
You can find more about this here: https://github.com/google/dagger/issues/1599

Dokka failure: com.intellij.psi.impl.source.PsiClassImpl cannot be cast to com.intellij.psi.PsiJavaFile

I have a build that generates Android artefacts (mainly an .aar) and bundles in dokka-generated docs (codebase is Kotlin+Java).
With some recent changes dokka started failing with this cryptic error:
> Task :mylib:dokka FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':mylib:dokka'.
> com.intellij.psi.impl.source.PsiClassImpl cannot be cast to com.intellij.psi.PsiJavaFile
Any help would be appreciated. I currently cannot seem to trace the source of this.
I can't pin-point the root of the problem but I can say for sure that Dokka has some issues with annotation classes.
In my changes, I had initially introduced a new annotation class based on Android's #IntDef inside an abstract class (alongside associated helper methods), written in Java.
These 2 steps seemed to have helped:
Switching to Kotlin for the annotation class' implementation, then defining the annotation class at a file's root scope (rather than as an inner item of a class).
Suppressing the annotation's package in Dokka's config, i.e. -
dokka {
...
packageOptions {
prefix = "com.mylib.myannotation"
suppress = true
}
}
The end result is that the documentation cannot reference the annotated class (that's no surprise, as it was suppressed), but the javadoc generation fully succeeded.

Error:Execution failed for task ':backend:appengineEndpointsGetClientLibs

When adding a Google Cloud Endpoint backend to a Android app using the Google App Engine Java Module Template, two classes (amongst other things) are added to the project.
We are then encouraged to modify these classes to our own requirements. So, I decided to delete the model class (MyBean) and the endpoint class (MyEndpoint) and replace them with my own classes. When I tried to rebuild/clean the project, I get the error message:
Error:Execution failed for task ':backend:appengineEndpointsGetClientLibs'.
There was an error running endpoints command get-client-lib:
<package-name>.MyEndpoint
I looked for a couple of hours for a solution to this problem until I found the cause.So I decided to share it since this question does not even exist on SO (Please correct me if I am wrong)
Go to backend\src\main\webapp\WEB-INF\web.xml
In the web.xml file, look for the init-param and change the parameter value to the new endpoint class you've just added.
<init-param>
<param-name>services</param-name>
<param-value>com.mycompany.backend.New-Endpoint-Class-Name</param-value>
</init-param>
Rebuild the project and everything should be just fine.

Categories

Resources