当前位置: 首页 > 工具软件 > Android++ > 使用案例 >

Android build.gradle 之 android-productFlavors 和 android-buildTypes

夏侯腾
2023-12-01
android {
    compileSdkVersion rootProject.ext.android.compileSdkVersion
    buildToolsVersion rootProject.ext.android.buildToolsVersion
    flavorDimensions "mode", "type"

    defaultConfig {
        applicationId "com.dgs.test.app1"
        minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
        versionCode rootProject.ext.android.versionCode
        versionName rootProject.ext.android.versionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
        multiDexEnabled true
        multiDexKeepProguard file('maindexlist.pro')

    }

    productFlavors {

        //创建不同风味的产品

        app2 {
            //设置包名
            applicationId "com.dgs.test.app2"

            //设置用于AndroidManifest.xml中 Application标签中的参数
            addManifestPlaceholders([APPLICATION: "com.dgs.test.app2"])
            addManifestPlaceholders([appName: "@string/app_name_repair"])
            addManifestPlaceholders([appLogo: "@mipmap/app_logo_repair"])

            dimension "mode"
        }

        app1 {
            applicationId "com.dgs.test.app1"
            dimension "mode"
            addManifestPlaceholders([APPLICATION: "com.dgs.test.app1"])
            addManifestPlaceholders([appName: "@string/app_name_app1"])
            addManifestPlaceholders([appLogo: "@mipmap/app_logo_app1"])
        }

        app1test {
            applicationId "com.dgs.test.app1test"
            addManifestPlaceholders([APPLICATION: "com.dgs.test.app1test"])
            addManifestPlaceholders([appName: "@string/app_name_app1test"])
            addManifestPlaceholders([appLogo: "@mipmap/app_logo_app1test"])
            dimension "mode"
        }

        phone {
            applicationIdSuffix ".phone"
            minSdkVersion rootProject.ext.android.minSdkVersion
            targetSdkVersion rootProject.ext.android.targetSdkVersion
            versionCode rootProject.ext.android.versionCode
            versionName rootProject.ext.android.versionName
            dimension "type"
        }
        ipd {
            //applicationIdSuffix ".ipd"
            minSdkVersion rootProject.ext.android.minSdkVersion
            targetSdkVersion rootProject.ext.android.targetSdkVersion
            versionCode rootProject.ext.android.versionCode
            versionName rootProject.ext.android.versionName
            dimension "type"
        }


    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
        phone {
            java.srcDirs('src/phone/java')
            res.srcDir('src/phone/res')
            manifest.srcFile('src/phone/AndroidManifest.xml')
            jniLibs.srcDirs = ['libs']
        }
        ipd {
            java.srcDirs('src/ipd/java')
            res.srcDir('src/ipd/res')
            manifest.srcFile('src/ipd/AndroidManifest.xml')
            jniLibs.srcDirs = ['libs']
        }
    }

    signingConfigs {
        key {
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storeFile getKeyStoreFile()
            storePassword 'android'
        }
    }

    buildTypes {
        
        //针对同一个风味的产品设置不同的构建环境,可在Build Variants 用于选择       

        prd {
            //匹配后备
            matchingFallbacks = ['debug', 'dev', 'release', 'beta']
            //设置当前环境测配置参数,可在BuildConfig中获取
            buildConfigField("String", "envType", "\"prd\"")
            buildConfigField("Boolean", "openBulgy", isOpenBugly())
            buildConfigField("Boolean", "isLinux", isLinux())
            buildConfigField("String", "logFilter", "\"" + readLogFilter() + "\"")

            signingConfig signingConfigs.key

            debuggable false
        }
        debug {
            matchingFallbacks = ['dev', 'beta', 'release', 'prd']
            buildConfigField("String", "envType", "\"debug\"")
            buildConfigField("Boolean", "openBulgy", isOpenBugly())
            buildConfigField("Boolean", "isLinux", isLinux())
            buildConfigField("String", "logFilter", "\"" + readLogFilter() + "\"")
            signingConfig signingConfigs.key
            //JPush
            //JPush
            manifestPlaceholders = [
                    JPUSH_PKGNAME: "com.dgs.test.app1",
                    JPUSH_APPKEY : "b0ee821d3c041de9d3035b00", //JPush 上注册的包名对应的 Appkey.
                    JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
            ]
        }
        dev {
            matchingFallbacks = ['debug', 'beta', 'release', 'prd']
            buildConfigField("String", "envType", "\"dev\"")
            buildConfigField("Boolean", "openBulgy", isOpenBugly())
            buildConfigField("Boolean", "isLinux", isLinux())
            buildConfigField("String", "logFilter", "\"" + readLogFilter() + "\"")
            signingConfig signingConfigs.key
            debuggable true
        }
        beta {
            matchingFallbacks = ['debug', 'dev', 'release', 'prd']
            buildConfigField("String", "envType", "\"beta\"")
            buildConfigField("Boolean", "openBulgy", isOpenBugly())
            buildConfigField("Boolean", "isLinux", isLinux())
            buildConfigField("String", "logFilter", "\"" + readLogFilter() + "\"")
            signingConfig signingConfigs.key
            debuggable true
        }
        release {
//          minifyEnabled true//混淆配置
            matchingFallbacks = ['debug', 'dev', 'beta', 'prd']
            buildConfigField("String", "envType", "\"release\"")
            buildConfigField("Boolean", "openBulgy", isOpenBugly())
            buildConfigField("Boolean", "isLinux", isLinux())
            buildConfigField("String", "logFilter", "\"" + readLogFilter() + "\"")
            debuggable true
            signingConfig signingConfigs.key
            //JPush
            manifestPlaceholders = [
                    JPUSH_PKGNAME: "com.dgs.test.app1",
                    JPUSH_APPKEY : "a6b84f569b40051bd4f8a5da", //JPush 上注册的包名对应的 Appkey.
                    JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
            ]
        }
    }

    configurations {
        cleanedAnnotations
        compile.exclude group: 'org.jetbrains', module: 'annotations'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions {
        abortOnError false
        lintConfig file("../lint.xml")
    }

    //在apk文件后边生成版本号信息
    android.applicationVariants.all {
        variant ->
            variant.outputs.all {

                output ->
                    def fileName
                    if (buildType.name.equals('release')) {
                        fileName = "正式版"
                    } else if (buildType.name.equals('prd')) {
                        fileName = "生产版"
                    } else if (buildType.name.equals('debug')) {
                        fileName = "开发版"
                    } else if (buildType.name.equals('beta')) {
                        fileName = "测试版"
                    } else if (buildType.name.equals('dev')) {
                        fileName = "dev开发版"
                    }

                    def formattedDate = new Date().format('yyyyMMddHHmmss')
                    def outFile = output.outputFile
                    def appName
                    if (variant.name.startsWith("app1Phone")) {
                        appName = "手机应用1";
                    } else if (variant.name.startsWith("app1Ipd")) {
                        appName = "平板应用1";
                    } else if (variant.name.startsWith("app1testIpd")) {
                        appName = "平板应用1工程师版";
                    } else if (variant.name.startsWith("app2Phone")) {
                        appName = "手机应用2";
                    } else if (variant.name.startsWith("app2Ipd")) {
                        appName = "平板应用2";
                    }
                    if (outFile != null && outFile.name.endsWith('.apk')) {
                        output.outputFileName = appName + "_" + fileName + "_${defaultConfig.versionCode}_" + "v${defaultConfig.versionName}_${formattedDate}.apk"
                    }
            }
    }
}

android-productFlavors

android-buildType

属性描述
boolean debbuggable该构建类型是否生成一个可调式的apk
boolean minifyEnabled是否可以移出无用的java代码,默认为false
Boolean multiDexEnabled是否可以分包
File multiDexKeepFile指定放在main dex内的类,如果设置则它的格式为一个类一行:com/example/MyClass.class
File multiDexKeepProguard指定用在main dex 的类上的混淆文件,跟系统混淆文件联合使用
String name这种构建类型的名称
proguardFiles指定插件使用的混淆文件
SigningConfig signingConfig签名配置文件
boolean zipAlignEnabled是否使用zipAlign优化apk,Android sdk包里面的工具,能够对打包的应用程序进行优化,让整个系统运行的更快
String versionNameSuffixVersionName的后缀



 

 类似资料: