Android Studio动态自定义编译输出的apk文件名

Android Studio默认编译会生成一个叫app-debug.apk或者叫app-release.apk文件,我们可以定义成下面的结构

包名-版本名称.apk

修改build.gradle向android{......}添加代码

apply plugin: 'com.android.application'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.test"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 16
        versionName "1.8.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
        }
    }
//添加代码 start
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "${variant.applicationId}-${variant.name}-${variant.versionName}.apk"
        }
    }
//添加代码 end
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.1.0'
    
}



最终build.gradle添加代码

生成apk

com.test-1.8.0.apk

 

你可能感兴趣的:(安卓)