简体   繁体   中英

How to setup Android Library module and be referenced by multiple projects in Android Studio?

My company is making several Android projects on Android Studio, which all of them share some similar codes, such as custom views, customised HTTP clients, and many other things.

The problem I am facing is, I am new to Android Studio and I am not sure how to extract these common codes across several projects to a single Android Library modules that will be referenced by these projects.

In Eclipse it is very simple, just create a new Android Library project, and then move your code over there, and set the Android Application projects to reference the common library.

How can such refactoring be done by Android Studio?

Our company used a structure with multiple projects with shared modules. Suppose you have 2 projects, project1 and project2 which are 2 independent Android Studio projects and want to share some modules. The folder structure will be like this:

source-code-root-folder/
    + android-studio-project1/
         + project1-app-module/
         + project1-internal-module/
    + android-studio-project2/
         + project2-app-module/
         + project2-internal-module/
    + shared-module1/
    + shared-module2/

You can first create the projects and modules from Android studio. Then relocate the folders like the structure above. Then update the setting in project1 by putting this settings in the source-code-root-folder/android-studio-project1/settings.gradle :

include ':android-studio-project1'
include ':project1-app-module'
include ':project1-internal-module'
include ':..:shared-module1'
include ':..:shared-module2'

Then open the android-studio-project1/project1-app-module/build.gradle and update the dependencies:

...
dependencies {
    ...
    compile project(':project1-internal-module')
    compile project(':..:shared-module1')
    compile project(':..:shared-module2')
}

This will make project1 be able to load the internal module and the shared modules as well. Try sync and build your project1 by running build.gradle in the project1 and it should work. Of course similar setting can be used for the project2.

Hope that this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM