gradle dependency error in android - android

In the following build.gradle, I added the configuration section to avoid double inclusion of support libraries. Support libraries are used in the main project and in the dependent projects like facebook sdk. Without the configuration section, I get "UNEXPECTED TOP-LEVEL EXCEPTION". Adding that configuration makes the error go away and the app all works fine.
Now, I'm trying to add RecyclerView to my app and I get RecyclerView class not found while inflating the recyclerview (although it builds ok). If I remove the facebook SDK and configuration section, the recyclerview works just fine.
Question: What changes can I make to the build.gradle to make the facebook SDK work and RecyclerView work? In other words, why is the config section excluding v7 when it is only supposed to exclude v4?
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:+'
compile 'com.android.support:support-v13:+'
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.google.android.gms:play-services:4.4.52'
compile project(':facebook-3.15')
compile project(':parse-1.5.1')
compile project(':viewpagerindicator-2.4.1')
}
configurations {
// to avoid double inclusion of support libraries
all*.exclude group: 'com.android.support', module: 'support-v4'
}

If you're having a dependency conflict with the v4 support library, you can just exclude it from one of the libraries via the gradle script:
compile ('com.android.support:recyclerview-v7:+') {
exclude module: 'support-v4'
}

I fixed that adding:
compile ('com.facebook.android:facebook-android-sdk:3.22.0#aar'){
exclude module: 'support-v4'
}

Found a solution to this:
Removed the config section in the build.gradle that excludes support-v4
It turns out that .aar files are basically a zip, so removed the support-v4 jar from the dependency .aar library (using 7-zip).
And now, I don't get the top-level exception and at the same time able to use recyclerview-v7.
If you are using dependency projects instead of .aar files, try removing the support-v4.jar files in the dependency projects before compiling.
Shouldn't the gradle build tool be intelligent enough to exclude the duplicate packages rather than having the users go thru this kind of workarounds and headaches?

Related

Not able to remove transitive dependency from gradle in an Android Project

I am trying to remove the transitive dependencies from my Android project and for some reason exclude is not working when i try to remove a dependency from my particular dependency.
For example i want to remove support-annotations from my project
if i use
configurations {
all*.exclude group: 'com.android.support', module:'support-annotations'
}
The dependency gets excluded and from the dependency tree. i can see the dependency tree by using ./gradlew app:dependencies
But if i use
compile('com.android.support:support-v4:23.4.0')
{
exclude group: 'com.android.support', module:'support-annotations'
}
Then i still see the dependency in the dependency tree.
So my question is that why is it not working when i try to remove the dependency from a particular dependency ?
Update 1:
Also can anyone tell me what does the star (*) symbol next to dependency in the tree represent ?
Update 2
I am also using Fresco I tried the same thing with Fresco and exclude rule seems to work for it
Dependency Tree of Fresco
Dependency Tree when i exclude imagepipeline-base in Fresco
compile("com.facebook.fresco:fresco:0.9.0")
{
exclude group: 'com.facebook.fresco', module: 'imagepipeline-base'
}
As you can see the imagepipeline-base dependency gets excluded. So i don't know why this doesn't work for Android Support Annotations transitive dependency
So i have figured this out with the help of one of my friends. The reason why i was not able to remove support-annotation was because most of the other dependencies were using support-v4 as transitive dependency and those support-v4 also had their own copy of support-annotation.
Now there are 2 solutions to this
Solution 1:
exclude support-annotation from all the dependencies that containsupport-v4 as transitive dependency.
Solution 2:
exclude support-annotation only from my support-v4 dependency and remove support-v4 dependency from all other dependencies that have support-v4 as transitive dependency.
Note: Using one of the above approaches i was able to solve my problem and figure out how we can remove transitive dependencies when they are referenced from multiple dependencies.
And regarding the ( * ) symbol it means that the dependency tree is for that dependency is already shown. Instead of showing the whole tree for those dependencies again gradle shows ( * ) symbol with them.
Sample build.gradle file is available here
There is more graceful solution: you can use configueation.all block in your build.gradle file like in example below:
configurations.all {
exclude group: 'com.android.support', module: 'support-annotations'
}
It should exclude all transitive dependencies from all inner modules in your application.

duplicate entry: com/android/volley/AuthFailureError.class while compiling project in android studio

