Android app 打包发布之build.gradle 配置

配置描述:在build.gradle(:app)文件中配置

包含以下几个部分:

  1. plugins:引入的工具
  2. android:主要配置都在这个里面
  3. dependencies:依赖
  4. android.applicationVariants.all:打包输出路径和名称

看android配置:

签名配置:

signingConfigs {
        release {
            storeFile file("签名路径.jks")
            storePassword "密码"
            keyAlias "签名别名"
            keyPassword "密码"
        }
    }

debug和release配置

buildTypes {
        debug {
//          signingConfig signingConfigs.adDebug
            signingConfig signingConfigs.release
            minifyEnabled true
            zipAlignEnabled true
            proguardFile('proguard-android.txt')
            proguardFile('proguard-rules.pro')
            manifestPlaceholders = [AD_LOG:false]
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            zipAlignEnabled true
            proguardFile('proguard-android.txt')
            proguardFile('proguard-rules.pro')
            manifestPlaceholders = [AD_LOG:false]
        }
    }

输出渠道

productFlavors {
        渠道名称1 {
            versionCode 100100
            versionName "0.1.0"
            buildConfigField("String", "CHANNEL", "\"unknown\"")
        }
        渠道名称2 {
            versionCode 1
            versionName "1.1"
            buildConfigField("String", "CHANNEL", "\"hw_cn\"")
        }
    }

查看渠道

Android app 打包发布之build.gradle 配置_第1张图片

 打包选择渠道

Android app 打包发布之build.gradle 配置_第2张图片

输出打包名称,以下配置和android同级

static def releaseTime() {
    return new Date().format("yyyyMMdd.kkmm", TimeZone.getTimeZone("GMT+8"))
}

android.applicationVariants.all { variant ->
    println('============')
    println(variant.name)

    variant.outputs.each { output ->
        def file = output.outputFile
        output.outputFileName = file.name.replace(".apk", "-描述信息-${variant.versionName}-${variant.versionCode}-${releaseTime()}.apk")
        println(output.outputFileName)

    }
}

完整的build.gradle文件

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
}


android {
    compileSdk 33

    defaultConfig {
        applicationId "your id"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"
        flavorDimensions "version"

        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }

        packagingOptions {
            doNotStrip "*/armeabi-v7a/*.so"
            doNotStrip "*/x86/*.so"
            doNotStrip "*/arm64-v8a/*.so"
            doNotStrip "*/x86_64/*.so"
            doNotStrip "armeabi.so"
        }

        viewBinding {
            enabled = true
        }

    }

    signingConfigs {
        release {
            storeFile file("xxxx.jks")
            storePassword "密码"
            keyAlias "别名"
            keyPassword "密码"
        }
    }
    buildTypes {
        debug {
//          signingConfig signingConfigs.adDebug
            signingConfig signingConfigs.release
            minifyEnabled true
            zipAlignEnabled true
            proguardFile('proguard-android.txt')
            proguardFile('proguard-rules.pro')
            manifestPlaceholders = [AD_LOG:false]
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            zipAlignEnabled true
            proguardFile('proguard-android.txt')
            proguardFile('proguard-rules.pro')
            manifestPlaceholders = [AD_LOG:false]
        }
    }
    productFlavors {
        渠道1 {
            versionCode 100100
            versionName "0.1.0"
            buildConfigField("String", "CHANNEL", "\"unknown\"")
        }
        渠道2 {
            versionCode 1
            versionName "1.1"
            buildConfigField("String", "CHANNEL", "\"hw_cn\"")
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
//    implementation fileTree(dir: './libs/ad', include: ['*.jar', '*.aar'])
    implementation fileTree(dir: './libs/ks_content', include: ['*.jar', '*.aar'])
//    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'

}

static def releaseTime() {
    return new Date().format("yyyyMMdd.kkmm", TimeZone.getTimeZone("GMT+8"))
}

android.applicationVariants.all { variant ->
    println('============')
    println(variant.name)

    variant.outputs.each { output ->
        def file = output.outputFile
        output.outputFileName = file.name.replace(".apk", "-描述-${variant.versionName}-${variant.versionCode}-${releaseTime()}.apk")
        println(output.outputFileName)

    }
}

你可能感兴趣的:(android)