12

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'
}
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
nomongo
  • 3,435
  • 7
  • 30
  • 33

3 Answers3

13

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'
}
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • I tried that but could not get both the facebook SDK and recyclerview working – nomongo Aug 26 '14 at 20:28
  • Have you tried excluding support-v4 from the facebook SDK dependency instead? They may have included a different version than the other support libraries. – Kevin Coppock Aug 26 '14 at 20:33
  • I was using facebook.aar file for the facebook library in my project so just deleted that support-v4 jar file from the facebook.aar archive. – nomongo Aug 26 '14 at 20:39
4

Found a solution to this:

  1. Removed the config section in the build.gradle that excludes support-v4

  2. 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?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
nomongo
  • 3,435
  • 7
  • 30
  • 33
4

I fixed that adding:

compile ('com.facebook.android:facebook-android-sdk:3.22.0@aar'){
    exclude module: 'support-v4'
}
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
Ultimo_m
  • 4,724
  • 4
  • 38
  • 60
  • Then why when I run **gradle app:dependencies**, I still see: `com.google.android.gms:play-services-gcm:9.4.0 | +--- com.google.android.gms:play-services-base:9.4.0 | | +--- com.google.android.gms:play-services-basement:9.4.0 | | | \--- com.android.support:support-v4:23.0.0 ` – IgorGanapolsky Aug 08 '16 at 18:01
  • 1
    maybe its another library that has support, try removing all other libraries and leave the one that you want to exclude app compat and check what is happening – Ultimo_m Aug 11 '16 at 21:24