I am using external libraries payu money sdk and linkedin-sdk, both uses volley libraries, which while compiling project gives duplicate entry of AuthFailureError.class
Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
java.util.zip.ZipException: duplicate entry: com/android/volley/AuthFailureError.class"
i have also added following code to exclude module, but still same error
configurations{
all*.exclude module: 'com.android.volley'
}
please help
I stumbled upon this same error, and after reading this, I was able to solve it.
Try adding this line inside your app dir build.gradle file -
android{
configurations {
all*.exclude group: 'com.android.volley'
}}
Hope this helps.
I had this problem when I tried to generate the APK (release) and I solved it changing the linkedin-sdk build.gradle:
From:
dependencies {
compile 'com.android.support:support-annotations:20.0.0'
compile 'com.android.support:support-v4:21.0.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/volley.jar')
androidTestCompile('junit:junit:4.12') }
To:
dependencies {
compile 'com.android.support:support-annotations:20.0.0'
compile 'com.android.support:support-v4:21.0.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.volley:volley:1.0.0'
androidTestCompile('junit:junit:4.12') }
Add multiDexEnabled true in the defaultConfig section of your gradle file
Then,
compile 'com.android.support:multidex:1.0.1' in your dependencies
Finally add below in your application class:
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
Also, check if you are using volley.jar in your libs folder. If so, delete that jar file, and compile again. Sometimes, jar dependencies conflicts with those compiled using remote source.
This is an example how to exclude classes in dependencies when there is duplicate entry in gradle.
compile ('com.google.api-client:google-api-client-android:1.17.0-rc') {
exclude module: 'httpclient'
}
or try with your way just add some more text
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
So, now what you have to do is
Search CTRL+SHIFT+N in android studio for the class AuthFailureError.class
See which jar contains this and remove it like above (This is just as an example/You have to figure out the duplicate class and manually remove it)
just remove the duplicate jar file(note:use new version,delete old version) for importing "com.android.volley.AuthFailureError" in build.gradle. Then clean project and rebuild project and then run you will get result.
I had the similar issue while making build on Jenkins, weirdly it was working fine on my local machine. After adding below exclude it worked both on local machine and Jenkins.
android{
configurations {
all*.exclude group: 'com.android.volley'
}}
I have added configurations block to my app's build.gradle inside android section.
If it matter's Compile SDK version is 22 and Build Tools version is 25.0.0
This worked like a charm.
Okay I got my answer
On mac instead of control n, it is command 0 and the command i needed was
configurations {
all*.exclude module: 'volley-release'
}
Just remove your volley library from dependancy.
Try clean and rebuild project it works for me.
Ex. payusdk are also implementing volley library so that is the reason exception shows duplicate entry. I hope it works. because i also found this error and i do these things it works.
Thanks.
Use the below command in Android studio terminal to get the dependency conflict data - [Replace with your app Name]
./gradlew -q :<app>:dependencyInsight --dependency volley --configuration compile
If you are using latest Volley library from android [https://github.com/google/volley/releases], add below two lines in your build.gradle file under each of the compile library entries that has conflict.
Ex:
compile('com.xyz:abc:1.1.0-RELEASE') {
exclude module: 'library'
exclude group: 'com.mcxiaoke.volley'
}

In an Android Gradle build, how to exclude dependencies from an included jar file?

In my Android project, I use a library that comes as a jar.
I include it in the dependencies section like so:
dependencies {
...
compile files('libs/thethirdpartylibrary.jar')
...
}
I also want to use the okhttp library, which I include like this:
compile ('com.squareup.okhttp:okhttp:2.7.5')
(This particular version of okhttp depends on okio 1.6.0.)
The problem is that the thirdparty jar library depends on okio v0.9.0 and what's worse, bundles it.
As a result, I get a dex conflict error at build time.
I was able to resolve this by manually removing okio from the jar file and this seems to work. But I'm wondering if there's a way to do this in gradle.
My question: Can I remove bundled, transitive ( <- I hope I'm using this word the right way) dependencies from an included jar during build-time with gradle?
Exclude the Group in the dependencies by using the below lines.
1.
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
2.
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile ("com.xxx:xxx-commons:1.+") {
exclude group: 'junit', module: 'junit'
}
}
3.
configurations {
runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}
Try this one.
For more detail
According to this discussion here https://groups.google.com/forum/#!topic/adt-dev/g1AiJM7PeVs, what you want to do is not possible.
The syntax suggested in the other answers is for "normal" Maven dependencies.

Compiling ion library without android-support to avoid conflict

I'm trying to compile the ion library into my project like this:
compile 'com.koushikdutta.ion:ion:2.1.6'
but the problem is its dependencies there is also support-v4 library that I already imported in a subproject and that I cannot remove from there.
Is there a way to compile/import the ion library without taking the support library? I remember there was a sort a way like:
compile 'com.koushikdutta.ion:ion:2.1.6:support-v4'
to remove it but I don't remember the exact syntax and I can't find anything about it... any idea?
Found! :)
You need to add before the android declaration, in the gradle file:
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}

