How to share a single library source across multiple projects
To make full use of Android Studio, I would recommend initially creating a normal android project. Then, within that project, add a Module "android library
" type, and (optionally) a "java library
" (if you have code you want to use that may be common to Android applications and to server side code).
Using this mechanism, you do not need to manipulate and modify gradle and manifest files.
I wanted to do something similar for my Android Wear project, where you have mobile
and wear
modules. In Android Studio 1.5.1, you can do File->New->Module and pick Android Library as your module. This will take care of many of the settings described above and put your library inside your existing Android Wear project.
Yes, it works now in 0.5.0. There isn't a friendly UI to it (Import Project isn't what you want, as that creates an entirely new project; Import Module is still broken; see https://code.google.com/p/android/issues/detail?id=62122), but you can set up your build files to make it work.
Let's say you have a directory structure that looks like this:
MyApp
appmodule
build.gradle
src
main
java
build.gradle
settings.gradle
MyPlainJavaLibrary
build.gradle
src
java
Note that MyPlainJavaLibrary isn't underneath the MyApp directory in the filesystem.
Set up your settings.gradle file like so:
include ':appmodule', '..:MyPlainJavaLibrary'
and include a dependency to it in build.gradle like this (don't forget the leading colon):
dependencies {
...
compile project(':..:MyPlainJavaLibrary')
}
This works for me. In my Project viewer, I now see MyApp and MyPlainJavaLibrary as two root-level module directories in the view, and I can make java import
statements work across module boundaries and such.
Note that in this default setup, this shared module will only have a single build output folder that will be shared among all the projects using it. This may or may not be what you want. If you'd like to have a different output directory for each project using the shared module, you can probably do some magic with the output directory in the sourceSet
; if you want help with this, I'd recommend trying it out on your own and posting a question with details on your work if you get stuck.
You can only have one settings.gradle file per project, so this isn't going to take a whole other multimodule Gradle project and bring in all the modules in one fell swoop; you'll need to bring them in individually. However, it should satisfy the use case of using a module in multiple projects.
(as of version 2.1+):
Below are the steps that I took to share library source outside of the project directory. Instead of plain Java library, my codes are compiled as Android module, but that library module is not contained inside the main project. It is fine with me as long as there are no code duplications and I only need to maintain one set of library code for all projects:
1) File->new Project. Give a name to your library project (here I use LibraryProject
). Continue the remaining steps to create a normal project (since this is intended as a library, I chose "add no activity")
2) By default, Android studio creates the module named as "app" inside the project folder. To prevent names collision with the actual application module, rename the module to something else (Right click "app" module at left panel->Refactor->Rename).
3) In the build.gradle
inside your library module folder, change the top line
apply plugin: 'com.android.application'
to
apply plugin: 'com.android.library'
and then remove the "applicationId" line under "defaultConfig". Also, since this is a library, remove the xlmns:...
namespace and <application ..>
body from Manifest.xml as well.
That's all for the library part. Now, to create/modify your main application:
4) If it is a new project, first create new project File->new Project->ApplicationName.
5) Open settings.gradle
(there should only be one such file in every project) and include the following line (note the missing leading semi-colon in library module):
include ':app', '..:LibraryProject:yourLibraryModule'
6) Then go to File->Project Structure.
7) Under the tab "Dependencies" click the green "+" button at right. Select "Module dependency". Choose your library module, then click OK.
You should now be able to use the library classes in your application.
ALTERNATIVE METHOD If, for some reason, there are still problems with the above method, you can try the following (suggested in here):
1) Repeat steps 1 to 4 above. By default the main and external (library) project look something like this:
/MainProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/
/LibraryProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/
2) As usual, refactor the modules name (in android studio right-click module->refactor->rename) to something less confusing, such as
/MainProject
+ build.gradle
+ settings.gradle
+ MainModule/
+ build.gradle
+ src/
/LibraryProject
+ build.gradle
+ settings.gradle
+ LibraryModule/
+ build.gradle
+ src/
3) Modify the settings.gradle
in MainProject
:
include ':LibraryModule', ':app'
project(':LibraryModule').projectDir = new File(settingsDir, '../LibraryProject/LibraryModule')
Sync the project and you're done.
Note on Proguard
Currently you should not use a proguard on external library projects/modules. Instead, you replace the following (original answer here)
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
debug {
minifyEnabled false
}
}
with the following (in build.gradle
of the library):
buildTypes {
release {
consumerProguardFiles 'proguard-project.txt'
}
}
where proguard-project.txt
is the file that contains the proguard rules for your library project.