An old question, but even now there doens't seem to be a propper solution for libraries in AndroidStudio. I've been looking into making the migration step for some time now and this is what I found.
My 'solution'
Lets say we've got a Library (called lib) which contains shared code and an Application project (called app) which wants to use said library's code.
The key is defining a project in settings.gradle
in the Project's root (/Some/Path/MyProjects/Project/settings.gradle
)
This file should already exist and contain something like include ':app'
.
We will modify this file to also include the library AND define the library with the following two lines:
/Some/Path/MyProjects/Project/settings.gradle
...
// tell gradle we want to include a module called 'lib'
include 'lib'
// tell gradle there is a module called 'lib', found in a specific directory
// Note the /app on the end, the project structure wraps a project around a module, you want to refer that module!
project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')
...
Also edit the projects build.gradle /Some/Path/MyProjects/Project/app/build.gradle
to depend on the newly added module lib
/Some/Path/MyProjects/Project/app/build.gradle`
...
dependencies {
...
compile project (':lib') // we want to depend on our library
}
...
Extra
When working with multiple developers or for the sake of flexibility, I use my gradle.properties
in my .gradle
directory (for *nix based systems usually found in homedir, not sure where Windows looks for it). Do note that you might need to create the directory and file yourself.
In this file you can create, if you like, constants that can be used by you throughout your gradle files. For example, mine contains something like the following:
theconstant=/Some/Path/MyProjects/Library/app
note the seemingly missing quotes (not sure whether thats really needed tho)
Now we can replace
project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')
with
project (':lib').projectDir = new File(theconstant)
Now you and your team could define this constant (which might differ per machine) and change the path accordingly.
pros
- No copying of whole libraries anymore!
- Flexibility, you can edit the library in the project's window
- Multiple developers working on the same projects can define their own paths
- Library gets compiled at the projects' compile time (
BuildConfig.DEBUG
!)
cons
I havn't found the chance to properly test this through, yet this seems like the most elegant solution for the time being! I would like to hear your thoughts on this.