How to use lombok with Kapt3 - java

When I try to run Java/Kotlin android application with Lombok while using Kapt3:
apply plugin: 'kotlin-kapt'
javac compilation fails with numerous
error: cannot find symbol
for generated methods.
It is probably caused by inability of kapt to generate stubs:
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
All aforementioned forces me to use older kapt where I can use:
compileOnly "org.projectlombok:lombok:$lombokVer"
annotationProcessor "org.projectlombok:lombok:$lombokVer"
kapt "org.projectlombok:lombok:$lombokVer"
kapt {
generateStubs = true
}
This however results in fail during test compilation, because I am also using databinding library:
What went wrong:
Execution failed for task ':app:compileXDebugUnitTestJavaWithJavac'.
java.lang.RuntimeException: Failed to parse data binding compiler options. Params:
kapt.annotations : ...\app\build\tmp\kapt\xDebugUnitTest\wrappers\annotations.bscplayDebugUnitTest.txt
kapt.kotlin.generated : ...\app\build\tmp\kapt\xDebugUnitTest\kotlinGenerated
This, on the other hand, forces me to use Kapt3 as described here:
android databinding unit test error Failed to parse data binding compiler options.
Does anyone know how to resolve this issue with Lombok? All I was a hint to use my first solution, but it leads to the databinding problem (as per Kotlin Support · Issue #1169 · rzwitserloot/lombok · GitHub)
Note: Situation is same in Android studio 2.3.3 and in Android Studio 3 (with gradle build tools 3.0.0).

In your build.gradle, add this:
kapt {
keepJavacAnnotationProcessors = true
}
For more info check the JetBrains Kotlin documentation

As was explained by #yanex in comments:
Unfortunately, Kotlin is incompatible with Lombok because it uses the private javac API do to its job. Although kapt3 is built on top of the Java compiler, kapt generates Java stubs for Kotlin classes so what can Lombok process is the stubs, not the original classes. By the way, the original kapt is deprecated and will be removed soon after the Kotlin 1.2 release. So you have some time to migrate to Kotlin & kapt3.

Related

Cannot find symbol error: Lombok 1.18.6 does not work with Gradle 5.2.1, JDK 10

Builds with Gradle 5.2.1 and Lombok 1.18.6 dependency are failing with JDK 10. It seems Lombok annotation are not being processed appropriately. I keep getting "cannot find symbol" error across various Java files in my source. Any thoughts on why this might be happening? I found that a defect has already been created: https://github.com/rzwitserloot/lombok/issues/1572
I am using:
Java JDK 10
Gradle 5.2.1
Lombok 1.18.6
Thanks.
I found the following work around for this issue using a plugin for processing Lombok annotation in compile time.
I had to perform the following steps in build.gradle:
1) Add id "net.ltgt.apt" version "0.15" to plugins section.
2) Add maven { url 'https://projectlombok.org/edge-releases' } to repositories section.
3) Add the following to dependencies section:
compileOnly 'org.projectlombok:lombok:edge-SNAPSHOT'
apt 'org.projectlombok:lombok:edge-SNAPSHOT'
compileOnly 'org.projectlombok:lombok:1.18:6'
annotationProcessor 'org.projectlombok:lombok:1.18:6'
4) Add a task:
tasks.withType(JavaCompile) {
options.annotationProcessorPath = configurations.apt
}
This lets your build complete successfully.
Update 03/29/2019: This workaround also works with Gradle 5.3, Java JDK 10
Thanks.

Failed to resolve project: Android library and Java library module dependency

I'm trying to create a project which includes an Android library and a Java library in Android Studio (3.1). The Java library depends on the Android library. Both are modules in my project like this:
MyProject
|-android
|-java
Both appear in settings.gradle:
include ':android', ':java'
And the Java library depends on the Android library like this:
java (build.gradle):
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':android')
}
...
android (build.gradle):
apply plugin: 'com.android.library'
...
When trying to sync the project I'm getting the following error:
Failed to resolve: project::android
Why?
P.S. The other way around (Android depending on Java) works just fine.
First let's try to fix the build error. Let's run ./gradlew build --stacktrace to see a more detailed output:
Caused by: org.gradle.internal.component.AmbiguousConfigurationSelectionException:
Cannot choose between the following configurations of project :androidLibrary:
debugApiElements
releaseApiElements
AGP is confused which variant to choose. After inspecting this answer, we can find how to overcome the issue:
implementation project(path: ':androidLibrary', configuration: 'default')
After trying to sync the project with that setup you'll see following output in console:
Ignoring dependency of module 'androidLibrary' on module 'javaLibrary'. Java modules cannot depend on Android modules
Thus, seems like what you try to do is not supported by the plugin.
Refer to similar question, where Nick Cardoso tries to clarify the case.

Use a Library in the processor's generated classes

