应用模块上的Android Studio最佳做法 [英] Android Studio best practices on app modules

查看:101
本文介绍了应用模块上的Android Studio最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Android上模块的概念.我已经阅读了官方文档中的这篇文章,但我有点使困惑.声明如下:

I'm trying to understand the concept of modules on Android. I've read this article on the official documentation, but I'm a bit confused. Here is stated:

Android应用程序模块是应用程序的源代码,资源文件和应用程序级别设置(例如模块级别的构建文件,资源文件和Android清单文件)的容器.应用程序模块的内容最终会内置到安装在设备上的.apk文件中.

An Android Application Module is the container for your application's source code, resource files, and application level settings, such as the module-level build file, resource files, and Android Manifest file. The application module contents are eventually built into the .apk file that gets installed on a device.

,这几乎是我在此找到的所有文档.我仍然不知道在哪种情况下创建不同的模块是有意义的.我知道具体情况可能会有所不同,但是理想情况下模块的最小逻辑大小是多少?

and that's pretty much all the documentation I've found on this. I still don't understand in which cases would make sense to create different modules. I know that it may be vary from case to case, but ideally what would be the minimum logical size of a module?

实际示例

我正在构建一个带有抽屉视图的应用程序.抽屉的每个按钮将打开一个片段,其中将包含一些逻辑.制作一个仅包含一个片段和一些不包含任何活动的java类的模块有意义吗?

I'm building an app with a drawer view. Each button of the drawer will open a fragment which will have some logic to it. Would it make sense to make a module that contains only a fragment and a few java classes with no activities in it?

我真的很想知道哪些是最佳做法.

I'd really like to know which are the best practices on this.

谢谢.

推荐答案

也许将代码和资源分解为模块的最佳论据是可重用性.如果不是针对该模块的多个依赖项,为什么还要将这些代码完全分离到一个模块中呢?如果您只有一个应用程序,则不需要将此代码分解为一个模块.

Perhaps the best argument for breaking code and resources into modules is reusability. If not for multiple dependents of such a module, why would you need to separate this code out into a module at all? If you only ever have one application, there's not much need to have this code broken out into a module.

一旦您有多个共享相同代码的应用程序,那么我认为一个模块是合理的.一个模块可能有很多东西.它可能是包含大部分代码的基本应用程序.它可能包含应用程序的模型"方面,或者可能包含API或网络层.它可以是专有算法的容器.您可能打算将某些通用代码开源.谁知道?

As soon as you have multiple applications sharing the same code, then I think a module is justified. A module could be many things. It could be a base application that contains the bulk of the code. It could contain the "Model" aspect of your application, or maybe an API or networking layer. It could be a container for proprietary algorithms. It could be some generalized code that you're planning to open-source. Who knows?

从相反的角度考虑它.如果您真的不需要,现在进行此操作有什么风险?也许精神上的开销?如果您没有充分的理由首先考虑并维护该模块,那么可能比它的价值还要麻烦.

Consider it from the opposite angle. What's the risk of doing this now if you don't really need to? Maybe mental overhead? Having to think about and maintain this module may be more trouble than its worth if you don't have a good reason to do it in the first place.

推迟此决定的费用是多少?有什么令人信服的理由为什么以后无法将这些代码从主应用程序中重构到模块中?

What is the cost of deferring this decision? Is there any compelling reason why this code couldn't be refactored out of the main application into a module at some later point?

单个UI组件的片段只有一个片段,听起来好像不需要在自己的模块中.

Something as small as a single Fragment for a single UI component sounds like something that doesn't need to be in its own module.

这篇关于应用模块上的Android Studio最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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