简体   繁体   English

Android Studio中的代码共享

[英]Code sharing in 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. 我已经开始从事一个项目,在该项目中我需要在许多应用程序中共享一堆Java类。 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). Eclipse ,可以创建一个具有所有此类类的项目,并将其用作包含所有相关项目的工作区中的库,但是在Android Studio ,似乎不可能这样做(至少不容易)。

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. 我一直在阅读许多文章,其中许多文章建议建立一个图书馆项目,生成一个aar文件,然后在我的项目中使用它。 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. 但这会创建原始模块的COPY。 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. 任何帮助表示赞赏。

You have a couple different options. 您有几种不同的选择。

One option is to maintain your libraries as separate projects and compile them to an archive format, such as JAR or AAR; 一种选择是将您的库维护为单独的项目,并将其编译为存档格式,例如JAR或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). JAR文件用于纯Java库,而AAR用于Android库(包含访问Android API和/或具有Android资源的代码)。 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; 正如评论中所指出的那样,AAR不会强迫您将代码发布到世界上,而不是JAR文件。 it's just an archive file format whose files can be local to your machine or your organization. 它只是一种存档文件格式,其文件可以位于您的计算机或组织的本地。

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. 如果您属于多开发人员组织,则可能会发现使用存储库管理器在组织内发布和维护这些库很方便,并且可以使用Maven坐标样式规范在您的项目中包括库,不必手动复制到您的开发计算机上。

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. 这种方法的缺点是,使这些库的更改变得更加困难:您需要加载项目,进行更改,建立档案并分发档案。

The other approach is to keep the library as a source module like you did in Eclipse. 另一种方法是像在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. 您观察到,如果您通过UI导入Android Studio,则会复制该模块,但是如果您绕过UI并直接修改构建脚本,则可以执行所需操作,即就地使用该模块并共享多个项目中的一个副本。 To do this, work in your settings.gradle file and add this: 为此,请在settings.gradle文件中工作并添加以下内容:

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

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. 在此示例中,路径锚定到Gradle提供的settingsDir变量,该变量定义为在其中找到settings.gradle的目录。 If you use a pure relative path (ie 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与CI服务器)中工作目录是否相同,这不是一件好事。

You need to think in the eclipse projects as Android Studio/IntelliJ Idea modules. 您需要在Eclipse项目中将其视为Android Studio / IntelliJ Idea模块。 Then, you can generate android (or java) libraries and then include them in your project. 然后,您可以生成android(或java)库,然后将其包含在您的项目中。

To mark an Android Studio module as a library you can go to File -> Project Structure -> Facets and there click on Library Module 要将Android Studio模块标记为库,可以转到文件 -> 项目结构 -> ,然后单击库模块

在此处输入图片说明

You can certainly create and use a library without making it open source or available to others. 您当然可以创建和使用库,而无需将其开源或供他人使用。

First, you don't need to make it an aar unless it contains Resources. 首先,除非包含资源,否则无需将其设为aar。 If it's just plain classes, you can just make it a .jar file. 如果只是普通类,则可以将其设置为.jar文件。

Either way, the easiest way to share these libraries (either aar or jar) is to set up your own repository. 无论哪种方式,共享这些库(ar或jar)的最简单方法是设置自己的存储库。 Nexus and Artifactory are the two most common repository managers. Nexus和Artifactory是两个最常见的存储库管理器。

You keep the library in its own project, and then publish it to your own, in-house repository. 您将库保留在其自己的项目中,然后将其发布到自己的内部存储库中。

Then, projects that need to use the library are configured (in gradle) to use the in-house repository, and get the library from it. 然后,将需要使用该库的项目配置(以gradle方式)以使用内部存储库,并从中获取该库。

I was in same situation as you, and i founded an approach using git. 我和你处在同样的情况下,我使用git建立了一种方法。 Steps to do, to have library: 有图书馆的步骤:

  1. Create project in Android Studio. 在Android Studio中创建项目。
  2. Create android library module in that project. 在该项目中创建android库模块。
  3. In that library module create git repository. 在该库模块中创建git存储库。
  4. Add modulename.iml in .gitignore file .gitignore文件中添加modulename.iml
  5. Use GitHub or Bitbucket for private cloud repository. 将GitHub或Bitbucket用于私有云存储库。 and push your library to it. 并将您的资料库推送到它。
  6. Create new android library model in any project that you want. 在您想要的任何项目中创建新的android库模型。
  7. Close Android Studio (not sure is that mandatory). 关闭Android Studio(不确定是否是强制性的)。
  8. Using explorer go to your created module folder. 使用资源管理器转到您创建的模块文件夹。
  9. Remove all data in it, except modulename.iml . 除去其中的所有数据,除了modulename.iml
  10. Clone your library from "GitHub" into it. 将库从“ GitHub”克隆到其中。

That's all. 就这样。

Now you are able to use library in multiple project whether you are at home or at work. 现在,无论您是在家还是在工作中,都可以在多个项目中使用库。 Just after finishing you work not forget to push library changes. 完成工作后,别忘了推动库更改。 And after opening new one pull them. 在打开新的一个之后,将其拉出。 I think you can automate this thing somehow. 我认为您可以通过某种方式使这件事自动化。

The benefit is that you don't need to build any jar, or aar. 好处是您不需要构建任何jar或aar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM