Android APP的Gradle详解(三)

一.productFlavors{}闭包初级讲解

 

1.说明

productFlavors{}闭包 位于 android{}闭包内。用来实现多渠道打包。

 

 

2.需求

项目,有两个签名选项 一个是Release签名 一个是Debug签名。现在需要打三个包 red包&blue包&yellow包。

并且,三个包都有特殊的配置。

red包:applicationId在项目默认基础上追加".red"字符串。使用release签名。

blue包:applicationId在项目默认基础上追加".blue"字符串。使用release签名。

yellow包:applicationId在项目默认基础上追加".yellow"字符串。使用debug签名。

 

 

3.代码

apply plugin: 'com.android.application'

android {

    compileSdkVersion 28
    /** defaultConfig闭包 默认配置 */
    defaultConfig {
        applicationId "com.wjn.okhttpmvpdemo"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    /** signingConfigs闭包 项目签名配置 */
    signingConfigs {
        release {
            storeFile file('C:\\Users\\wujianning\\AndroidStudioProjects\\demo\\OKHttpMVPDemo\\test.jks')
            storePassword '123456'
            keyAlias 'key0'
            keyPassword '123456'
            v1SigningEnabled true
            v2SigningEnabled true
        }

        debug {
            storeFile file('C:\\Users\\wujianning\\AndroidStudioProjects\\demo\\OKHttpMVPDemo\\test.jks')
            storePassword '123456'
            keyAlias 'key0'
            keyPassword '123456'
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }

    /** buildTypes闭包 项目打包配置 比如是否混淆 是否压缩 是否支持调式等等 这里为了区分下面多渠道打包Debug包和Release包 在versionName后添加了不同的后缀*/
    buildTypes {
        release {
            minifyEnabled false
            debuggable false
            multiDexEnabled false
            versionNameSuffix '-Release'
        }

        debug {
            minifyEnabled false
            debuggable true
            multiDexEnabled false
            versionNameSuffix '-Debug'
        }
    }

    /** productFlavors闭包 多渠道打包 */
    productFlavors {
        red {
            applicationIdSuffix ".red"
            signingConfig signingConfigs.release
        }
        blue {
            applicationIdSuffix ".blue"
            signingConfig signingConfigs.release
        }
        yellow {
            applicationIdSuffix ".yellow"
            signingConfig signingConfigs.debug
        }
    }

    /** compileOptions闭包 配置项目使用JDK1.8 */
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'io.reactivex:rxjava:1.3.2'
    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'com.squareup.okhttp3:okhttp:3.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
    implementation 'com.readystatesoftware.systembartint:systembartint:1.0.3'

    implementation 'com.squareup.retrofit2:retrofit:2.7.0'
    implementation 'com.github.bumptech.glide:glide:4.4.0'

}

 

 

4.编译

报错,错误日志

All flavors must now belong to a named flavor dimension.

A problem occurred configuring project ':app'.
> All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

 

 

5.原因

没有给productFlavors{}闭包添加维度。

 

6.解决

/** defaultConfig闭包 默认配置 */
defaultConfig {
    applicationId "com.wjn.okhttpmvpdemo"
    minSdkVersion 19
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
    
    //给productFlavors{}闭包添加维度 版本号
    flavorDimensions "versionCode"
}

 

 

7.AndroidStudio任务栏截图

 

 

8.打包截图

 

 

9.各个APK对比

<1> blueDebug包

 

<2> blueRelease包

 

<3> redDebug包

 

<4> redRelease包

 

<5> yellowDebug包

 

<6> yellowRelease包

 

 

 

 

 

 

 

二.productFlavors{}闭包中级讲解

 

1.说明

上述讲解的是使用productFlavors{}闭包完成多渠道打包,打的六个包。分别是blueDebug包,blueRelease包,redDebug包,redRelease包,yellowDebug包,yellowRelease包。但是这种多渠道只是单维度的,如何实现多维度呢?比如 blueDebug包 又可以分出免费blueDebug包和付费blueDebug包。

 

 

2.需求

多渠道打包的前提下,red包和blue包。付费免费包。那么就总有这么几个包  red免费包(redDebug免费包+redRelease免费包)  red付费包(redDebug付费包+redRelease付费包)  blue免费包(blueDebug免费包+blueRelease免费包)  blue付费包(blueDebug付费包+blueRelease付费包)

 

 

3.代码

apply plugin: 'com.android.application'

android {

    compileSdkVersion 28
    /** defaultConfig闭包 默认配置 */
    defaultConfig {
        applicationId "com.wjn.okhttpmvpdemo"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    /** signingConfigs闭包 项目签名配置 */
    signingConfigs {
        release {
            storeFile file('C:\\Users\\wujianning\\AndroidStudioProjects\\demo\\OKHttpMVPDemo\\test.jks')
            storePassword '123456'
            keyAlias 'key0'
            keyPassword '123456'
            v1SigningEnabled true
            v2SigningEnabled true
        }

        debug {
            storeFile file('C:\\Users\\wujianning\\AndroidStudioProjects\\demo\\OKHttpMVPDemo\\test.jks')
            storePassword '123456'
            keyAlias 'key0'
            keyPassword '123456'
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }

    /** buildTypes闭包 项目打包配置 比如是否混淆 是否压缩 是否支持调式等等 这里为了区分下面多渠道打包Debug包和Release包 在versionName后添加了不同的后缀*/
    buildTypes {
        release {
            minifyEnabled false
            debuggable false
            multiDexEnabled false
            versionNameSuffix '-Release'
        }

        debug {
            minifyEnabled false
            debuggable true
            multiDexEnabled false
            versionNameSuffix '-Debug'
        }
    }

    /** productFlavors闭包 多渠道打包 flavorDimensions属性确定维度 分支&免费付费 */
    flavorDimensions "branch", "free_pay"
    productFlavors {
        red {
            dimension "branch"
            applicationIdSuffix ".red"
            signingConfig signingConfigs.release
        }

        blue {
            dimension "branch"
            applicationIdSuffix ".blue"
            signingConfig signingConfigs.debug
        }

        free {
            dimension "free_pay"
            buildConfigField "boolean", "IS_FREE", "true"
        }

        pay {
            dimension "free_pay"
            buildConfigField "boolean", "IS_FREE", "false"
        }
    }

    /** compileOptions闭包 配置项目使用JDK1.8 */
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'io.reactivex:rxjava:1.3.2'
    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'com.squareup.okhttp3:okhttp:3.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
    implementation 'com.readystatesoftware.systembartint:systembartint:1.0.3'

    implementation 'com.squareup.retrofit2:retrofit:2.7.0'
    implementation 'com.github.bumptech.glide:glide:4.4.0'

}

 

 

4.说明

<1> flavorDimensions属性定义二维空间 类似于二维数组。下标0的数值代表分支 下标1的数值代表免费付费。

<2> productFlavors{}闭包实现多渠道打包。

<3> 这里免费包定义了一个布尔类型的变量IS_FREE数值true。付费包定义了一个布尔类型的变量IS_FREE数值false。

 

 

5.结果

<1> AndroidStudio的任务截图

 

<2> 打出的包截图

 

<3> 验证正确与否

app-blue-free-debug.apk包
包截图

 

BuildConfig文件

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.wjn.okhttpmvpdemo.blue";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "blueFree";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0-Debug";
  public static final String FLAVOR_branch = "blue";
  public static final String FLAVOR_free_pay = "free";
  // Fields from product flavor: free
  public static final boolean IS_FREE = true;
}

 

 

app-blue-pay-release.apk

包截图

 

BuildConfig文件

public final class BuildConfig {
  public static final boolean DEBUG = false;
  public static final String APPLICATION_ID = "com.wjn.okhttpmvpdemo.blue";
  public static final String BUILD_TYPE = "release";
  public static final String FLAVOR = "bluePay";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0-Release";
  public static final String FLAVOR_branch = "blue";
  public static final String FLAVOR_free_pay = "pay";
  // Fields from product flavor: pay
  public static final boolean IS_FREE = false;
}

 

全部BuildConfig文件截图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值