模块化 android 项目 - 如何? [英] Modular android project - how?

查看:33
本文介绍了模块化 android 项目 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景

我必须实现一个模块化"Android 应用程序.有一个核心模块,我应该能够从中调用其他模块.每个模块为用户提供不同的功能.想象一下,我正在制作一个城市指南,然后一个模块可能包含一张带有 POI 的地图,另一个是活动日历,第三个是预定义的城市指南.这些模块包含要在核心模块的活动中加载的视图(如每个模块放置其项目/图片的仪表板).它们还包含应该被调用的活动(比如当用户点击仪表板上的项目时).据我所知,我只需要核心模块中的数据库和/或首选项.插件模块"使用核心模块的类(实用程序),例如连接到后端时.

I have to implement a "modular" android app. There is a core module from which I should be able to invoke other modules. Each module provides a different feature to the user. Imagine I am making a city guide, then one module may contain a map with POIs, another one an event calendar and a third one pre-defined city guides. The modules contain views to be loaded in activities of the core module (like dashboards where each module puts its item/picture). They also contain activities which should be invoked (like when a user taps an item on the dashboard). As far as I know, I will need a database and/or preferences only in the core module. The "plug-in modules" use classes (utilities) of the core module, e.g. when connecting to the backend.

我在 iOS 上的解决方案

对于 iOS,我通过 XCode 中的目标实现了这一点.我有一个项目,根据客户的需要,我只编译相关模块.如果用户可以随时安装模块,而无需重新安装核心"应用程序,那就更好了.

For iOS, I achieved this with targets in XCode. I have one project and depending on the customer's needs I compile only the relevant modules. It would be even better if the user can install modules whenever he wants, without the need of reinstalling the "core" application.

我在 Android 上的问题

在SO中,我已经找到了各种解决方案,例如库项目,从Eclipse切换到Android Studio +东西,使用包管理器和广播接收器......但我仍然不明白...... android的模块化如何要实现的应用?

In SO, I already found various solutions like library project, switching from Eclipse to Android Studio + something, using package manager and broadcast receiver... But I still don't understand... How is the modularity of an android application to be achieved?

以下是我看到的一些具体问题:

Here are some concrete problems that I see:

  • 库:我的模块都使用核心模块的类,所以它们不是独立的.我根据我需要的灵活性使用接口/继承来实现模块化.

  • Libraries: My modules all use classes of the core module, so they are not independent. I achieve the modularity by using interfaces/inheritance depending on the flexibility that I need.

广播接收器:这似乎不是推荐的所有内容.例如,请参见此处此处.

Broadcast receiver: This seems to be everything else than recommended. See, for example, here or here.

我需要的是,至少,能够使用相同的代码将具有功能 A 和 B 的应用程序交付给一个客户,并将具有 B 和 C 的应用程序交付给另一个客户.而且,直到现在,我都不知道如何实现它.

What I need is, at least, to be able to use the same code for delivering app with features A and B to one customer and with B and C to another one. And, until now, I have no idea how to achieve it.

PS:我不想使用脚本,我不熟悉.

PS: I don't want to use scripting, I am not familiar with that.

推荐答案

我不认为这个模块化"应用程序与一个应用程序有什么不同,它有几个包,每个包都包含离散的功能,适用于某些列表设置或外部参数(由用户或您提供).

I don't see this "modular" app as anything different from one app, with several packages, each containing discrete functionality, that is adapted to some list of settings or external parameter (either provided by the user or you).

我的方法是拥有一个主"包.该包将包含您上面提到的共享功能,并作为您的应用程序的中心.然后我会为不同的附加"功能创建单独的子包.这使您仍然可以通过简单的导入语句使用主包中的代码.根据您的描述,这些附加功能可能应该实现为 Fragment.Fragment 几乎是一个独立的应用程序,除了它由 Activity 托管".根据这些附加功能的使用方式(我不知道它们是否与 UI 相关,只是后台处理等),您可以轻松拥有 4 个不同片段中的 3 个,并选择在运行时仅加载其中的 1、3 或 2 个.

My approach would be to have a "main" package. That package would contain the shared functionality you mention above and serve as the hub for your application. I would then create separate sub-packages for the different "add on" functionality. This allows you to still use the code in your main package with a simple import statement. From your description these additional functions should probably be implemented as a Fragment. A Fragment is almost a stand alone application with the exception that it is "hosted" by an Activity. Depending on how these add on functions are used (I cannot tell if they relate to the UI, just background processing etc) you could easily have 3 of 4 different fragments and choose to load only 1 or 3 or 2 of them at runtime.

为了控制使用代码的哪些部分,我只需设置一个简单的切换类(它甚至可能是启动的第一个活动的一部分,我无法从上面的描述中看出).在这里,我将检查一些设置,指示应用程序的哪些部分将处于活动状态".这可以使用 SharedPreferences 轻松定义以存储特定配置,例如在交付最终项目之前使用 A 和 B.然后,您只需初始化所需的片段并在 Fragment 布局元素或 FrameLayout 中单独显示它们 (1);(2) 集中在其他一些视图结构中,例如 ViewPager.

To control which parts of the code are used I would just set up a simple switching class (it could even be part of the first activity launched, I cant tell from your description above). Here I would check for some setting indicating which parts of the app will be "active." This could be easily defined using SharedPreferences to store a specific configuration, e.g. use A and B, prior to delivering the final project. You would then just initialize the fragments you need and display them either (1) individually in a Fragment layout element or FrameLayout; (2) collectively in some other view structure like a ViewPager.

我在 BroadcastReceiver 上关注您的链接,但我仍然不确定为什么它们是除了推荐之外的所有其他内容".正确使用 BroadcastReceiver 非常有用.我倾向于使用 LocalBroadcastManagerBroadcastReceiver 来通知应用程序的其他部分,当一些 AsyncTask,例如下载了很多资料,就完成了.然后,应用程序的这些部分可以访问本地数据库或自行处理下载的信息.如果这是您正在寻找的内容,我不会使用 BroadcastReceiver 来调制应用程序的某些部分.我会改为使用 SharedPreference 文件在运行时设置配置.

I follows your links on the BroadcastReceiver and I am still not sure why they are "everything else than recommended." Used correctly a BroadcastReceiver is very useful. I tend to use a LocalBroadcastManager along with a BroadcastReceiver to notify other sections of the app when some AsyncTask, e.g. downloading a lot of data, is complete. These parts of the app can then access a local database or process the information downloaded on their own time. I wouldn't use a BroadcastReceiver to modulate parts of the app if that is what you're looking for. I would instead just use a SharedPreference file to set the configuration at runtime.

这篇关于模块化 android 项目 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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