I'm developing a library to generate classes using annotations and processors. The generated classes should use Gson library from google.
My question is : Where should I add the Gson dependency ? I'm currently adding it into the processor build.gradle but when the classes are generated, Gson is not found and Android Studio is suggesting to add it to the app module.
build.gradle of the processor :
implementation project(':lib-annotation')
implementation 'com.squareup:javapoet:1.9.0'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.google.auto.service:auto-service:1.0-rc3'
build.gradle of the app :
implementation project(':lib-annotation')
annotationProcessor project(':lib-processor')
Any help would be greatly appreciated, thanks!
P.S. The project is meant to be a library. I expect the users to only include my library in their gradle file, not the "sub dependency".
Simply using implementation prevent you from using transitive dependency.
The main project can't use or invoke the dependencies you added to your library.
As reminded to docs the implementation configuration should be used to declare dependencies which are internal to the component.
So you have to use api OR compile instead of implementation or add a condition to your transitive library e.g :-
implementation ('com.google.code.gson:gson:2.8.1'){
transitive = true;
}
Besides the fact implementation hides the dependency and therefore keeping it out of the app's path (so if you need to expose Gson, avoid using implementation), the problem is due to being compiling/implementing the library module instead of using a regular aar/jar/apklib package. Try this:
-Add the Android Maven Gradle plugin to the library project and configurate it.
-Using the plugin, compile and install the library into your local maven repository.
-instead of using compile/api/implementation for adding the library to the app, include mavenLocal() into your build.gradle, and then add the library as a regular dependency. All the necessary dependencies should be there.
here's an example of the plugin, in case you need it: https://github.com/fcopardo/EnhancedMapView
follow this link, I have imported without any problems.
http://blog.madadipouya.com/2015/09/21/how-to-add-gson-library-to-android-studio-project/
Okay, I finally fixed the problem!
I had to add the Gson dependency into the processor build.gradle using implementation or compile :
implementation 'com.google.code.gson:gson:2.8.1'
And add the dependency again into the annotation build.gradle using compile :
compile 'com.google.code.gson:gson:2.8.1'
I think it wasn't working because the Gson dependency was in the processor build.gradle which is included using annotationProcessor 'lib-processor' (Somehow the "transitivity" wasn't applying). Therefore I put the Gson dependency in the annotation build.gradle since I include it using implementation 'lib-annotation' and it worked.
Hope it will help

Android Dependency Issue

Trying to run my app, this is an error I'm getting from the Gradle Build:
Error:A problem occurred configuring project ':app'.
Could not resolve all dependencies for configuration ':app:_debugApkCopy'.
Could not find any version that matches com.google.android.gms:play-services-vision:9.4.0.+.
Versions that do not match:
11.0.2
11.0.1
11.0.0
10.2.6
10.2.4
+ 17 more
Required by:
project :app
Here's what my dependencies look like:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// Important - the CameraSource implementation in this project requires version 8.1 or higher.
compile 'com.android.support:support-v4:24.2.0'
compile 'com.google.android.gms:play-services-vision:9.4.0.+'
compile 'com.android.support:design:24.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
I've tried to update the play services through Tools>Android SDK>..
I've read some other posts and people have similar problems, but the fixes don't work. I was also hoping to understand the issue a bit more.
Thanks
Try :
A full clean and rebuild
Uninstalling/installing android support repository
Your specification of "9.4.0.+" is not correct. Maybe you meant something like "9.4.+", but at any rate dynamic dependencies aren't a great idea IMHO. The error message lists the most recent version available on your machine as "11.0.2" (which is the most recent version at the time of writing). Use that instead. According to your comment above, this part of your problem is clearly resolved.
As to your other problem ("support libraries must use the same version"), something (play-services-vision or constraint-layout) most likely pulls in a different version of some part of the support libraries. You should look at this SO question to track down where your dependencies are coming from. You will need to make sure and use compatible versions of everything. In the end, it may suffice to just make sure you have the latest version of each.

Dagger 2, Gradle Plugins and Jack - Guidance Required

I'm using Dagger 2 (first time user of the DI library) with Android Studio (AS 2.1.2 - SDK 24), and obviously my first thought is "What dependencies do I need in order to use it?"
Having looked at examples I can see that you need in the top level build.gradle file you need a annotation processor plugin:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
Now the dependencies in the Module level build.gradle apply the apt plugin for the Module so I end up with something like:
apply plugin: 'com.neenbedankt.android-apt'
// other generic settings - android / buildTypes etc.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
.
.
compile 'com.google.dagger:dagger:2.6'
apt 'com.google.dagger:dagger-compiler:2.6'
provided 'javax.annotation:jsr250-api:1.0'
}
PROBLEM
I have been using, for sometime, the Jack Tool Chain and Java 8 in my projects and found out the above configuration is incompatible with Jack.
Having researched what I needed in order for Jack and Dagger 2 to cooperate I changed the following:
gradle-wrapper.properties :
Changed to :
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
build.gradle (Top Level)
Changed to:
classpath 'com.google.guava:guava:19.0' // added after getting NoSuchMethod Error - it solved the issue
classpath 'com.android.tools.build:gradle:2.2.0-alpha7'
build.gradle (Module Level)
Changed to:
compile 'com.google.dagger:dagger:2.6'
annotationProcessor 'com.google.dagger:dagger-compiler:2.6'
With these changes I was able to use the Jack Tool Chain, and build a small project. However without changing any code I started having trouble with my Component creation - in a simple line of code : testComponent = DaggerTestComponent.builder().build(); the word DaggerTestComponent became red (obviously the Dagger 2 library wasn't working as it should). I managed to resolve the error by a good old invalidate caches, clean and rebuild. However I have been plagued with random errors each time I try and build the small test project - I have managed a couple times to run the app (and it works as expected), but obviously it is quite temperamental.
QUESTION
So the question, and I apologise for it taking this long, but thought that all of the above was relevant,
Is there a more 'Stable' setup than what I'm using at the moment? Has anyone else encountered these problems?
Obviously I don't have to use the Jack tool chain and Java 8, but I'd rather not compromise if I don't have to!

Categories

Resources