如何在Android Studio中自定义多个应用程序?

huangapple go评论77阅读模式
英文:

How to customize mutliple app in Android Studio?

问题

我在Android Studio中有一个项目,有两个不同的应用程序模块,但它们都在使用同一个Android库(核心代码)。我想在应用程序中更改一些资源和逻辑,但这些代码位于核心(Android库)中。我应该如何处理?如何自定义核心代码?

英文:

I have a project in Android Studio and two different app module but both of them are using this same Android library (core code). I want change some resources and logic in application but this code is in core (Android library). How do I approach this? How to customize core code?

答案1

得分: 1

我一直在询问相同的问题(https://stackoverflow.com/questions/60385546/android-modules-vs-flavor),经过一些研究,我发现你有不同的方法:

Gradle 版本:

为什么使用产品风味(Flavors)?

  • 它们解决了为应用程序的每个版本拥有单独的项目代码的问题,同时仍然保持一个项目代码。
  • 在你有免费和付费应用程序的情况下,你可以在免费版本中限制功能,并在付费版本中公开所有其他功能。
  • 在需要根据国家实现区域特定功能的另一种情况下,你可以为这种情况使用产品风味。
  • 白标签化(这些是由某家公司开发的应用程序,它们会被其他公司重新品牌和转售)。

优点

  • 它们解决了为应用程序的每个版本拥有单独的项目代码库的问题。

  • 它们保持代码整洁,使得在代码库中导航变得更加容易和快速,因为与特定产品风味相关的所有内容都将保存在它们相应的文件夹中。

缺点

  • (扩展)变体越多,复杂性就越大,从而使得维护代码库变得更加困难。
  • 在切换变体后,IDE有时需要花费时间来构建项目。

来源:https://levelup.gitconnected.com/simple-guide-to-android-product-flavors-674106455038


多模块

为什么?

  • 更快的构建时间。
  • 精细的依赖控制。
  • 提高在其他应用程序中的可重用性。
  • 提高代码库的所有权和质量。
  • 与软件包相比,边界更严格。

优点

  • 随着应用程序随着新功能的增长而扩展得很好
  • 中大型开发团队可以在不相互影响的情况下工作于不同的模块(合并冲突)
  • 将单元测试和UI测试封装到其特定功能中
  • 在模块之间保持资源分离。这提高了可读性和组织性
  • 在各自的模块中保留逻辑,可以将其隐藏在接口后面
  • 强制开发人员使其代码更好地组织和结构化
  • 改进的构建速度,因为模块中的更改意味着只需重新构建该模块

缺点

  • 在构建模块时增加了额外的样板代码
  • 总体上需要更多的开发时间
  • 每个模块需要一个Maven/Gradle文件
  • 在模块之间设置正确的导航可能会很难
  • 需要更多的预先计划,以确定最佳的代码结构,并确定共享代码库存储的位置。
  • 有限数量的在线资源展示最佳实践

来源:

同时查看Gradle文档以开始模块化。

英文:

I've been asking the same question and after some research, I've found that you have different approach:

Gradle Flavors:

Why Product Flavors?

  • They address the issue of having separate project code for each version of the app while still having one project code.
  • Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.
  • Given another scenario where you want to implement region-specific functions depending on the country, you can use product flavors for such a use case.
  • White labeling (these are apps that are developed by a certain company, and they are re-branded and resold by other companies).

Pros

  • They address the issue of having a separate project code base for each version of the app.

  • They keep the code tidy and makes it much easier and faster to navigate through the code base as everything related to the specific product flavor would be kept in their corresponding folders.

Cons

  • (Scaling Up) The more variants, the greater the complexity which thereby makes it harder to maintain the codebase.
    -IDEs sometimes takes time to build the project after switching between variants.

Source: https://levelup.gitconnected.com/simple-guide-to-android-product-flavors-674106455038


Multi-Module

Why ?

  • Faster build times.
  • Fine-grained dependency control.
  • Improve reusability across other apps.
  • Improves the ownership & the quality of the codebase.
  • Stricter boundaries when compared to packages.

Pros

  • Scales well as the application grows with new features
  • Medium to large development teams are able to work on different modules without affecting each other (Merge Conflicts)
  • Encapsulates unit and ui tests to their specific features
  • Keeps Resources separated between modules. Which improves readability and organization
  • Keeps logic contained in their own modules, which can be hidden behind interfaces
  • Forces the developer to keep their code better organized and structured
  • Improved build speed, as changes in a module means only that module will need to be rebuilt

Cons

  • Adds additional boilerplate around the construction of the modules
  • More development time overall
  • Requires a maven/gradle file for each module
  • Navigation between module activities can be difficult to setup correctly
  • Requires a lot more pre-planning on how best to structure code, and determining where shared code bases are stored.
  • Limited amount of online resources showing best practices

Source:

Also look at the gradle doc to start with modularizing.

答案2

得分: 0

你可以构建该库的 aar 文件,并通过将其简单复制到项目目录中的 lib 文件夹中,在主项目中使用它。

或者你可以使用类似 Jitpack 的服务构建它,并通过 Gradle 中的 implementation 方法将其添加到你的项目中。

英文:

You can build the library's aar and use it in your main projects by simply copying into lib folder in your project directory.

Or you can build it with services like Jitpack and add it to your project by the implementation method in Gradle.

huangapple
  • 本文由 发表于 2020年4月5日 16:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61039715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定