Android Studio 中的代码共享 [英] Code sharing in Android Studio

查看:23
本文介绍了Android Studio 中的代码共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始从事一个项目,我需要在多个应用程序之间共享一堆 Java 类.在 Eclipse 中,可以使用所有这些类创建一个项目,并将其用作包含所有相关项目的工作区中的库,但在 Android Studio 中则不能似乎可以这样做(至少不容易).

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 文件,然后在我的项目中使用它.但是,据我所知,这将使我的库成为开源(我说得对吗?),这是我不想要的.我正在为客户做这件事,我希望代码库是私有的.

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.

另外,我知道可以将模块导入到新项目中.但这会创建原始模块的副本.这根本不是我想要的.我不想维护相同类的多个副本,这完全违背了代码共享"的目的.

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'.

有什么好的方法可以实现我的目标吗?任何帮助表示赞赏.

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

推荐答案

您有几个不同的选择.

一种选择是将您的库作为单独的项目进行维护,并将它们编译为存档格式,例如 JAR 或 AAR;JAR 文件用于纯 Java 库,AAR 用于 Android 库(包含访问 Android API 和/或具有 Android 资源的代码).正如评论中指出的那样,AAR 不会像 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 中所做的那样.你观察到如果你通过UI导入Android Studio会制作一个模块的副本,但是如果你绕过UI直接修改构建脚本,你可以做你想做的,就是就地使用模块并共享多个项目之间的一个副本.为此,请在您的 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')

我强烈建议您不要在这里使用纯粹的相对路径;在此示例中,路径锚定到 Gradle 提供的 settingsDir 变量,该变量被定义为找到 settings.gradle 的目录.如果您使用纯相对路径(即未锚定到任何内容),则您依赖于运行构建文件的所有环境(命令行、Android Studio 与 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.

这篇关于Android Studio 中的代码共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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