如何设置Android库模块并被Android Studio中的多个项目引用? [英] How to setup Android Library module and be referenced by multiple projects in Android Studio?

查看:159
本文介绍了如何设置Android库模块并被Android Studio中的多个项目引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司在Android Studio上制作了几个Android项目,它们都共享一些类似的代码,例如自定义视图,自定义HTTP客户端以及其他许多内容。



我面临的问题是,我是Android Studio的新手,我不确定如何将这些跨越多个项目的通用代码解压到这些项目将引用的单个Android库模块。



在Eclipse中,它非常简单,只需创建一个新的Android库项目,然后将代码移到那里,并将Android应用程序项目设置为引用公共库。



Android Studio如何完成这样的重构?

解决方案

项目共享模块。假设您有2个项目,project1和project2是2个独立的Android Studio项目,并且想共享一些模块。文件夹结构将如下所示:

 源代码根文件夹/ 
+ android-studio-project1 /
+ project1-app-module /
+ project1-internal-module /
+ android-studio-project2 /
+ project2-app-module /
+ project2-internal-module /
+ shared-module1 /
+ shared-module2 /

您可以先从Android Studio创建项目和模块。然后重新定位上述结构的文件夹。然后通过将这些设置放在 source-code-root-folder / android-studio-project1 / settings.gradle 中更新project1中的设置:



包含':android-studio-project1'
include':project1-app-module'
include':project1-internal-模块'
include':..::shared-module1'
include':..:shared-module2'

然后打开 android-studio-project1 / project1-app-module / build.gradle 并更新依赖项:

  ... 
依赖关系{
...
编译项目(':project1-internal-module' )
编译项目(':..:shared-module1')
编译项目(':..:shared-module2')
}

这将使project1能够加载内部模块和共享模块。尝试同步并通过在project1中运行build.gradle来构建您的project1,它应该可以工作。当然类似的设置可以用于project2。



希望这可以帮到你。


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.

这篇关于如何设置Android库模块并被Android Studio中的多个项目引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