Dagger 2, Gradle Plugins and Jack - Guidance Required - java

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!

Related

IntelliJ with gradle gives inconsistency in compilation

I'm writing Minecraft Plugin using IntelliJ IDEA Ultimate with gradle. I have added dependency org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT as compileOnly. During development, I noticed that gradle compiles my code in different way than IntelliJ does. For example, IntelliJ was unable to accept addPassenger on Boat, but gradle compiled it. In the opposite way, if I changed it into setPassenger, IntelliJ didn't mark it as error, but gradle failed to compile. I tried to invalidate caches, reimport, clean, even remove %userprofile%\.gradle directory, nothing helped. As a POC I changed compileOnly to compile and it worked well, IntelliJ and gradle compilation results were consistent. What's the reason?
Ok, I found the solution (and forgot about this question).
I had been using multiple dependencies, and one load another with older version that I loaded implicitly in my build.gradle. However, they weren't exactly the same dependencies, but parallel ones. So gradle could not choose higher version of one dependency. Solution was to exclude this one explicitly loaded dependency and everything worked well.
Before:
dependencies {
compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.0.1'
compileOnly group: 'org.spigotmc', name:'spigot-api', version: '1.15.1-R0.1-SNAPSHOT'
}
After:
dependencies {
compileOnly('com.sk89q.worldedit:worldedit-bukkit:7.0.1') {
exclude `org.bukkit:bukkit:1.15.1-R0.1-SNAPSHOT`
}
compileOnly group: 'org.spigotmc', name:'spigot-api', version: '1.15.1-R0.1-SNAPSHOT'
}

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.

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.

multiple dex files define landroid/support/annotation/AnimRes

The moment I added the android support annotations to my dependencies
compile 'com.android.support:support-annotations:20.0.0'
I got this error:
Error Code:
2 Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
build.gradle
android {
compileSdkVersion 19
buildToolsVersion '20.0.0'
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
}
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.crashlytics.android:crashlytics:1.+'
compile 'com.android.support:support-annotations:20.0.0'
}
Anybody else experienced this issue? I have tried the solutions from here.
The problem is that android-support-annotations.jar used to be a separate library containing the android annotations, but for some reason these annotations are already included in recent versions of the android-support-v4.jar file.
Deleting the annotations jar solved the issue.
Build->clean Project ,and it worked
I deleted the android-support-v4.jar and it worked.
If this is cordova / ionic project this worked for me
add these line to build.gradle under platforms/android after line number 22 i.e after apply plugin: 'android'
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
Solved this exact issue in a Cordova project that used the facebook plugin. I was able to successfully build by commenting out this line from platforms\android\project.properties, as shown:
# cordova.system.library.1=com.android.support:support-v4:+
And by commenting out this line from platforms\android\build.gradle, as shown:
// compile "com.android.support:support-v4:+"
Then doing the build. The problem started when I installed (katzer/cordova-plugin-local-notifications) which added these lines, but it created a conflict since the library it was adding to the build was already part of the facebook plugin build.
As other users said, the first elements to troubleshoot are dependencies. Although, sometimes you can struggle for hours and you don't find any problem so you can focus on the build process instead.
Changing the way in which the .dex files are produced sometimes solves the problem. You can go through these steps:
Open your Build.gradle (app) file
Search for the task dexOptions
Change it to:
dexOptions {
incremental false
}
If you don't find the task in your file then you can add it.
For me the reason was the new data-binding lib
com.android.databinding:dataBinder:1.0-rc2
it somehow used a conflicting version of the annotations lib, which I could not force with
configurations.all {
resolutionStrategy {
force group: 'com.android.support', name: 'support-v4', version: '23.1.0'
force group: 'com.android.support', name: 'appcompat-v7', version: '23.1.0'
force group: 'com.android.support', name: 'support-annotations', version: '23.1.0'
}
}
but the new rc3 and rc4 versions seem to have fixed it, so just use those versions
I had the same problem , but i deleted build files from the build folder
projectname/app/build
and it removed all the related error. "can't clean the project" and also "dex errow with $anim"
I managed to fix this issue. The reason was that I included the android support library 19.0.0 as a dependency, but 19.1.0 is required. See here for more information
So it has to be
dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile 'com.crashlytics.android:crashlytics:1.+'
compile 'com.android.support:support-annotations:20.0.0'
}
If you import AppCompat as a library project and you also have android-support-annotations.jar in libs elsewhere, make sure to import everywhere AppCompat library only (it already includes this annotations lib). Then delete all android-support-annotations.jar to avoid merging multiple versions of this library.
Updating Android SDK Tools fixed it for me, now it just sees the copy in android-support-v4.jar.
I had the same problem when using ant, and the annotations library was being included automatically by an outdated sdk.dir/tools/ant/build.xml.
Clean project works as a temporary fix, but the issue will reappear on next compilation error.
To fix more reliably, I had to update the dependency to android support-v4 to com.android.support:support-v4:22.2.0.
Put in your build.gradle the dependency of support-annotations according with your compileSdkVersion. For instance: A project with the compileSdkVersion 25 you can put the following dependence:
compile 'com.android.support:support-annotations:25.0.1'
This will solve your problem.
In my case I had a file called cache.xml under /build/intermediates/dex-cache/cache.xml in the root project folder. I deleted this file, rebuild the project and it worked for me.
I deleted the android-support-v4.jar and it worked.
Explain - android-support-v4.jar is conflicting with my other .jar files of project\libs files ** specially when you are running with java 8 on AS.
Put android-support-v4.jar in your libs folder in eclipse. Clean and build the project. It will resolve the issue.
Another reason that messages such as these can come up in Android Studio when building and launching can be the cause of application tags in your libraries.
If you have several Android Library projects that you imported as modules. Go into those projects and remove the <application> ... </application> tags and everything between them. These can cause issues in the build process along with the support library issues already mentioned.
From /platforms/android/libs/
delete android-support-v4.jar.
It works for me.

Categories

Resources