A lightweight cross-module dependency-injection library for Android.
- AppInject aar library
- AppInject gradle plugin
Root project's build.gradle
:
buildscript {
repositories {
//...
jcenter()
}
dependencies {
// ...
classpath "com.lyc:app-inject-plugin:latest.release"
}
}
In your app
module (module to output apk), build.gradle
:
apply plugin: 'com.android.application'
// must be applied after 'application' plugin
apply plugin: "com.lyc.app-inject-plugin"
dependencies {
// provide Annotations and AppInject API
implementation "com.lyc:app-inject:latest.release"
}
It's recommond to provide AppInject API in base module as follow:
dependencies {
// pass dependency to other modules
api "com.lyc:app-inject:latest.release"
}
Define your interface(api
) in module A
. For example:
Or abstract class.
@InjectApi
supports both abstract class and interface.
// oneToMany default value is false
// if oneToMany == true
// you can create more than one implementation class of this interface
// else you can only create one class
@InjectApi(oneToMany = false)
public interface ITestApi {
String logMsg();
}
Implement this interface (or extends class) in any module. We call it module B
:
@InjectApiImpl(api = ITestApi.class)
public class TestApiImpl implements ITestApi {
@Override
public String logMsg() {
return "I'm TestApiImpl!";
}
}
Now you can get instance of ITestApi at any module which depends on module A
. No need to depend on module B
:
// use AppInject.getInstance().getOneToManyApiList() if oneToMany == true
val testApi = AppInject.getInstance().getSingleApi(ITestApi::class.java)
Log.d(TAG, testApi.logMsg())
Use kotlin can simplify call to AppInject
, for example:
inline fun <reified T> getSingleApi(): T? {
return AppInject.getInstance().getSingleApi(T::class.java)
}
inline fun <reified T> getOneToManyApiList(): List<T> {
return AppInject.getInstance().getOneToManyApiList(T::class.java)
}
use it:
val testApi = getSingleApi<ITestApi>()
// or
val testApi:ITestApi? = getSingleApi()
# for createMethod getInstance()
-keepclasseswithmembers @com.lyc.appinject.annotations.InjectApiImpl class * {
public static * getInstance();
}
Local modifies on
app-inject
module will work immediately.
Use local maven forapp-inject-plugin
module.
Step1 Modify file common.properties
at root directory.
testInjectPluginLocal=false
testRemoteRepo=false
Step2 Run gradle updateArchives
to build and publish to local maven (directory repo
at root directory). repo
directory should be created.
Step3 Modify file common.properties
at root directory.
testInjectPluginLocal=true
Step4 Gradle sync. Now you can run sample
module.
Use
jcenter
forapp-inject
andapp-inject-plugin
. Local modified on both module will not work.
Step1 Modify file common.properties
at root directory.
testInjectPluginLocal=false
testRemoteRepo=true
Step2 Sync project and run sample
module