Ok Google, how to add git submodule external library gradle android
So you have some kind of standard library with a lot of useful stuff and you use it in every project you worked on.
Here is mine:
As you can see, it’s an independent Android library with it’s own top level build.gradle and settings.gradle files.
I had some troubles adding the library into my projects as a git submodule and making it easy to use. So I’d like to share my final working recipe. I’m not a Gradle pro or something, so if you have a better solution, please share it in comments.
First of all we need to upload the library to a git repository so we can add it to our new project as a git submodule:
git submodule add https://your_lib_repository_here
Here is the first trick: the library’s directory name must be capitalized:
And now we have a problem. Gradle does not support multiple settings.gradle scripts and the library modules depend on each other:
So if new project’s :app module declares dependency from the library:
the library’s :stdlib_android module won’t be able to resolve it’s own dependency:
because, as far as I understand, Gradle resolves project dependencies relatively to calling project’s settings.gradle file, but since we added the library as a part of new project, relative paths had changed.
To make everything work, we need to change project dirs for the library’s modules relatively to new project’s settings.gradle file:
This is a perfectly fine working solution. We can include modules we want and they don’t even need to be inside your new project directory.
But if you have only one library with a lot of modules (my case) and you don’t want to write extra code for each module, there is a better way.
I took the solution from here and tweaked it to include required modules, and not the whole lib:
It is important to add the library dependencies above “some gradle magic” block, or build will be broken.
After this, we can easily add modules we need from the library (saving us compile time).