Android Studio:在不重复代码的情况下使用 Android 模块 [英] Android Studio: using Android Modules without duplicating code

查看:27
本文介绍了Android Studio:在不重复代码的情况下使用 Android 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现两个应用程序 AppAAppB,它们使用相同的 Android 库 LibC.

I need to implement two applications AppA and AppB that use the same Android Library LibC.

使用 Android Studio,我希望拥有三个项目:LibCAppAAppB,以及 AppAcode> 和 AppB 引用 LibC.我找不到获得它的方法.

Using Android Studio, my desire is to have three projects: LibC, AppA, and AppB, with AppA and AppB referencing LibC. I cannot find a way to obtain this.

到目前为止,我已经实现了AppA.在编写 AppA 时,我通过

So far I have implemented AppA. While writing AppA I created a module via

File > New Module > Android Library > `LibC`

LibC 库现在位于 AppA 项目中,我需要开始在 AppB 项目中使用它.

The LibC library is now in the AppA project and I need to start using it within the AppB project.

如何将 LibC 移出项目 AppA 并仅为 LibC 创建一个项目?

How do I move LibC out of project AppA and create a project only for LibC?

我应该如何创建一个仅包含 Android 库的新项目?

How am I supposed to create a new project containing only an Android Library?

如果我开始一个新的 Android 项目,它会不断创建一个新的应用程序.

If I start a new Android Project it keeps creating a new App.

此处的文档 说:

1) 我需要创建一个新应用程序,然后在该应用程序中创建一个新模块(这是我在编写 AppA 时所做的);

1) that I need to create a new Application and then a new module within that application (which is what I did while writing AppA);

2) 我可以将现有的库代码导入到新项目中.

2) that I can import existing library code into a new project.

但是我不想将库的代码复制到新项目中(这是发生的事情),我想使用相同文件以便为库保留单一代码库.

However I do not want to copy the code of the library into the new project (which is what happens), I want to use the same files in order to keep a single code base for the library.

这可能吗?如果是,我在哪里可以找到有关它的文档?

Is this possible? If it is, where can I find some doc about it?

推荐答案

由于我最近在自己的项目中有这个需求,可能给大家提一些建议.

Since I had this requirement in my own project recently, I may have some suggestions for you.

基本上这里有您的选择:

Basically here are your options:

如果 AppA 和 AppB 有某种关联(这是您必须决定的),您可以采用简单的方法.你的项目中已经有 AppA 和 LibC,所以你只需要在 Studio 中为 AppB 创建另一个 App 模块(文件 > 新建模块 > 应用模块)

If AppA and AppB are somehow related (this is something you have to decide), you can go the easy way. You already have AppA and LibC in your project, so you only need to create another App module in Studio for AppB (File > New module > Application module)

您可能会看到有趣的是,您的项目目录最初的名称为 AppA,而您的 AppA 模块的名称为 app.这是因为 Android Studio 默认以您在向导中提供的名称命名您的项目,而您的第一个模块始终命名为 app.

You may see interestingly, that your project directory initially has the name AppA, and your AppA module is named app. That is because Android Studio per default names your project after the name you provided in the wizard, while your first module is always named app.

