[转载]一键打包出多个不同包名,不同应用名称和图标的APK

[转载] http://blog.csdn.net/qq_27942511/article/details/54286783

此Demo的原理与多渠道打包的原理相同(动态设定App名称,应用图标,替换常量,更改包名,变更渠道)
最近有一个需求,就是一套代码要根据不同的客户打包出不同包名,不同appName,图标的apk,如果一个客户更改一次打包出一个apk的话效率非常的低,并且不利于维护
本demo的软件环境是AS,ES现在已经逐渐被AS取代,所以后期都会转向AS开发,主要工作就是修改moudle中的build.gradle文件,下面是build.gradle的所有配置:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
    applicationId "com.jd.demo"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
    app1 {
        manifestPlaceholders = [str: "releaseStr", package_name: "com.jd.cloud1"]
        applicationId "com.jd.cloud1"
        resValue "string", "app_name", "测试1"
        resValue "bool", "isrRank", 'true'
        manifestPlaceholders = [ENVIRONMENT: "app1",
                                app_icon   : "@drawable/icon1"]
    }

    app2 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud2"]
        applicationId "com.jd.cloud2"
        resValue "string", "app_name", "测试2"
        resValue "bool", "isrRank", 'true'
        manifestPlaceholders = [ENVIRONMENT: "app2",
                                app_icon   : "@drawable/icon2"]
    }
    app3 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud3"]
        applicationId "com.jd.cloud3"
        resValue "string", "app_name", "测试3"
        resValue "bool", "isrRank", 'true'
        manifestPlaceholders = [ENVIRONMENT: "app3",
                                app_icon   : "@drawable/icon3"]
    }
    app4 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud4"]
        applicationId "com.jd.cloud4"
        resValue "string", "app_name", "测试4"
        resValue "bool", "isrRank", 'true'
        manifestPlaceholders = [ENVIRONMENT: "app4",
                                app_icon   : "@drawable/icon4"]
    }
    app5 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud5"]
        applicationId "com.jd.cloud5"
        resValue "string", "app_name", "测试5"
        resValue "bool", "isrRank", 'true'
        manifestPlaceholders = [ENVIRONMENT: "app5",
                                app_icon   : "@drawable/icon5"]
    }
}
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
}

下面就来逐个来解析这些字段的具体含义:

1. 不同环境,不同包名:
productFlavors {
    app1 {
        manifestPlaceholders = [str: "releaseStr", package_name: "com.jd.cloud1"]
        applicationId "com.jd.cloud1"
    }

    app2 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud2"]
        applicationId "com.jd.cloud2"
    }
    app3 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud3"]
        applicationId "com.jd.cloud3"
    }
    app4 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud4"]
        applicationId "com.jd.cloud4"
    }
    app5 {
        manifestPlaceholders = [str: "devStr", package_name: "com.jd.cloud5"]
        applicationId "com.jd.cloud5"
    }

在defaultConfig中会默认的配置一个applicationId,但是这里会覆盖掉默认的applicationId

2. 不同环境,不同appName:
productFlavors {
    app1 {
        resValue "string", "app_name", "测试1"
        resValue "bool", "isrRank", 'true'
    }

    app2 {
        applicationId "com.jd.cloud2"
        resValue "string", "app_name", "测试2"
        resValue "bool", "isrRank", 'true'
    }
    app3 {
        resValue "string", "app_name", "测试3"
        resValue "bool", "isrRank", 'true'
    }
    app4 {
        resValue "string", "app_name", "测试4"
        resValue "bool", "isrRank", 'true'
    }
    app5 {
        resValue "string", "app_name", "测试5"
        resValue "bool", "isrRank", 'true'
    }

注意:需要将res/values/string.xml文件中的app_name字段删除,这样打包之后就会根据不同的环境加载不同的app_name.
原理就是使用resValue指令来动态的定义String资源,这里根据不同的环境定义了appName字段的内容,所以当引用到appName资源的时候会根据环境的不同区加载不同的内容。同理:利用这种方法可以动态的添加color,dimens资源。

3. 不同环境,不同的常量:
  1. 定义字段:
    这里会在app/build/source/BuildConfig/dev/com.lyl.dev/BuildConfig中生成相应的资源
productFlavors {
dev {
    buildConfigField "String", "ENVIRONMENT", '"app1"'
}
stage {
    buildConfigField "String", "ENVIRONMENT", '"app2"'
}
prod {
    buildConfigField "String", "ENVIRONMENT", '"app3"'
}
}
  1. 引用字段:
public class Constants {
public static final String ENVIRONMENT = BuildConfig.ENVIRONMENT
}

注意:这里有个小细节,看其中第三个参数,是先用了“’”,然后在用了“””,这种语法在 Java 里可能比较陌生,但是在很多其他语言中,这种用法是很常见的。
它的意思是 “app*” 这个整体是属于一个字符串,至于为什么要这么写,你把单引号去掉,然后去 app/build/source/BuildConfig/dev/com.lyl.dev/BuildConfig 这个文件看一看就知道了。
由于我这里没有这个需求,所以在build.gradle中没有使用

4. 不同环境,不同图标:

要实现这个需求就需要修改AndroidManifest.xml里的渠道变量:

  1. 在 AndroidManifest.xml 里添加渠道变量

...

...

这样 android:icon=”${app_icon}”会报红,不要紧,后面会有处理的方法。

  1. 在build.gradle设置:
productFlavors {
    app1 {
        manifestPlaceholders = [ENVIRONMENT: "app1",
                                app_icon   : "@drawable/icon1"]
    }

    app2 {
        manifestPlaceholders = [ENVIRONMENT: "app2",
                                app_icon   : "@drawable/icon2"]
    }
    app3 {
        manifestPlaceholders = [ENVIRONMENT: "app3",
                                app_icon   : "@drawable/icon3"]
    }
    app4 {
        manifestPlaceholders = [ENVIRONMENT: "app4",
                                app_icon   : "@drawable/icon4"]
    }
    app5 {
        manifestPlaceholders = [ENVIRONMENT: "app5",
                                app_icon   : "@drawable/icon5"]
    }

在drawable中放入五张不同的图片,这样就可以实现不同的环境,加载不同的图标

5. 最后需要配置一个检查要求
lintOptions {
    checkReleaseBuilds false
    // Or, if you prefer, you can continue to check for errors in release builds,
    // but continue the build even when errors are found:
    abortOnError false
}

这里的作用就是即使项目中报错也不会停止打包

  • 然后就可以打包了


    [转载]一键打包出多个不同包名,不同应用名称和图标的APK_第1张图片
    image

    这样就在相应的目录生成了不同的包,如图:


    [转载]一键打包出多个不同包名,不同应用名称和图标的APK_第2张图片
    image.png

    那么到底成功了没有呢,下面就来看一下效果吧,将这五个app安装到手机上,效果图如下:
    image.png

这就说明已经包名也已经成功的更改了,只有包名不同才能安装到同一个手机上,下面就通过命令行的方式来验证一下。

首先cd进入到你的sdk目录下,进入build-tools下的任意一个版本,你会看到有一个aapt.exe,这就是我们需要的插件。
然后执行 aapt dump badging

你可能感兴趣的:([转载]一键打包出多个不同包名,不同应用名称和图标的APK)