多个SDK控制管理

需求:制作一个公共组件,可以实现多个SDK想用哪个用哪个,集中管理

组织方式:

架构形式

注意点:

1.sdk必须通过maven库来compile,因为jar会打到aar中;所以library和主module都要compile

2.控制文件SdkConfig.properties(放在src/main/assets下)

O7Sdk=false
UserCenterSdk=true
#UserCenterSdk为false时需要注释掉UserCenterForeign
UserCenterForeign=true
UMengSdk=false

3.主工程module的build.gradle

try {
        Properties props = new Properties();
        props.load(new FileInputStream(rootProject.file("src/main/assets/SdkConfig.properties")));
        props.each({
            println("Adding property to rootProject.ext: " + it.key + " = " + it.value);
            rootProject.ext[it.key] = it.value
            if ("O7Sdk".equals(it.key) && "true".equals(it.value)) {
                compile 'com.outfit7.android.aars:o7sdk:1.1.0'
                println("compile o7sdk ")
            } else if ("UserCenterForeign".equals(it.key)) {
                if ("true".equals(it.value)) {
                    compile 'user-center:tracksdk_foreignlogin:1.0.0'
                    println("compile UserCenterForeign ")
                } else if ("false".equals(it.value)) {
                    compile 'user-center:tracksdkDex:1.0.0'
                    println("compile UserCenterChina ")
                }
            } else if ("UMengSdk".equals(it.key) && "true".equals(it.value)) {
                compile 'com.umeng.sdk:common:1.5.0'
                compile 'com.umeng.sdk:analytics:7.5.0'
                println("compile UMeng ")
            }
        })
    } catch (Exception e) {
    }

4.生命周期

@Override
    public void onResume() {
        Log.d(TAG, "CentralizationImpl--onResume");
        gameCenter.onResume();
        o7Impl.onResume();
        新sdk.onResume();
    }

5.新增sdk实现类要增加如下

    public static final String 变量名 = "键的值";
    private boolean canSdkUsed;

6.其中键的值和主工程配置文件SdkConfig.properties里要一致,用于管理sdk是否可用

    O7Sdk=false
    gameCenterSdk=false
    键的值=true

并在构造函数里取配置文件的值

     String key = SdkConfig.getInstance().getKey(变量名);
      canSdkUsed = TextUtils.isEmpty(key) ? false : (key.equals("false") ? false : true);
      Log.d(TAG, "GameCenterImpl--canSdkUsed:" + canSdkUsed);

然后每个方法前面都要添加

        if (!canSdkUsed) {
            return;
        }

7.取键值类

public class SdkConfig {
    private static Map configMap;
    private static SdkConfig instance;

    public static final String O7SDK_KEY = "O7Sdk";
    public static final String UMENGSDK_KEY = "UMengSdk";
    public static final String USER_CENTER_SDK = "UserCenterSdk";
    public static final String USER_CENTER_FOREIGN = "UserCenterForeign";

    public SdkConfig() {
    }

    public static SdkConfig getInstance() {
        if (instance == null) {
            instance = new SdkConfig();
        }
        return instance;
    }

    public static void init(Context context) {
        if (configMap != null) {
            return;
        }
        configMap = new HashMap();
        String fileName = "SdkConfig.properties";
        InputStream in = null;
        try {
            in = context.getAssets().open(fileName);
            Properties properties = new Properties();
            properties.load(in);
            Iterator<Map.Entry<Object, Object>> iterator = properties.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Object, Object> entry = iterator.next();
                configMap.put(entry.getKey(), entry.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getKey(String key) {
        if (configMap != null) {
            return (String) configMap.get(key);
        } else {
            return null;
        }
    }
}

如果有更好的方案,欢迎留言 

原文地址:https://www.cnblogs.com/anni-qianqian/p/9025627.html