Skip to content

A simple injection library for Android. Uses the Android resources qualification mechanism to create concrete instances.

Notifications You must be signed in to change notification settings

menny/FrankenRobot

Repository files navigation

#FrankenRobot#

Build Status

A simple injection library for Android: uses Android resources qualification mechanism to map interfaces to concrete implementations.
FrankenRobot takes two string-array resources; one of canonical interface names, and the second of canonical concrete implementations.
You can specify a different implementation for every imaginable resource qualifier (be it API level, screen-size, locale, etc.), and FrankenRobot will instantize the most appropriate implementation using Android's resource qualifier mechanism.

##How To Use##

Dependency

I'm using JitPack to publish this library.

Add it in your build.gradle at the end of repositories:

repositories {
    //...
    maven { url "https://jitpack.io" }
}

Step 2. Add the dependency in the form

dependencies {
    compile 'com.github.menny:FrankenRobot:1.1.5'
}

Usage

This is how AnySoftKeyboard uses FrankenRobot: Create two string-array resources in the res/values folder (say, in a designated frankenrobot.xml file):
One for the interfaces definition

<string-array name="frankenrobot_interfaces">
    <item>com.anysoftkeyboard.devicespecific.DeviceSpecific</item>
</string-array>

And another string-array resource for the concrete implementations

<string-array name="frankenrobot_concreate_classes">
    <item>@string/frankenrobot_device_specific_implementation</item>
</string-array>

Specify the concrete implementation class name, first in the res/values folder:

<string name="frankenrobot_device_specific_implementation">com.anysoftkeyboard.devicespecific.DeviceSpecific_V3</string>

Where special implemenations are needed:
In the res/values-v5 folder:

<string name="frankenrobot_device_specific_implementation">com.anysoftkeyboard.devicespecific.DeviceSpecific_V5</string>

In the res/values-v7 folder:

<string name="frankenrobot_device_specific_implementation">com.anysoftkeyboard.devicespecific.DeviceSpecific_V7</string>

Etc.

Initialize FrankenRobot, and embody the interface

FrankenRobot frank = Lab.build(getApplicationContext(),
        R.array.frankenrobot_interfaces,
        R.array.frankenrobot_concreate_classes);
//using a diagram to create the monster
DeviceSpecific deviceSpecific = frank.embody(new FrankenRobot.Diagram<DeviceSpecific>(){});

It is guaranteed that the returned instance is the most suitable, based on the qualifier rules.
AnySoftKeyboard achives impressive backword compatibility using this method, using API-level-bound implementations, with no complex coding, and no reflection.

About

A simple injection library for Android. Uses the Android resources qualification mechanism to create concrete instances.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages