我想创建一些库(核心和模块),将所有这些库添加到应用程序中,然后从核心库中的模块调用方法。所有模块都应该是可选的。例如,如我所见:
应用范围build.gradle
:
compile('core-library:1.0@aar')
compile('module1-library:1.0@aar')
compile('module2-library:1.0@aar')
compile('module3-library:1.0@aar')
在每个模块中,使用相同的名称定义类和方法:
public class ModuleClass {
public int moduleMethod1() {
// Do something and return result
return 1;
}
public String moduleMethod2() {
return "Some String";
}
}
在核心库中:
for(ModuleClass c : getAllModules()) {
Log.d("tag", "Result: " + c.moduleMethod1() + " / " + c.moduleMethod2();
}
当然那只是伪代码。如何实现它或类似的东西?
更新:核心模块build.gradle
:
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
minSdkVersion 9
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField "String[]", "KNOWN_MODULES", "{" +
"\"module1\"," +
"\"module2\"" +
"}"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compileOnly project(':module1')
compileOnly project(':module2')
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
}
您可以尝试List<Class>
在中添加core-library
。尝试加载列表时必须非常小心,但是应该能够做到。
首先,您moduleN-library
需要确保compileOnly
它不会与捆绑在一起core-library
。
然后,在您core-library
的单例中安全列出所有可能的模块类。
package com.jbirdvegas.test;
import java.util.HashMap;
import java.util.Map;
public class ModuleClassManager {
private static final Object LOCK = new Object();
private static ModuleClassManager instance;
private Map<Class, Methods> moduleClasses = new HashMap<>();
private ModuleClassManager() {
try {
moduleClasses.put(MayNotExist1.class, new MayNotExist1());
} catch (Exception ignored) {
}
try {
moduleClasses.put(MayNotExist2.class, new MayNotExist2());
} catch (Exception ignored) {
}
try {
moduleClasses.put(MayNotExist3.class, new MayNotExist3());
} catch (Exception ignored) {
}
}
public static ModuleClassManager getInstance() {
if (instance == null) {
synchronized (LOCK) {
instance = new ModuleClassManager();
}
}
return instance;
}
public void callMethodOnAll() {
moduleClasses.forEach((aClass, methods) -> methods.methodOne());
}
// the below interface in the real world would be located
// in the `core-library` and the classes that implement the
// interface, `Methods` in this example, would exist in the
// module's they represent.
interface Methods {
void methodOne();
}
class MayNotExist1 implements Methods {
@Override
public void methodOne() {
}
}
class MayNotExist2 implements Methods {
@Override
public void methodOne() {
}
}
class MayNotExist3 implements Methods {
@Override
public void methodOne() {
}
}
}
或者...使用OSGi。OSGi的设置更为复杂,但是它可以很好地处理此用例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句