您可以将包含的项目从 AppA 重命名为 project 之类的名称(通过右键单击项目 > 重构 > 重命名,并将 app 模块重命名为 AppA.

You may rename your containing project from AppA to something like project (via right-click on project > Refactor > rename, and rename the app module to AppA.

基本结构如下:

Studio Project AppA (rename to "project")
|
|\_app (module AppA, rename to "AppA")
| |_ src
|
|\_AppB (module AppB)
| |_ src
|
 \_LibC (module LibC, dependency for AppA and AppB)
  |_ src

此设置的优点是它包含在一个 Android Studio 项目中,因此您可以免费获得应用模块和库之间的所有 Android Studio 重构魔法.

Advantage of this setting is that it is contained in one Android Studio project, and therefore you get all the Android Studio refactoring magic between your app modules and the library for free.

假设您在 LibC 中更改了一个方法,它会同时反映在 AppA 和 AppB 中.

Imagine you change a method in LibC, it would be reflected in both AppA and AppB.

基本上,这反映了您为 AppA 和 AppB 设置两个不同项目位置的想法 - 没有 2 个物理 LibC 位置.只需将 LibC (AppA) 符号链接到新位置 (AppB).

Basically this reflects your thought of having two different project locations for AppA and AppB - without having 2 physical LibC locations. Just take LibC (AppA) and symlink it to the new location (AppB).

不是一个漂亮的解决方案,但可能(我只能代表类 Unix 系统,但也应该适用于 Win.)

Not a beautiful solution but possible (I can only speak for Unix like systems, but should work on Win as well.)

Studio Project AppA (location 1 on disk)
|
|\_app
| |_ src
|
 \_LibC (dependency for app)
  |_ src

… 

Studio Project AppB (location 2 on disk)
|
|\_app
| |_ src
|
 \_*LibC (symlinked from AppA project dir)
  |_ src

请注意,在这种情况下,项目间重构将失败,例如您打开项目 AppA,在 LibC 中进行实质性更改,运行 AppA.稍后您打开 AppB 会遇到编译时失败,因为 AppB 尚未适应使用 LibC 的新 API.

Beware that inter-project refactoring will fail in that case, e.g. you open project AppA, make substantial changes in LibC, run AppA. later you open AppB and will get compile time failures, since AppB has not been adapted to use LibC’s new APIs.

这可以被认为是迄今为止最专业"的方式.创建您的应用项目,它们不必像选项 1 那样位于同一项目树中,并将它们链接到您的库 LibC(aar 或 jar)的构建工件.

This can be considered by far the most "professional" way. Create your App projects, they don’t have to be in the same project tree like in option 1, and link them against a built artefact of your library LibC (aar or jar).

理想情况下,这个人工制品是版本化的并且位于(本地)maven 存储库中.这样您就始终拥有一致的构建(因为您链接到库的特定版本,无论 LibC 当前处于何种开发状态)

Ideally, this artefact is versioned and located in a (local) maven repository. That way you always have a consistent build (since you link against a specific version of your library, regardless of the development state LibC is currently in)

Studio Project AppA (location 1 on disk)
|
 \_app
  |_ src
  |_ build.gradle dependency on libc (jar/aar + version XX)

… 

Studio Project AppB (location 2 on disk)
|
|
 \_app
  |_ src
  |_ build.gradle dependency on libc (jar/aar + version YY)


Studio Project LibC (location 3 on disk)
|
 \_LibC (library module from AppA project dir)
  |_ src (in development)

正如您所说,Android Studio 以某种方式强制您先创建应用模块,然后才能创建库模块.我希望这会在未来随时改变,但这没什么大不了的.之后只需创建您的库模块,然后摆脱 app 模块.然后,您可以通过 gradle 构建您的库并导出要用于您的项目的人工制品(aar 或 jar).

As you said, Android Studio somehow forces you to create an app module first, before you can create a library module. I hope this will change anytime in the future, but it is no big deal. Just create you library module afterwards, and get rid of the app module. You can then build your library via gradle and export the artefact (aar or jar) to be used with your project.

在 LibC 的 build.gradle 中,您可能会找到类似这样的内容来将其部署到本地机器的 maven 存储库:

In LibC’s build.gradle you may find something like this to deploy it to your local machine’s maven repository:

apply plugin: 'maven'
group = 'your.groupid.artefactid'
version = '0.2.0'

uploadArchives {
    repositories {
        mavenDeployer {
            repository url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
        }
    }
}

参见 http://www.alonsoruibal.com/my-gradle-tips-and-tricks/ 供参考.

如果您运行 gradle uploadArchives,它会创建您的工件并将其推送到您的本地 Maven 存储库中.

If you run gradle uploadArchives, it creates your artefact and pushes it into your local maven repository.

还有另一种可能的解决方案,类似于选项 2.通过 gradle 从多个项目中引用库作为文件引用,参见

There is another possibile solution, similar to option 2. Reference the library from multiple projects via gradle as a File reference, see

https://stackoverflow.com/a/21021293/1128600

这个方法也有选项2的缺点(没有多个API版本控制,重构时没有应用间同步)

This method also has the drawbacks of option 2 (no multiple API versioning, no inter-app syncing when refactoring)

希望基于个人经验的总结可以帮助您找到方向.

Hope that summary based on personal experience helps you in finding a direction.

如果应用程序相关,我个人会选择选项 1 或选项 3 (Maven)

I personally would go with either option 1 if Apps are related, or option 3 (Maven)

这篇关于Android Studio:在不重复代码的情况下使用 Android 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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