There are situation in modular approach when you want to exclude some of the dependency from that the library project used. This happens because of many reason. Either we use a latest dependency or we have transitive dependency issue.
In this blog we are going to watch how dependencies are excuded from library project.
Let Us first create two project
- GradleDemo (our main project)
- MyLibrary (Android Library project)
Let us save following dependency in MyLibrary
1 2 3 4 5 6 |
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.2.0' compile 'com.android.support:design:23.2.0' } |
Now let us draw dependency tree hierarchy on different case.
Note: we can draw dependency tree graph using android studio terminal (command prompt) by following command
gradle app:dependencies
or use gradle –gui for graphic user interface.
Case 1. When no library project is Added
1 2 3 4 5 6 |
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.2.0' compile 'com.android.support:design:23.2.0' } |
Result1.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
+--- com.android.support:appcompat-v7:23.2.0 | +--- com.android.support:support-v4:23.2.0 | | \--- com.android.support:support-annotations:23.2.0 | +--- com.android.support:animated-vector-drawable:23.2.0 | | \--- com.android.support:support-vector-drawable:23.2.0 | | \--- com.android.support:support-v4:23.2.0 (*) | \--- com.android.support:support-vector-drawable:23.2.0 (*) \--- com.android.support:design:23.2.0 +--- com.android.support:recyclerview-v7:23.2.0 | +--- com.android.support:support-annotations:23.2.0 | \--- com.android.support:support-v4:23.2.0 (*) +--- com.android.support:appcompat-v7:23.2.0 (*) \--- com.android.support:support-v4:23.2.0 (*) |
Case2. When no dependency is added in the main project
1 2 3 4 5 6 |
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' // compile 'com.android.support:appcompat-v7:23.2.0' // compile 'com.android.support:design:23.2.0' } |
Result2.
1 |
No dependencies |
Case3. When Library Project is Added
1 2 3 4 5 6 7 |
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' // compile 'com.android.support:appcompat-v7:23.2.0' // compile 'com.android.support:design:23.2.0' compile project(':mylibrary') } |
Result3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
\--- project :mylibrary +--- com.android.support:appcompat-v7:23.2.0 | +--- com.android.support:support-v4:23.2.0 | | \--- com.android.support:support-annotations:23.2.0 | +--- com.android.support:animated-vector-drawable:23.2.0 | | \--- com.android.support:support-vector-drawable:23.2.0 | | \--- com.android.support:support-v4:23.2.0 (*) | \--- com.android.support:support-vector-drawable:23.2.0 (*) \--- com.android.support:design:23.2.0 +--- com.android.support:recyclerview-v7:23.2.0 | +--- com.android.support:support-annotations:23.2.0 | \--- com.android.support:support-v4:23.2.0 (*) +--- com.android.support:appcompat-v7:23.2.0 (*) \--- com.android.support:support-v4:23.2.0 (*) |
Case4. Excluding a group of dependency from our library project when building our main project
1 2 3 4 5 6 7 8 9 |
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' // compile 'com.android.support:appcompat-v7:23.2.0' // compile 'com.android.support:design:23.2.0' compile (project(':mylibrary')){ exclude group:'com.android.support' } } |
Result4.
1 |
No dependencies |
Case5. Excluding a module from a group of dependency from our library project when building our main project
1 2 3 4 5 6 7 8 9 |
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' // compile 'com.android.support:appcompat-v7:23.2.0' // compile 'com.android.support:design:23.2.0' compile (project(':mylibrary')){ exclude group:'com.android.support', module:'design' } } |
Result5.
1 2 3 4 5 6 7 8 |
\--- project :mylibrary \--- com.android.support:appcompat-v7:23.2.0 +--- com.android.support:support-v4:23.2.0 | \--- com.android.support:support-annotations:23.2.0 +--- com.android.support:animated-vector-drawable:23.2.0 | \--- com.android.support:support-vector-drawable:23.2.0 | \--- com.android.support:support-v4:23.2.0 (*) \--- com.android.support:support-vector-drawable:23.2.0 (*) |
Case 6: When same dependencies are present in the library project
1 2 3 4 5 6 7 |
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.2.0' compile 'com.android.support:design:23.2.0' compile(project(':mylibrary')) } |
Result 6:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
+--- com.android.support:appcompat-v7:23.2.0 | +--- com.android.support:support-v4:23.2.0 | | \--- com.android.support:support-annotations:23.2.0 | +--- com.android.support:animated-vector-drawable:23.2.0 | | \--- com.android.support:support-vector-drawable:23.2.0 | | \--- com.android.support:support-v4:23.2.0 (*) | \--- com.android.support:support-vector-drawable:23.2.0 (*) +--- com.android.support:design:23.2.0 | +--- com.android.support:recyclerview-v7:23.2.0 | | +--- com.android.support:support-annotations:23.2.0 | | \--- com.android.support:support-v4:23.2.0 (*) | +--- com.android.support:appcompat-v7:23.2.0 (*) | \--- com.android.support:support-v4:23.2.0 (*) \--- project :mylibrary +--- com.android.support:appcompat-v7:23.2.0 (*) \--- com.android.support:design:23.2.0 (*) |