Running AspectJ and Firebase Performance Monitoring in Gradle builds - android

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'
[...]

Related

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

Updating OneSignal dependency getting Dependency failing error

I am getting this Error while updating one signal Dependency.
how can I rectify this??
Dependency failing: com.onesignal:OneSignal:3.12.6 -> com.google.android.gms:play-services-base#[10.2.1, 16.1.99], but play-services-base version was 17.1.0.
In your app/build.gradle where you applied the plugin
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
make sure its is before
apply plugin: "com.android.application"
Android studio also gives a warning if the order of the above statements is not correct.
I was missed out updating the classpath in dependencies
dependencies {
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.6, 0.99.99]
}

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.

Kotlin-allopen for android

Is it possible to use kotlin-allopen gradle plugin for android testing with mockito?
I've tried to add kotlin-allopen plugin to my build.gradle and define the annotation.
buildscript {
ext.kotlin_version = '1.0.6'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-allopen'
apply plugin: 'kotlin-android'
allOpen {
annotation("com.mycompany.OpenForTest")
}
And these for annotation itself
annotation class OpenForTest
It's not working for me. Maybe I miss something?
Yes you can.
Because it's a compiler plugin, you'll get all-open code after compilation.
So it should work with tests.
Don't worry.
Edit: according to the comment area, updating the kotlin plugin version seems work. Currently the newest version is 1.2.41.
First add the dependency in your build.gradle (project) file:
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
After that apply the plugin in your build.gradle (app mobdule) file:
apply plugin: 'kotlin-allopen'
Then specify the list of annotations that will make classes open:
allOpen {
annotation('com.example.myproject.OpenForTesting')
}
And use this annotation for every class which you want to be open
#OpenForTesting
Here is the Kotlin official documentation about All-open: https://kotlinlang.org/docs/reference/compiler-plugins.html
Hope this help

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

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'

Categories

Resources