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

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

问题描述

我的公司正在 Android Studio 上制作多个 Android 项目,它们都共享一些相似的代码,例如自定义视图、自定义 HTTP 客户端和许多其他内容.

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 库模块中.

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.

在 Eclipse 中非常简单,只需创建一个新的 Android 库项目,然后将您的代码移到那里,并将 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.

Android Studio 是如何进行这种重构的?

How can such refactoring be done by Android Studio?

推荐答案

我们公司使用了一种结构,其中包含具有共享模块的多个项目.假设你有 2 个项目,project1 和 project2,它们是 2 个独立的 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/

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

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'

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

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

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

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.

希望能帮到你.

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

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