问题
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?
回答1:
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.
回答2:
Refer these Links
How to create a library project in Android Studio and an application project that uses the library project
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup
来源:https://stackoverflow.com/questions/34125937/how-to-setup-android-library-module-and-be-referenced-by-multiple-projects-in-an