gpt4 book ai didi

android - 如何使用模块创建库?

转载 作者:行者123 更新时间:2023-12-03 05:12:50 24 4
gpt4 key购买 nike

我想创建几个库(核心和模块),将所有这些库添加到应用程序并从核心库中的模块调用方法。所有模块都应该是可选的。例如,在我看来:

申请 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 bundle 在一起.

然后在你的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 设置起来更复杂,但是它处理这个用例非常好。

关于android - 如何使用模块创建库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41467554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com