Gradle之通过配置productFlavors实现多版本差异化打包

1 基础知识

1.1 productFlavors

gradle中有一个功能叫做变体「productflavors」,这是来为APP设置不同的打包配置,以实现多渠道打包的一种方案。

android {
	productFlavors {
		baidu {
        }
        _360 {
	    }
        yingyongbao {
	    }
  }

1.2 buildTypes

gradle中打包编译类型「buildTypes」,主要区分:debug和release类型。

android {
	buildTypes {
	    debug {
	    }
	    release {
	    }
  }

1.3 结果

(1)这样的话最后打包的时候就可以生成9种包:

· baiduDebug
· baiduRelease
· _360Debug
· _360Release
· yingyongbaoDebug
· yingyongbaoRelease

(2)在Android Studio左下角可以找到并在每次build的时候选择不同种类的包:
这里写图片描述

2 实现分渠道配置

2.1 配置java变量

(1)在gradle中有一个功能叫「buildConfigField」,可以在系统的buildconfig中设置一个值。如下:

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "org.guan.demo";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "baidu";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0.0";
}

(3)使用

Logger.DEBUG = BuildConfig.DEBUG;
public static final String flavor = BuildConfig.FLAVOR;

2.2 配置manifest变量

(1)很多第三方sdk喜欢在manifest中配置appkey等,可以在gradle中使用:

 manifestPlaceholders = [UMENG_CHANNEL: "0",UMENG_APPKEY : "123456789"]

(2)然后在manifest中配置:

<meta-data
    android:name="UMENG_CHANNEL"
    android:value="${UMENG_CHANNEL}" />
<meta-data
    android:name="UMENG_APPKEY"
    android:value="${UMENG_APPKEY}" />

(3)配置包名
  在gradle中包名用applicationId代表。

defaultConfig {
    applicationId "com.guan.demo"
}
    
yingyongbao {
    applicationId "com.guan.demo.majia"		
}

(4)配置版本号

yingyongbao {
	versionCode 1
    versionName "1.0.0"		
}

3 实战

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'
    dexOptions { javaMaxHeapSize "4g" }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs'] //打包so库
        }
    }
    defaultConfig {
        minSdkVersion 15 
        applicationId "com.guan.demo"
        multiDexEnabled true
        manifestPlaceholders = [CHANNEL_NAME: "baidu", EXECUTE_FLAG: "true", EXECUTE_ADV_FLAG: "true"]
        dependencies {
            debugCompile files('libs/AppOffer_2.4.3_newsensenetworks.jar')
            debugCompile files('libs/Baidu_MobAds_SDK.jar')
        }
        ndk {
            // 设置支持的 SO 库构架
            abiFilters 'armeabi', 'armeabi-v7a', 'x86'// , 'arm64-v8a', 'x86_64', 'mips', 'mips64'
        }
    }
	
    buildTypes {
        debug {
            buildConfigField "boolean", "LOG_DEBUG", "true"
        }
        release {
            buildConfigField "boolean", "LOG_DEBUG", "false"
        }
    }
    
    productFlavors {
        baidu {
            buildConfigField "boolean", "yingyongbao", "false"
           
            manifestPlaceholders = [CHANNEL_NAME: "umeng", UMENG_KEY:"1",UMENG_SECRET:"123456", EXECUTE_FLAG: "true"]
			
            dependencies {
                baiduCompile files('libs/AppOffer_2.4.3_newsensenetworks.jar')
                baiduCompile files('libs/Baidu_MobAds_SDK.jar')
            }
        }

        _360 {
            buildConfigField "boolean", "yingyongbao", "false"
          
            manifestPlaceholders = [CHANNEL_NAME: "umeng", UMENG_KEY:"2",UMENG_SECRET:"123456", EXECUTE_FLAG: "false"]
			
            dependencies {
                provided files('libs/AppOffer_2.4.3_newsensenetworks.jar')
                provided files('libs/Baidu_MobAds_SDK.jar')
            }
        }

        yingyongbao {
            buildConfigField "boolean", "yingyongbao", "true"
            
            versionCode 100
            versionName "1.0.0"

            applicationId "com.guan.demo.majia"
			
            manifestPlaceholders = [CHANNEL_NAME: "umeng", UMENG_KEY:"2",UMENG_SECRET:"123456", EXECUTE_FLAG: "false"]
			
            dependencies {
                provided files('libs/AppOffer_2.4.3_newsensenetworks.jar')
                provided files('libs/Baidu_MobAds_SDK.jar')
            }
        }
    }
}

dependencies {
    compile project(':Downloader')
    compile files('libs/umeng_social_sdk.jar')
}

repositories {
    mavenCentral()
    jcenter()
    flatDir {
        dirs 'libs'
    }
}

4 创建统一文件夹

(1)切换到Project模式的目录
在这里插入图片描述
(2)在src目录下新建vest包:
在这里插入图片描述
(3)再新建以下包和文件:
在这里插入图片描述
(4)调试时点击 Build Variant 选择自己需要的版本即可正常引用:
在这里插入图片描述

5 更高级的实战:不同的APP名称/图标/UI等

这里写图片描述

6 其他配置

6.1 清单文件里引用了相同的资源文件解决方法

在马甲渠道包下,在application里的标签里加入tools:replace="android:name"替换了主包渠道的Application。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:name="com.reader.ReaderVestApplication"
        tools:replace="android:name">
        
        <activity
            android:name="com.reader.activity.MainReaderVestActivity"
            android:alwaysRetainTaskState="true"
         android:configChanges="orientation|keyboardHidden|screenSize"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan" />
    </application>
</manifest>

参考链接:将依赖库中清单文件,引用的依赖库的清单文件中的,冲突部分删除掉

7 学习链接

Android 通过配置 productFlavors 实现多版本差异化打包

彻底弄明白Android开发Gradle相关配置

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值