在机器人工作室code共享 [英] Code sharing in Android Studio

查看:185
本文介绍了在机器人工作室code共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始工作的一个项目,我需要跨越了一堆应用程序共享一堆的Java 类。在的Eclipse 有可能创建一个项目,所有的这些类和使用它作为一个库与你的所有相关项目的工作空间,但在安卓工作室这似乎不可能这样做(至少不容易)。

I have started working on a project where I will need to share a bunch of Java classes across a bunch of apps. In Eclipse it was possible to create one project with all such classes and use it as a library in a workspace with all your dependent projects, but in Android Studio it doesn't seem possible to do so (At least not easily).

我一直在读了一堆职位和他们中的很多建议建立一个库项目,生成一个 AAR 文件,然后使用在我的项目。不过,按照我的理解,这将使我的图书馆开源(对吗?),我不想要的。我这样做的一个客户,我想了code基地是私有的。

I have been reading a bunch of posts and a lot of them suggest setting up a library project, generating an aar file and then using that in my projects. But, as I understand it, this will make my library open-source (Am I right?), which I don't want. I am doing this for a client and I want the code base to be private.

另外,我知道,一个模块可以导入到一个新的项目。但是,这将创建原始模块的COPY。这不是我想要的。我不想保持相同的类,这完全违背了code共享的宗旨多个副本。

Also, I know that a module can be imported into a new project. But this creates a COPY of the original module. This is not what I want at all. I don't wanna maintain multiple copies of the same classes, which completely defeats the purpose of 'code sharing'.

有没有达到我所期待的任何好办法?任何帮助是AP preciated。

Is there any good way of achieving what I am looking for? Any help is appreciated.

推荐答案

您有几个不同的选择。

一种选择是保持你的库作为单独的项目,并将其编译成一个压缩文件格式,如JAR或AAR; JAR文件是纯Java库和AAR是Android库(包含code访问Android的API和/或拥有Android的资源)。正如有人指出的意见,AAR不强迫你发布你的code到世界比JAR文件更多的会;它只是一个存档文件,其文件可以是本地的机器或组织形式。

One option is to maintain your libraries as separate projects and compile them to an archive format, such as JAR or AAR; JAR files are for pure Java libraries, and AAR is for Android libraries (which contain code that accesses Android APIs and/or has Android resources). As was pointed out in the comments, AAR doesn't force you to publish your code to the world any more than JAR files would; it's just an archive file format whose files can be local to your machine or your organization.

根据手中的归档文件,可以将其包含在其他项目中。如果你是一个多开发组织的一部分,你会发现它方便地使用一个仓库管理器对您的组织在发布和维护那些库,你可以使用Maven坐标格式的规格,包括在你的项目,你没有对手动复制到你的开发机库。

With that archive file in hand, you can include it in other projects. If you're part of a multi-developer organization, you may find it convenient to use a repository manager to publish and maintain those libraries within your organization, and you can use Maven coordinate-style specs to include libraries in your projects, which you don't have to manually copy over to your development machine.

这种方法的缺点是,它使得它有点难以更改这些库:你需要加载的项目,进行修改,建立一个归档和分发档案

The disadvantage of this approach is that it makes it a little harder to make changes to those libraries: you need to load up the project, make changes, build an archive, and distribute the archive.

另一种方法是保持库中作为源模块就像你在Eclipse一样。你观察到的Andr​​oid Studio将使得模块的副本,如果你通过用户界面导入它,但如果你直接绕过用户界面和修改构建脚本,你可以做你想做的,就是用模块就地和共享在多个项目中的一个副本。要做到这一点,工作在 settings.gradle 文件,加入:

The other approach is to keep the library as a source module like you did in Eclipse. You observed that Android Studio will make a copy of the module if you import it via UI, but if you bypass the UI and modify the build scripts directly, you can do what you want, which is to use the module in-place and share a single copy among multiple projects. To do this, work in your settings.gradle file and add this:

include ':module_name'
project(':module_name').projectDir = new File(settingsDir, '../relative/path/to/module')

我会强烈建议您不要使用纯相对路径在这里;在这个例子中,路径是固定由摇篮提供的 settingsDir 变量,它定义为目录中的 settings.gradle 被发现。如果您使用的是纯粹的相对路径(即不固定到任何东西),你是依赖于工作目录是在所有环境相同的地方生成文件运行(命令行与Android的工作室与CI服务器)这是不是一件好事承担。

I would strongly encourage you to not use a pure relative path here; in this example, the path is anchored to the settingsDir variable supplied by Gradle, which is defined to be the directory where settings.gradle is found. If you use a pure relative path (i.e isn't anchored to anything), you're dependent on the working directory being the same in all environments where the build file is run (command line vs. Android Studio vs. CI server), which isn't a good thing to assume.

这篇关于在机器人工作室code共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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