Android Gradle配置productFlavors、apk路径及文件名示例

productFlavors

productFlavors {
    defaultConfig {
        //versionNameSuffix 'default'
    }
    developTest {
        minSdkVersion 15
        applicationId 'cn.appblog.example'
        targetSdkVersion 28
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode versionCode
        versionName versionName
        //versionNameSuffix 'devtest'
    }
    productTest {
        minSdkVersion 15
        applicationId 'cn.appblog.example'
        targetSdkVersion 28
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode versionCode
        versionName versionName
        //versionNameSuffix 'protest'
    }
    home {
        minSdkVersion 15
        applicationId 'cn.appblog.example'
        targetSdkVersion 28
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode versionCode
        versionName versionName
        //versionNameSuffix 'home'
    }
    update {
        minSdkVersion 15
        applicationId 'cn.appblog.example'
        targetSdkVersion 28
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode versionCode
        versionName versionName
        //versionNameSuffix 'update'
    }
    googleplay {
        minSdkVersion 15
        applicationId 'cn.appblog.example'
        targetSdkVersion 28
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode versionCode
        versionName versionName
        //versionNameSuffix 'googleplay'
    }
}

apk路径及文件名

applicationVariants.all { variant ->
    variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + '/outputs/' + versionName)  //指定apk输出目录
    def buildName = 'AppBlog'
    if (variant.buildType.name.equals('release')) {
        variant.outputs.all { output ->
            def type = variant.buildType.name
            def flavor = variant.productFlavors[0].name
            def releaseApkName
            if (flavor.equals('defaultConfig')) {
                releaseApkName = buildName + '-v' + versionName + '-release.apk'
            } else {
                releaseApkName = buildName + '-v' + versionName + '-release' + '-' + flavor + '.apk'
            }
            //def releaseApkName = buildName + '_' + type + "_" + versionName + '_' + getTime() + '.apk'
            outputFileName = releaseApkName
        }
    } else if (variant.buildType.name.equals('debug')) {
        variant.outputs.all { output ->
            def flavor = variant.productFlavors[0].name
            println 'flavor: ' + flavor
            def debugApkName = buildName + '-v' + versionName + "-debug" + "-" + flavor + '.apk'
            outputFileName = debugApkName
        }
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/android-gradle-config-productflavors-apk-path-and-file-name-examples/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android Gradle配置productFlavors、apk路径及文件名示例
productFlavors productFlavors { defaultConfig { //versionNameSuffix 'default' } developTest { minSdkVersion 1……
<<上一篇
下一篇>>
文章目录
关闭
目 录