Dagger build fails with realm plugin, Cannot find symbol class DaggerApplicationComponent - android

While setting up realm in project by following this documentation.
below is my project level gradle :
buildscript {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// Realm
classpath "io.realm:realm-gradle-plugin:1.2.0"
}
app level gradle :
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'
dependencies {
final DAGGER_VERSION = '2.7'
....
def daggerCompiler = "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
annotationProcessor daggerCompiler
testAnnotationProcessor daggerCompiler
androidTestAnnotationProcessor daggerCompiler
compile "com.google.dagger:dagger:$DAGGER_VERSION"
provided 'org.glassfish:javax.annotation:10.0-b28' //Required by Dagger2
}

This is one of the most serious downsides of Dagger 2 - its error reporting is terrible. I regularly observe these kind of errors, and you can't understand a thing from that message.
The problem is that Dagger 2 pre-processor runs before javac compilation, and if the code couldn't be processed, then pre-processor fails without generating components implementations. To my best knowledge, the pre-processor doesn't report what error caused the failure. Then, when javac compilation executed it fails because it can't find the generated file, but it doesn't proceed to compilation of other files, therefore the only error you see is about missing Dagger component.
What I do in such cases is three stepped solution:
Review VCS history and visit all changed files. Since IDE compiles each file upon visit, I will see if any of them has compilation errors.
If #1 didn't help, I would try to incrementally remove Dagger 2 code from the project (commit beforehand!) and try to recompile on the way. When Dagger 2 is removed from problematic region, Dagger components will be generated and javac comilation will show you the actual errors.
If nothing else helps, I'm visiting all project files one-by-one. Have done it twice already, and it is a nightmare.
I know nothing about Realm, but since I see it uses its own plugin, you could start by changing the order of plugins appliance in build.gradle. Switch these lines:
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'

This answer is based off of Vasiliy's previous answer. I'm promoting my comments to his/her answer to make the solution that worked for me more visible.
As Vasiliy suggested, reversing these lines:
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'
to
apply plugin: 'realm-android'
apply plugin: 'com.neenbedankt.android-apt'
allowed the reporting of an underlying Realm error that was preventing Dagger from generating my application component class (in my case, a breaking change introduced in Realm 2.x).
After addressing this issue, with the reordered apply plugin lines I received this error:
java.lang.IllegalArgumentException: XXX is not part of the schema for this Realm
By reordering the plugins, Realm annotations are not being processed as needed. So the plugin lines must then be restored to:
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'

Related

Running AspectJ and Firebase Performance Monitoring in Gradle builds

