Android Studio:使用Android模块而不复制代码 [英] Android Studio: using Android Modules without duplicating code

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

问题描述

我需要实现两个使用同一Android库LibC的应用程序AppAAppB.

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

使用Android Studio,我希望拥有三个项目:LibCAppAAppB,其中AppAAppB引用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时所做的);

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具有两个不同项目位置的想法-而没有两个物理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.

到目前为止,这被视为最专业" 方式. 创建您的App项目,它们不必与选项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会以某种方式迫使您首先创建一个应用程序模块,然后才能创建一个库模块.我希望这种情况将来会随时改变,但这没什么大不了的.之后只需创建您的库模块,然后删除应用程序模块即可.然后,您可以通过gradle构建您的库,并导出要与您的项目一起使用的人工制品(ar或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 -提示和技巧/以供参考.

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

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

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

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天全站免登陆