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

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

问题描述

我的方案

我要实现一个模块化Android应用程序。有从中我应该能够调用其他模块的芯模块。每个模块提供了不同的功能给用户。想象一下,我想提出一个城市指南,则一个模块可能包含兴趣点,另外一个事件日历和第三个pre定义的城市指南地图。该模块包含的观点在核心模块的活动被加载(如仪表板,其中每个模块把它的项目/图片)。它们还含有应被调用(当用户在仪表板上水龙头的项等)的活动。据我知道,我将只需要在核心模块的数据库和/或preferences。核心模块的插件模块使用类(公用事业),例如连接到后端的时候。

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,我在X $ C $ç目标做到了这一点。我有一个项目,并根据客户的需求,我只编译相关的模块。它甚至会更好,如果用户可以安装每当他想要的模块,而无需重新安装核心应用程序。

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工作室+什么的,......但我还是不明白...如何是一个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.

我需要的是什么,至少要能使用相同的code与功能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).

我的做法是有一个主包。该软件包将包含您在上面提到并作为枢纽您的应用程序共享功能。然后我会创建单独的子包为不同的功能上添加。这使您可以仍然使用code在主包用一个简单的import语句。从你的描述这些附加功能或许应该被实现为片段。 A 片段几乎与异常的独立的应用程序,它是托管由活动。根据如何将这些添加上所使用的功能(我不知道他们是否涉及到用户界面,只需后台处理等),你可以很容易地有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.

要控制哪些使用code的部分,我只想设置一个简单的开关类(它甚至可能会推出,我不能从你的描述告诉上面的第一个活动的一部分)。在这里我要检查一些设置指示哪些应用程序的部分将是积极的。这可以用共享preferences 来存储一个特定的配置,例如轻松定义用A和B,前交付最终项目。然后,您可以只初始化你需要的片段,并显示它们是(1)单独在片段布局元素或的FrameLayout ; (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.

我跟随你的广播接收器链接,我仍然不知道为什么他们是比一切其他建议。正确使用一个广播接收器是非常有用的。我倾向于使用 LocalBroadcastManager 随着一个广播接收器来通知应用程序的其他部分时,一些的AsyncTask ,例如:下载大量的数据,就完成了。然后该应用的这些部分可以访问本地数据库或处理下载的自己的时间的信息。我不会用一个广播接收器来调节应用程序的部分,如果这是你在找什么。我反而只需要使用共享preference 文件在运行时设置的配置。

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天全站免登陆