The error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. Ok, let’s say you have an App structure that looks like this:
So you have the main “app” and then you have a bunch of sub-apps/modules/libraries. The libraries are: 1) gene_test_library
, 2) genes_nine_old_androids_library
, & 3) swipe_list_view_library
.
My name is Gene, so that’s why there are all these “gene” libraries.
Inside the build.gradle
for “app”
, I have:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
//compile project(':libraries:genes_nine_old_androids_library')
compile project(':libraries:swipe_list_view_library')
}
Inside the build.gradle
for gene_test_library
, I have nothing:
dependencies {
}
Inside build.gradle
for gene_nine_old_androids_library
, I have:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
Inside build.gradle
for swipe_list_view_library
, I have:
dependencies {
compile 'com.nineoldandroids:library:2.4.0+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
This line of code compile fileTree(dir: 'libs', include: ['*.jar'])
just means “hey, look inside the ‘libs’ folder inside this module for any jar files. I do not have anything in the libs folder of any of the modules so you can ignore that line of code.
So let’s say I uncomment out //compile project(':libraries:genes_nine_old_androids_library')
In the build.gradle
for the “app”
module. Then I would get the “UNEXPECTED TOP-LEVEL EXCEPTION:”
error. Why is that?
Well, writing //compile project(':libraries:genes_nine_old_androids_library')
inside the build.gradle
for “app”
, is the same as taking the build dependencies of “genes_nine_old_androids_library”
module and putting it there. So uncommenting the //compile project(':libraries:genes_nine_old_androids_library')
statement, the build.gradle
for “app” module becomes:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
***compile fileTree(dir: 'libs', include: ['*.jar'])***
***compile 'com.android.support:appcompat-v7:21.0.0'***
compile project(':libraries:swipe_list_view_library')
}
Notice how now compile 'com.android.support:appcompat-v7:21.0.0'
shows up 2x. That’s where the error is coming from.