cannot access DialogStyle

After updating android-support library to 22.2.0 project stopped compiling.
error: cannot access DialogStyle
class file for android.support.v4.app.DialogFragment$DialogStyle not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for android.support.v4.app.DialogFragment$DialogStyle not found
Can't find how to work around this issue.
Previously used version was 22.1.1
#takoli's answer works in most cases but if you have other dependencies that silently include support-v4 or if you are too lazy to explicitly exclude support-v4 everywhere here is another solution.
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:mediarouter-v7:22.2.0'
// Force stable version of support-v4
compile ('com.android.support:support-v4:22.1.1') {
force = true
}
Update:
AndroidAnnotations released a new version 3.3.2 that resolves this issue. If you are using AndroidAnnotations update to 3.3.2 and use the 22.2.0 support libraries without forcing the old version of support-v4. For more information see this thread
Here is a couple workarounds that worked for us:
Workaround 1 (some people see a NPE with this, some don't)
I just found a TEMPORARY workaround... till appcompat fixes this issue:
Create the following package in your project src/main/java
android.support.v4.app
Create the following new file:
DialogFragment$DialogStyle.java
Contents
package android.support.v4.app;
// todo remove this file when fixed in appcompat (https://code.google.com/p/android/issues/detail?id=175086)
public #interface DialogFragment$DialogStyle {
}
Workaround 2 (bit more ugly, but less potential for a build issue)
I found another work-around.... a bit more ugly... but has gotten us around this issue (including the NPE on the above work-around) till appcompat 22.2 is fixed.
Create the following package in your project src/main/java
android.support.v4.app
Copy the Google v4 FragmentDialog.java code
https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/DialogFragment.java
Rename the class (to something like TempFragmentDialog). You will get a "Duplicate" class error if you don't rename the class.
Any FragmentDialog, in your project, that has #Inject will need to extend your copy of the FragmentDialog (example: public class MyFragmentDialog extends TempFragmentDialog)
Try this, it resolved my issue:
compile ('com.android.support:appcompat-v7:22.2.0') {
exclude module: 'support-v4' }
compile ('com.android.support:recyclerview-v7:22.2.0') {
exclude module: 'support-v4' }
compile ('com.android.support:cardview-v7:22.2.0') {
exclude module: 'support-v4' }
compile ('com.android.support:design:22.2.0') {
exclude module: 'support-v4' }
// and exclude support-v4 from other dependencies as well that might include it
// finally add it, but avoid version 22.2.0...
compile ('com.android.support:support-v4:22.1.1')
There is no need to manually add the support-v4 library to your libs directory, the last import ensures that the right version is included in your project.
BTW all of this workaround is not your fault, blame others :)
I am using the work around found here https://code.google.com/p/android/issues/detail?id=175086#c9
I modified my build.gradle file to say the following in the dependencies section:
compile fileTree(include: ['*.jar'], dir: 'libs')
compile ('com.android.support:appcompat-v7:22.2.0') {
exclude module: 'support-v4'
}
compile ('com.android.support:gridlayout-v7:22.2.0') {
exclude module: 'support-v4'
}
compile ('com.android.support:cardview-v7:22.2.0') {
exclude module: 'support-v4'
}
compile ('com.android.support:design:22.2.0') {
exclude module: 'support-v4'
}
You will also have to exclude it from any other dependencies that use the support library like dagger or facebook.
Then, I added the android-support-v4.jar file found in $ANDROID_HOME/extras/android/support/v4 to my libs directory, as that file does seem to have DialogFragment$DialogStyle.
Now, my build is fully working again, but I still hope this can get fixed soon.
Simply put, this is a bug in support library version 22.2.0
Just upgrade to the next update 22.2.1, works like a charm.
Just in case to not to skip an answer: here is also a discussion about this problem https://github.com/excilys/androidannotations/issues/1435
BTW, do you use Android Annotations in a project where this problem exists ?
It happens if you are using android.support.v4.app.DialogFragment.
Try to use android.app.DialogFragment instead.

Categories

Resources