eclipse 如何从一个代码库创建多个 Android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4188250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:21:10  来源:igfitidea点击:

How to create multiple Android apps from one code base

androideclipse

提问by Doug

I have an Android code base which uses APIs with settings to get different data for several apps. All apps use the same code base but with one or two design tweaks. So how do I re-use the main code base without having to copy the whole Android project each time?

我有一个 Android 代码库,它使用带有设置的 API 来获取多个应用程序的不同数据。所有应用程序都使用相同的代码库,但进行一两个设计调整。那么如何重用主代码库而不必每次都复制整个 Android 项目呢?

iPhone uses multiple targets in the same project which works well. If android cant do this do I need to compile binaries of the code base in one project and then import into each new app project? If so how? I'm using Eclipse and am an intermediate Java developer.

iPhone 在同一个项目中使用多个目标,效果很好。如果 android 不能这样做,我是否需要在一个项目中编译代码库的二进制文件,然后导入到每个新的应用程序项目中?如果是这样怎么办?我正在使用 Eclipse 并且是一名中级 Java 开发人员。

Any help much appreciated!

非常感谢任何帮助!

Doug

道格

回答by Blumer

Check out "Working With Library Projects" from the Android documentation. This should be just what you're looking for: http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject

从 Android 文档中查看“使用库项目”。这应该正是您要寻找的:http: //developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject

回答by Ryan Heitner

The current way to approach this issue if you are using Android Studio with Gradle is by using Gradle, Build Type + Product Flavor

如果您将 Android Studio 与 Gradle 一起使用,目前解决此问题的方法是使用 Gradle、构建类型 + 产品风味

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

Build Variants

构建变体

One goal of the new build system is to enable creating different versions of the same application.

新构建系统的目标之一是能够创建同一应用程序的不同版本。

There are two main use cases:

有两个主要用例:

Different versions of the same application For instance, a free/demo version vs the “pro” paid application.

同一应用程序的不同版本 例如,免费/演示版本与“专业”付费应用程序。

Same application packaged differently for multi-apk in Google Play Store.

在 Google Play 商店中为多 apk 打包的相同应用程序不同。

This new concept is designed to help when the differences are very minimum. If the answer to “Is this the same application?” is yes, then this is probably the way to go over Library Projects.

这个新概念旨在在差异非常小时提供帮助。如果回答“这是同一个应用程序吗?” 是的,那么这可能是检查图书馆项目的方法。

回答by Dalbergia

Note: This answer is basically obsolete now that one can create .aar libraries with resources. It still works, though, and there may be times when the portability of a .jar is desirable, so I'm leaving it here.

注意:这个答案基本上已经过时了,因为可以使用资源创建 .aar 库。不过,它仍然有效,而且有时可能需要 .jar 的可移植性,所以我将它留在这里。

Blumer's answer is a good one, and you should definitely look into Android's idea of library projects. However, there is another alternative. If you have a module that contains only Java code, but no resources of any kind (images, layouts, etc.), you can compile it to a .jar file separately and use the resulting .jar in multiple application projects. It works like this:

Blumer 的回答很好,您绝对应该研究 Android 的库项目想法。但是,还有另一种选择。如果您的模块仅包含 Java 代码,但不包含任何类型的资源(图像、布局等),则可以将其单独编译为 .jar 文件,并在多个应用程序项目中使用生成的 .jar。它是这样工作的:

  • Create a new Java project in Eclipse (not an Android project) and move the source code of your module there.
  • If your module references any classes from the SDK, you'll need to add the Android SDK .jar to the project's classpath (Project > Properties > Java Build Path > Libraries > Add JAR).
  • When your module is ready to use, bundle up its .class files into a .jar. You can do this manually, or you can look around to figure out how to get Eclipse to do it for you.
  • Copy your module .jar file into the "libs" directory of your app's main project.
  • Add the module .jar to the project's classpath (again, Project > Properties > Java Build Path > Libraries > Add JAR).
  • 在 Eclipse 中创建一个新的 Java 项目(不是 Android 项目)并将模块的源代码移到那里。
  • 如果您的模块引用了 SDK 中的任何类,您需要将 Android SDK .jar 添加到项目的类路径(项目 > 属性 > Java 构建路径 > 库 > 添加 JAR)。
  • 当您的模块准备好使用时,将其 .class 文件捆绑到一个 .jar 中。您可以手动执行此操作,也可以环顾四周以找出如何让 Eclipse 为您执行此操作。
  • 将模块 .jar 文件复制到应用程序主项目的“libs”目录中。
  • 将模块 .jar 添加到项目的类路径(同样,项目 > 属性 > Java 构建路径 > 库 > 添加 JAR)。

Now you should be able to build multiple apps using the same .jar, while maintaining only one copy of the module's source code.

现在您应该能够使用同一个 .jar 构建多个应用程序,同时只维护模块源代码的一个副本。

Depending on your particular situation, this may or may not work any better for you than the standard Android library mechanism. But it's worth considering as an alternative.

根据您的特定情况,这可能比标准的 Android 库机制更适合您,也可能不适合您。但作为替代方案值得考虑。

回答by Swaroop

The Android documentation recommends another approach if there aren't too many "different APIs" used.

如果没有使用太多“不同的 API”,Android 文档推荐了另一种方法。

The idea is to use reflection instead of making direction references to the code. Make sure to use optimized reflection instead of lookups every time.

这个想法是使用反射而不是对代码进行方向引用。确保每次都使用优化的反射而不是查找。

References

参考

回答by Bryan Denny

You might want to consider using a source control system like Subversion (or GIT). Keep your most feature complete version in the trunk, and make branches for your separate versions that use different data sources or require minor layout changes, etc.

您可能需要考虑使用像 Subversion(或 GIT)这样的源代码控制系统。将你功能最完整的版本放在主干中,并为使用不同数据源或需要细微布局更改等的单独版本创建分支。