Back to Home

DroidParts - library for Android 8-in-1

android development · ioc · dependency injection · object-relational mapping · orm · json parser · rest-client

DroidParts - library for Android 8-in-1

With this article, I open a series devoted to the development of Android applications. But not typical for Google Play, written, obviously, by the back left mNogoy, but applications that are correct and elegant. In this matter, DroidParts , the Swiss knife of Android libraries with 8 blades , will help us :

  1. Dependency Injection : initialization of system and custom dependencies;
  2. Object-Relational Mapping for SQLite: CRUD operations “out of the box”;
  3. Support for Fragments , ActionBar Sherlock;
  4. Simple (de) serialization of JSON ;
  5. AsyncTasks, IntentService with Exception processing;
  6. RESTful HTTP client with JSON support;
  7. L. og without a tag, configurable via AndroidManifest;
  8. Utilities such as cached asynchronous http-loader, View and Intent helpers.


Literally an hour ago *, the first version of the library was released, but first ...

For a long time I was a supporter of the old-school approach with code in the style of:

private Button btn;
onCreate(Bundle b) {
	super.onCreate(b);
	btn = (Button)findViewById(R.id.btn);
}

Until he participated in a project using RoboGuice. And after:

@­InjectView
private Button btn;

there was no voluntary return back.

RoboGuice, on the other hand, was nervous about the size and still unreleased version 2.0 with Fragments support. Also, ORM needed a third-party library. And Gson to make working with JSON easier. Wrong this, pull dependencies across multiple application sizes.

In general, following the principle of " if you want something done right, do it yourself ", I started just for fun .

What came of it is available in version 0.5 at github.com/yanchenko/droidparts and consists of:
  • base - DI, ORM, Activities and other key components;
  • extra - optional RESTful client and various utilities;
  • support-actionbarsherlock - support for Fragments, ActionBar and tabs in conjunction with the default ActionBarSherlock ;
  • sample is a sample application.
We will proceed with the consideration of the latter.

To start making git clone github.com/yanchenko/droidparts.gitand Eclipse-import projects from base, extra, sample.
The first two of them are libraries (Android library project).
In the project.propertieslatter, which is a full-fledged application, pay attention to the line proguard.config=../proguard.cfgpointing to the modified obfuscator config used when exporting to .apk.

Although DroidParts contains custom org.droidparts.Application, it makes sense to inherit from it only if the injection is needed in the Application itself. There is no such need for a simple application. But you need an injection of your own dependencies.
In AndroidManifest.xmlline:


declares a class that inherits org.droidparts.inject.AbstractDependencyProviderand is a factory of the objects we need.

The scheme is simple: each method DependencyProvider'a returns an instance of one of the classes available for injection. Two types of methods are supported: called without parameters and with a single parameter Context. In the case when an injection is made into an Activity, the transferred Context will be exactly to it, which is important for dependencies associated with the UI.

Examples in the code, including how singles are implemented (singletons, ahaha, I love the terms in translation), you can see directly in org.droidparts.sample.DependencyProvider.
For the injection of system resources and services, additional methods, of course, are not needed.

The injection itself is done for fields annotated@­Inject...when calling one of the methods Injector.get().inject(...). A manual call can be skipped inheriting from Activity, Serviceetc. from org.droidparts.
In the appendix org.droidparts.sample.activity.EntryListActivityis an example of such an approach.
Pay attention to:

	@Override
	public void onPreInject() {
		setContentView(R.layout.activity_entry_list);
	}

All calls onCreate(...)will occur after the Injector completes. In this case, this would lead to an unknown Views.

On this chaotic understatement, I complete the introductory article. Examples of DI, ORM and JSON serialization, sufficient to start using the library, are in the application. A detailed review and best practices is a topic for further articles.
In the near future I will tell you how you can replace your own constructors for Activity(hint: this pattern is in the application).

I will answer questions here or there: stackoverflow.com/questions/tagged/droidparts .

Thanks, and please write less code! (:

* as of last night.

Read Next