[英]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. 我的公司正在Android Studio上制作几个Android项目,它们都共享一些相似的代码,例如自定义视图,自定义HTTP客户端以及许多其他东西。
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. 我面临的问题是,我是Android Studio的新手,我不确定如何将多个项目中的这些通用代码提取到单个Android库模块中,这些项目将引用这些模块。
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. 在Eclipse中,这非常简单,只需创建一个新的Android Library项目,然后将代码移到那里,然后将Android Application项目设置为引用公共库即可。
How can such refactoring be done by Android Studio? 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.
假设您有2个项目project1和project2,它们是2个独立的Android Studio项目,并且希望共享一些模块。 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. 您可以先从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
: 然后通过将以下设置放入
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: 然后打开
android-studio-project1/project1-app-module/build.gradle
并更新依赖项:
...
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. 这将使project1能够加载内部模块和共享模块。 Try sync and build your project1 by running build.gradle in the project1 and it should work.
尝试通过在project1中运行build.gradle来同步并构建您的project1,它应该可以工作。 Of course similar setting can be used for the project2.
当然,类似的设置可以用于项目2。
Hope that this can help you. 希望对您有所帮助。
Refer these Links 参考这些链接
How to create a library project in Android Studio and an application project that uses the library project 如何在Android Studio中创建库项目以及使用该库项目的应用程序项目
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.