模块化的android项目 - 怎么样? [英] Modular android project - how?

查看:108
本文介绍了模块化的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上的问题

在这里,我已经找到了各种解决方案,如图书馆项目,从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.

广播接收者:这似乎是推荐的一切。请参见此处 here

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).

我的方法是拥有一个主包。该包将包含您上面提到的共享功能,并作为您的应用程序的中枢。然后,我将为不同的添加功能创建单独的子包。这允许您使用简单的import语句在主包中使用代码。从您的描述中,这些附加功能应该可以实现为 Fragment Fragment 几乎是一个独立的应用程序,除了它由code活动托管。根据这些添加功能的使用方式(我无法判断它们是否与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。然后,您将只需要初始化所需的片段,并在($)$ ; (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 非常有用。我通常使用 LocalBroadcastManager 以及 BroadcastReceiver 通知应用程序的其他部分,当一些 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天全站免登陆