Android build.gradle版本名打包配置

1、生成密钥文件到app工程目录下

image.png
image.png
image.png

2、在gradle.properties文件下配置密钥文件信息

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
RELEASE_KEY_PASSWORD=key-password
RELEASE_KEY_ALIAS=key-alias
RELEASE_STORE_PASSWORD=store-password
#jks文件名
RELEASE_STORE_FILE=sugr-ivy.jks

3、build.gradle文件下配置

apply plugin: 'com.android.application'

android {

    compileSdkVersion 26
    project.archivesBaseName = "AppName"
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 23
        targetSdkVersion 26
        versionCode 1
        versionName "1.4.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        release {
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
        }
        debug {
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
        }
    }

    buildTypes {
        release {
            buildConfigField "boolean", "LOG_DEBUG", "false"  // 不显示Log
            minifyEnabled false                      //混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release    //签名
        }
        debug {
            signingConfig signingConfigs.debug    //签名
        }
    }

    applicationVariants.all {
        variant ->
            variant.outputs.all {
                Calendar calendar = Calendar.getInstance(Locale.CHINA);
                def buildDate = String.format(Locale.CHINA, "%04d%02d%02d", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH))
                def versionName = defaultConfig.versionName
                def versionCode = defaultConfig.versionCode
                //android studio3.0之前的写法
                //    output->output.outputFile=new File(output.outputFile.parent,output.outputFile.name.replace(".apk","-"+defaultConfig.versionName+".apk"))
                //android studio3.0的写法
                //项目名-版本名-版本号-release/debug.apk
                if (variant.buildType.name.equals('release')) {
                    outputFileName = "${project.archivesBaseName}-v${versionName}-c${versionCode}-${buildDate}-release.apk"
                } else {
                    outputFileName = "${project.archivesBaseName}-v${versionName}-c${versionCode}-${buildDate}-debug.apk"
                }
            }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    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 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:recyclerview-v7:26.1.0'

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 说明 本文主要介绍和Gradle关系密切、相对不容易理解的配置,偏重概念介绍。部分内容是Android特有的(例如...
    jzj1993阅读 15,855评论 1 62
  • 这一章主要针对项目中可以用到的一些实用功能来介绍Android Gradle,比如如何隐藏我们的证书文件,降低风险...
    acc8226阅读 12,275评论 3 25
  • https://www.jianshu.com/p/7c288a17cda8 总的来说,Android的系统体系结...
    燕京博士阅读 4,948评论 0 6
  • 多云星期六 今天二宝的成绩出来了,不是很理想,我虽然不高兴但也没训她,让她自己反醒了下。自己知道也是粗心。宝贝加油...
    倾月萱萱_0bd6阅读 1,612评论 0 0
  • 在这种舒适,甚至是无所事事的的状态下,心必然会空虚,寂寞。 人这个高级动物好奇怪,有时对自己的错可以视而不见,有时...
    三毛和她阅读 1,891评论 4 4