多渠道打包应用加载不同的SDK

遇到一个项目,需求场景是使用了OCR识别,由于暂未确定采购方案,因此对三种不同的厂家的SDK均集成进项目,打包出三种不同的APK。

 

一、首先多渠道打包部分:

manifest:

设置了一个key为THIRD_SDK的占位符

build.gradle:

对manifestPlaceholders配置,配置的值name为@return The name of this project. Never return null.

(对了,这里也可以在buildTypes中对manifestPlaceholders进行设置,这样可以对debug和release版本做不同处理)

productFlavors {
        yunMai{
            versionName "1.10.01.0"
        }
        heHeWenTong{
            versionName "1.20.01.0"
        }
        zhongAnWenTong{
            versionName "1.30.01.0"
        }
    }

    productFlavors.all{
        flavor->
            flavor.manifestPlaceholders = [THIRD_SDK:name];
    }

 开始通过不同的打包选择不同的SDK,关键代码及实现步骤:

1.先通过ApplicationInfo获取到定义的${THIRD_SDK}的值,也就是项目名

2.根据不同的项目名字去HashMap中获取到对应的SDKSelector类,这三个类都是接口IThirdSDKSelector的实现

3.接口中的抽象方法的功能就是返回几个打开一些摄像机调用的OCR识别界面的Intent

4.再去第三方的sdk代码中把识别结果的返回方式重写下,在我们的结果页统一接收

private static final String KEY_META_DATA = "THIRD_SDK";


private static final HashMap> gThirdSDKSelectorTable = new HashMap>() {
        {
            put(VALUE_META_DATA_YUNMAI, YunMaiSDKSelector.class);
            put(VALUE_META_DATA_HEHEWENTONG, HeHeWenTongSDKSelector.class);
            put(VALUE_META_DATA_ZHONGANWENTONG,ZhongAnWenTongSDKSelector.class);
        }
    };




private static String getMetaData(){
        if (gMetaData == null){
            PackageManager packageManager = CompositiveApplication.getInstance().getPackageManager();
            if (null != packageManager){
                try {
                    ApplicationInfo info = packageManager.getApplicationInfo(CompositiveApplication.getInstance().getPackageName(), PackageManager.GET_META_DATA);
                    if (info != null && info.metaData != null){
                        gMetaData = info.metaData.getString(KEY_META_DATA);
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return gMetaData;
    }


public IThirdSDKSelector getThirdSDKSelector(){
        if (mThirdSdkSelector == null){
            String metaData = getMetaData();
            Class cls = gThirdSDKSelectorTable.get(metaData);
            if (null != cls){
                try {
                    mThirdSdkSelector = cls.newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        if (mThirdSdkSelector == null){
            mThirdSdkSelector = new YunMaiSDKSelector();
        }

        return mThirdSdkSelector;
    }


IThirdSDKSelector:

public interface IThirdSDKSelector {
    public Intent getMainActivityIntent(Context context);

    public Intent getAddCarNumberActivityIntent(Context context);

    public Intent getAddPassportActivityIntent(Context context);

    public Intent getCarNumberDetectActivitityIntent(Context context);

    public IThirdSDKDataParser getDataParser();
}

YunMaiSDKSelector:

public class YunMaiSDKSelector implements IThirdSDKSelector {

    @Override
    public Intent getMainActivityIntent(Context context) {
        return new Intent(context, MainActivity.class);
    }

    @Override
    public Intent getAddCarNumberActivityIntent(Context context) {
        return new Intent(context, AddCarNumberActivity.class);
    }

    @Override
    public Intent getAddPassportActivityIntent(Context context) {
        return new Intent(context, AddPassportActivity.class);
    }

    @Override
    public Intent getCarNumberDetectActivitityIntent(Context context) {
        return new Intent(context, CameraActivity.class);
    }

    @Override
    public IThirdSDKDataParser getDataParser() {
        return new YunMaiSDKDataParser();
    }
}

 

你可能感兴趣的:(android)