Android - 来自单个代码库的多个 .apk 文件

发布于 2024-12-15 18:44:13 字数 248 浏览 5 评论 0原文

我在 android 中开发了 3 个应用程序,它们的主要功能是相同的,但 UI 看起来不同。屏幕的图像和背景颜色不同。

现在,我想创建一个代码库,从中我可以为 3 个应用程序生成多个 .apk 文件。

我尝试为 3 个应用程序的 src 文件夹创建 3 个不同的包。但我不知道如何为这些应用程序设置 res 文件夹。

需要有关创建单个代码库的指导,我们可以从中生成多个 .apk 文件,其中仅包含各自的 src 和 res 文件夹。

I had developed 3 applications in android where the major functionalities are the same but the UI looks different. Images and the background color of the screens are different.

NOw, i want to create a single code base from which i can generate multiple .apk files for the 3 apps.

I tried creating 3 different packages for src folder for the 3 apps. But i dont know how to set the res folder for these apps.

Need pointers on creating a single code base from which we can generate multiple .apk files which includes only the respective src and res folders.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

待天淡蓝洁白时 2024-12-22 18:44:13
  • 使用包含所有常用代码的 Android 库项目
  • 创建引用库项目的单独 Android 项目(您需要将清单复制到每个项目中,并确保所有组件都使用完整的 Java 包名称进行声明)。
  • 将特定于每个应用程序的任何资源(绘图、颜色等)放入各个项目资源文件夹中,它们将在构建时覆盖库项目中类似命名的资源。
  • Use an Android Library Project that contains all your common code.
  • Create separate Android projects that reference the Library Project (you will need to copy your Manifest into each of these and make sure all components are declared with their full Java package name).
  • Put any resources specific to each app (drawables, colors etc) into the individual project resource folders and they will override similarly named resources in the library project at build time.
離殇 2024-12-22 18:44:13

我认为最好的选择是使用 ant,您需要为每个构建添加一个 ant 目标并更改资源文件夹。
如果使用生成的build.xml,则res文件夹定义如下
因此您需要覆盖它

i think the best option is to use ant, you'll need to add an ant target for each build and change the resource folder.
if you use the generated build.xml, the res folder is defined like this
<property name="resource.absolute.dir" location="res" /> so you'll want to override that

笑红尘 2024-12-22 18:44:13

难道您不能将所有通用代码放入一个库项目中,然后仅从 3 个包含相关资源的独特项目中的每一个中引用该项目吗?

更新:使用 Gradle 构建系统时,此答案现已过时。

Can't you put all of your common code into a library project and then just reference that project from each of the 3 unique projects that each contain the relevant resources.

Update: This answer is now obsolete when using the Gradle build system.

少年亿悲伤 2024-12-22 18:44:13

为什么不使用单个应用程序,该应用程序根据用户设置的 SharedPreferences 值或安装时的上下文执行三项不同的操作。如果您确实想分开,则可以拥有三个不同的活动,然后决定从重定向到其中一个不同活动的静默主活动中启动哪个活动。

另一种方法是拥有一个独特的 Activity,该 Activity 在 onCreate 时动态地从 3 个不同的布局中自行扩展。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (...custom check for layout... equals(layout1)) {
        setContentView(R.layout.main_layout1);
    } else if (... equals(layout2)) {
        setContentView(R.layout.main_layout2);
    } else if (... equals(layout3)) {
        setContentView(R.layout.main_layout3);
    } else {
        throw new IllegalStateException("Unknown layout!");
    }
    ... your onCreate stuff....
}

它将使代码维护更容易(只需修改一个代码源,只需维护一个版本列表和变更集)

请查看此处:
如何在 Android 中使用 SharedPreferences 来存储、获取和编辑值

Why don't you use a single application, that does three different things based on SharedPreferences values set by the user, or from context at install time. If you really want to separate, you can have three different activities, and you decide which one to launch from a silent main Activity that redirects to either of the different ones.

An alternative is to have a unique activity that inflates itself dynamically from 3 different layouts at onCreate time.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (...custom check for layout... equals(layout1)) {
        setContentView(R.layout.main_layout1);
    } else if (... equals(layout2)) {
        setContentView(R.layout.main_layout2);
    } else if (... equals(layout3)) {
        setContentView(R.layout.main_layout3);
    } else {
        throw new IllegalStateException("Unknown layout!");
    }
    ... your onCreate stuff....
}

It will make code maintenance easier (only one code source to modify, only one version-list and changeset to maintain)

Check here:
How to use SharedPreferences in Android to store, fetch and edit values

葵雨 2024-12-22 18:44:13

我建议使用 Gradle 口味

它似乎很好地解释了所有基础知识。我今天刚刚完成向 Gradle 的转换,效果非常好。自定义应用程序图标、名称和字符串等。

正如网站所解释的,这种设计背后的部分目的是使其更加动态,并且更容易允许使用基本相同的代码创建多个 APK,这听起来与您的类似正在做。

另请参阅最近的问题 我参考了您的项目结构并为每个应用程序使用了自定义代码。

I would suggest using Gradle flavors.

It seems to explain all the basics really well. I just finished converting to Gradle today, and it works great. Custom app icons, names, and strings, etc.

As the website explains, part of the purpose behind this design was to make it more dynamic and more easily allow multiple APKs to be created with essentially the same code, which sounds similar what you're doing.

Also see a recent question I had, referring to your project structure and using custom code for each app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文