Android生成厂商包

来源:互联网 发布:clean my mac 怎么样 编辑:程序博客网 时间:2024/05/01 09:09

android厂商包

根据公司的业务需求,要在当前的app的基础上删减部分功能然后实现一个新的厂商包。 可能很多人都会有类似的需求。当时第一时间想到的办法就是分支,但是合并的时候好像还是挺复杂的。所以排除了这个办法。
那么问题来了,how to play?
这里提供一种我个人认为不错的实现方案。
在渠道包的概念上将一个厂商定义为一个渠道,将厂商和渠道等同。放弃了渠道(下面的例子是将umeng渠道id和厂商id统一)。
ps,下面的多渠道打包基于android studio,eclipse的同学可以看看然后自己去研究eclipse下的多渠道打包方案自行研究。我们以取umeng的渠道id为例子

  1. 在bulid.gradle文件中配置我们的渠道名
  productFlavors {        渠道名 {            applicationId "渠道名对应的包名"              versionCode 201  //从200开始新的2.0的版本            versionName "2.0.1"          }        渠道名 {            applicationId "渠道名对应的包名"            versionCode 1            versionName "1.0.0"         proguardFiles '混淆文件名'        }        渠道名 {           applicationId "渠道名对应的包名"            versionCode 1            versionName "1.0.0"            proguardFiles '混淆文件名'        }    }
这里提一下多包名的处理
           <service            android:name="com.umeng.message.UmengService"            android:exported="true"            android:label="PushService"            android:process=":push" >            <intent-filter>                <action android:name="APPLICATIONID.intent.action.START" />            </intent-filter>            <intent-filter>                <action android:name="APPLICATIONID.intent.action.COCKROACH" />            </intent-filter>            <intent-filter>                <action android:name="org.agoo.android.intent.action.PING_V4" />                <category android:name="umeng" />            </intent-filter>        </service>
这里是umeng的服务在xml中的例子这里将需要的填的包名替换成APPLICATIONID然后在build.gradle文件加入此段代码目前在替换过程中发现appicon无法替换一个图标 没有深究原因,已经接受了手动替换app图标的设定,如果有朋友知道此处如何修改欢迎交流
   android.applicationVariants.all { variant ->        variant.outputs.each { output ->            output.processManifest.doLast {                def manifestFile = output.processManifest.manifestOutputFile                def updatedContent = manifestFile.getText('UTF-8')                if ("渠道名1".equalsIgnoreCase("${variant.productFlavors[0].name}")) {                    updatedContent = updatedContent.replaceAll("@string/app_name","@string/渠道名1的名称).replaceAll("APPLICATIONID", "包名")                }                if ("渠道名2".equalsIgnoreCase("${variant.productFlavors[0].name}"))                {                    updatedContent = updatedContent.replaceAll("@string/app_name","@string/渠道名2的名称).replaceAll("APPLICATIONID", "包名")                }                manifestFile.write(updatedContent, 'UTF-8')            }        }    }

java代码中获取渠道包名然后通过渠道包名去组织不同的界面

    public static String getAppMetaData(Context ctx, String key)    {        if (ctx == null || TextUtils.isEmpty(key))        {            return null;        }        String resultData = null;        try        {            PackageManager packageManager = ctx.getPackageManager();            if (packageManager != null)            {                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);                if (applicationInfo != null)                {                    if (applicationInfo.metaData != null)                    {                        resultData = applicationInfo.metaData.getString(key);                    }                }            }        }        catch (PackageManager.NameNotFoundException e)        {            e.printStackTrace();        }        return resultData;    }
    public static int getCompanyID(Context con) {            String data =   AppUtils.getAppMetaData(con,"UMENG_CHANNEL");            if ("渠道名1".equals(data)) {                return "渠道名1的标记";            }else if ("渠道名2".equals(data)) {                return "渠道名2的标记";            }    }
这样我们就可以在java代码知道我们对应的渠道名 然后通过渠道名来对界面的图片文字已经隐藏显示进行设置。个人在采用这种设计的时候多个包之间的逻辑不适合变动太大。
  • 优点
    是一套代码有问题,只要修改了一个 其他的也就修改了。不需要分支然后再合并。
  • 缺点
    很明显这样会存在一些冗余的代码进行判断,已经冗余的资源文件。
0 0
原创粉丝点击