Problem statement
I want to use AspectJ in an Android build with Gradle whilst instrumenting the built app with Firebase Performance Monitoring.
Unfortunately the build fails in the DEX merge step with Type android.support.v4.app.INotificationSideChannel$Default is defined multiple times: ...
Findings
In app/build/ajc-transform.log I found:
Wed Jul 22 07:55:14 CEST 2020
ABORT
Exception thrown from AspectJ 1.9.4
This might be logged as a bug already -- find current bugs at
http://bugs.eclipse.org/bugs/buglist.cgi?product=AspectJ&component=Compiler
Bugs for exceptions thrown have titles File:line from the top stack,
e.g., "SomeFile.java:243"
If you don't find the exception below in a bug, please add a new bug
at http://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ
To make the bug a priority, please include a test program
that can reproduce this exception.
Expecting .,<, or ;, but found firebaseperf while unpacking <MessageType:Lcom/google/android/gms/internal/firebase-perf/zzdz<TMessageType;TBuilderType;>;BuilderType:Lcom/google/android/gms/internal/firebase-perf/zzdy<TMessageType;TBuilderType;>;>Ljava/lang/Object;Lcom/google/android/gms/internal/firebase-perf/zzgr;
In the project root there is a ajcore.log:
java.lang.IllegalStateException: Expecting .,<, or ;, but found firebaseperf while unpacking <MessageType:Lcom/google/android/gms/internal/firebase-perf/zzdz<TMessageType;TBuilderType;>;BuilderType:Lcom/google/android/gms/internal/firebase-perf/zzdy<TMessageType;TBuilderType;>;>Ljava/lang/Object;Lcom/google/android/gms/internal/firebase-perf/zzgr;
at org.aspectj.util.GenericSignatureParser.parseClassTypeSignature(GenericSignatureParser.java:204)
at org.aspectj.util.GenericSignatureParser.parseFieldTypeSignature(GenericSignatureParser.java:155)
at org.aspectj.util.GenericSignatureParser.parseFormalTypeParameter(GenericSignatureParser.java:130)
at org.aspectj.util.GenericSignatureParser.parseAsClassSignature(GenericSignatureParser.java:51)
at org.aspectj.weaver.UnresolvedType.forGenericTypeSignature(UnresolvedType.java:275)
at org.aspectj.weaver.bcel.BcelWorld.addSourceObjectType(BcelWorld.java:479)
at org.aspectj.weaver.bcel.BcelWorld.addSourceObjectType(BcelWorld.java:453)
at org.aspectj.weaver.bcel.BcelWeaver.addClassFile(BcelWeaver.java:456)
at org.aspectj.weaver.bcel.BcelWeaver.addClassFile(BcelWeaver.java:480)
at org.aspectj.ajdt.internal.core.builder.AjBuildManager.initBcelWorld(AjBuildManager.java:908)
at org.aspectj.ajdt.internal.core.builder.AjBuildManager.performBuild(AjBuildManager.java:253)
at org.aspectj.ajdt.internal.core.builder.AjBuildManager.batchBuild(AjBuildManager.java:189)
at org.aspectj.ajdt.ajc.AjdtCommand.doCommand(AjdtCommand.java:114)
at org.aspectj.ajdt.ajc.AjdtCommand.runCommand(AjdtCommand.java:60)
at org.aspectj.tools.ajc.Main.run(Main.java:371)
at com.archinamon.api.AspectJWeaver$doWeave$2.invoke(AspectJWeaver.kt:128)
at com.archinamon.api.AspectJWeaver$doWeave$2.invoke(AspectJWeaver.kt:14)
at com.archinamon.api.AspectJWeaver$Companion.runBlocking(AspectJWeaver.kt:182)
at com.archinamon.api.AspectJWeaver.doWeave$android_gradle_aspectj(AspectJWeaver.kt:126)
at com.archinamon.api.transform.AspectJTransform.transform(AspectJTransform.kt:182)
at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:284)
[...]
Used Gradle configuration
Here's a reduced version of my build.gradle files:
project level:
buildscript {
[...]
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
[...]
classpath "com.archinamon:android-gradle-aspectj:4.2.1"
classpath "com.google.gms:google-services:4.3.3"
classpath "com.google.firebase:firebase-crashlytics-gradle:2.2.0"
classpath "com.google.firebase:perf-plugin:1.3.1"
}
}
[...]
app level:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.firebase.firebase-perf'
[...]
apply plugin: 'com.archinamon.aspectj'
[...]
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
[...]
implementation "com.google.firebase:firebase-analytics:17.4.4"
implementation "com.google.firebase:firebase-analytics-ktx:17.4.4"
implementation ("com.google.firebase:firebase-crashlytics:17.1.1"
implementation ("com.google.firebase:firebase-perf:19.0.7"
[...]
}
Investigation
Firebase-perf conflicting with let plugin
https://github.com/firebase/quickstart-android/issues/770
I've tried the approaches mentioned there, but they did not solve my problem.
I found out that the order of the plugins matters. Applying the AspectJ plugin before the Firebase plugins in the app level build.gradle solved my issue:
apply plugin: 'com.android.application'
apply plugin: 'com.archinamon.aspectj'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.firebase.firebase-perf'
[...]

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

How to setup lombok annotationProcessing in modern gradle 4.6 environment?

This is my setup:
AndroidStudio Lombok Plugin is installed
Enable Annotation Processing is checked in AndroidStudio Settings
Gradle wrapper is using version 4.6
I am using kotlin version 1.2.71
I am using com.android.tools.build:gradle:3.2.1
I tried "Invalidate cache / Restart AndroidStudio" after each change
Lombok dependencies are defined like this:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
dependencies {
compileOnly "org.projectlombok:lombok:1.18.2"
kapt "org.projectlombok:lombok:1.18.2"
}
When I build the app, I get the followin error:
Annotation processors must be explicitly declared now.
The following dependencies on the compile classpath are found to
contain annotation processor.
Please add them to the annotationProcessor configuration.
- lombok-1.18.2.jar (org.projectlombok:lombok:1.18.2)
I also tried this dependency setup:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
dependencies {
compileOnly "org.projectlombok:lombok:1.18.2"
annotationProcessor "org.projectlombok:lombok:1.18.2"
}
But then I get "cannot find symbol"-errors all over the place, because no getters/setters are generated by lombok.
Annotation processors must be explicitly declared now. The following
dependencies on the compile classpath are found to contain annotation
processor.
Please add them to the annotationProcessor configuration.
- lombok-1.18.2.jar (org.projectlombok:lombok:1.18.2)
Try adding it as annotationProcessor:
annotationProcessor 'org.projectlombok:lombok:1.18.2'
However, read this: Is it possible to use Lombok with Kotlin?
Lombok does not run on your source code, but on the AST. Anyway, it is
an annotation processor that is run at compile-time by the Java
compiler. The Kotlin compiler does not use these annotation
processors. See also the answer
https://stackoverflow.com/a/35530223/2621917 straight from the horse’s
mouth.

Exclude a Gradle dependency that is added by the Realm plugin

Sporadically, my Gradle syncs will fail. I'll be given the unhelpful message that "a 3rd party Gradle plugin" may be the cause. If I open up the Event Log, I'll see the message:
Outdated Kotlin Runtime
Your version of Kotlin runtime in 'Gradle: org.jetbrains.kotlin:kotlin-stdlib:1.2.10#jar' library is 1.2.10-release-109 (1.2.10), while plugin version is 1.2.51-release-Studio3.1-1.
Runtime library should be updated to avoid compatibility problems.
The mismatched number is neither the Kotlin version in my Gradle files, or the version of my Kotlin plugin in Android Studio.
After running a Gradle dependency tree, I found the culprit:
+--- io.realm:realm-android-kotlin-extensions:5.1.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10 -> 1.2.51
I'm not including any realm-android-kotlin-extensions library. I'm assuming that it's added by classpath "io.realm:realm-gradle-plugin:5.1.0" and apply plugin: "realm-android"
This makes matters difficult. If it were a regular dependency, I could try something like
implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
transitive = false
}
or
implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
}
Theoretically that would probably work. It would be forced to use the newer version of Kotlin, the error would go away, and hopefully Gradle syncs would work and all would be well with the world. But if I try this approach my Gradle sync fails and I get this error in the Event Log:
Gradle sync failed: Could not find method io.realm:realm-android-kotlin-extensions:5.1.0() for arguments [build_2krw7i3nwfkd5lrq1ly9b8huw$_run_closure3$_closure29#7b5b2081] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I'm assuming that's because this isn't a dependency I added (perhaps not a public dependency?), that it fails because the dependency is added via the plugin instead of directly in my Gradle file.
So how do I fix this? Perhaps there's a line I can add to tell the Realm plugin to exclude the obsolete dependency? Or am I completely barking up the wrong tree and the solution for my Kotlin version clash issue is something else entirely?
(BTW, if you're wondering why I'm using Realm 5.1.0, 5.3.1 causes some weird bugs in our app, so we're waiting for a later version to be released in the hope that will no longer cause the issues.)
One of the things on my personal "I wish I had known this a year ago" list is that you can manually add what Realm adds to your project instead of relying on the Gradle plugin.
buildscript {
ext.kotlin_version = '1.2.51'
ext.realm_version = '5.3.1'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "io.realm:realm-transformer:5.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
import io.realm.transformer.RealmTransformer
android.registerTransform(new RealmTransformer(project))
dependencies {
implementation "io.realm:realm-annotations:$realm_version"
implementation "io.realm:realm-android-library:$realm_version"
implementation "io.realm:realm-android-kotlin-extensions:$realm_version" {
exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
}
kapt "io.realm:realm-annotations-processor:$realm_version"
}
As per docs.

'Unresolved reference' errors for android library module referenced in app module

I have problems referencing my android library modules in my projects. Beside the main app module I use to have an android library module with either util stuff or as data module. I reference it in app module like that:
dependencies {
implementation project(":data")
}
When I build the project, it´s giving me lot of error messages 'Unresolved reference: ...' for all stuff that I reference in the app module to the android library module. But the IDE itself doesn´t have a problem, Intelligent finds all classes, interfaces etc., imports are fine, nothing is red. The android library module itself builds and creates aar-file in the output. It´s the compileDebugKotlin task that fails
Any general idea what may be related to that?
Found the problem, my android library module was missing the kotlin configuration:
apply plugin: 'kotlin-android'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion:<version>"
}
Although I used kotlin .kt files in it, it could build without and also
Tools -> Kotlin -> 'Configure Kotlin in projects'
had told me 'All modules with Kotlin files are configured'
Your module's build.gradle file should have:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
...
}
In my case It was apply plugin: 'kotlin-android',
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
and also added it on build.gradle
androidExtensions {
experimental = true
}
If it is a kotlin module make sure to add in its build.gradle file
apply plugin: 'kotlin'
I was using CoroutineWorkers and in my case I had to add work-runtime-ktx dependency to use it
implementation "androidx.work:work-runtime-ktx:2.4.0"

Categories

Resources