AndroidStudio中修改打包生成的apk名称

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28261207/article/details/83147347

前言 : 之前总结了 Android同一套代码打多个APP包并能够在同一个手机上安装运行的代码 (点击可跳页查看) , 打包以后生成的名字想要更完善 , 想在打包时自动生成带版本号的apk名称 .

  • 需求 :

    • 打包出的名字例如 :
      • appname_v1.0.0_debug.apk
      • appname_v1.0.0_release.apk
  • 实现 :

    • 在app文件夹下的build.gradle里加 :
// 加在 android { ...... } 里
android.applicationVariants.all {
    variant ->
        variant.outputs.all {
            //这里修改apk文件名
            outputFileName = "${productFlavors[0].name}_v${defaultConfig.versionName}_${buildType.name}.apk"
        }
}
  • 如果要加时间
    • 例如 : app1_v1.0.0_debug_181018.apk (打包时间为2018年10月18号)
android.applicationVariants.all {
    variant ->
        variant.outputs.all {
            //这里修改apk文件名
            outputFileName = "${productFlavors[0].name}_v${defaultConfig.versionName}_${buildType.name}_${releaseTime()}.apk"
        }
}
def releaseTime() {
    return new Date().format("yyMMdd", TimeZone.getTimeZone("UTC"))
}
  • 完整build.gradle代码 :
apply plugin: 'com.android.application'

android {

    compileSdkVersion 27

    defaultConfig {
        applicationId "com.example.app2"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0.0"
        //版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
        flavorDimensions "versionCode"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
        // app1
        app1 {
            // 设置applicationId(这里很重要,两个相同applicationId的apk不同同时安装在同一台Android手机中)
            applicationId "com.example.app2"
            // 自动生成@string/app_name为demo
            resValue "string", "app_name", "app111"
            // 定义app_icon字段,在AndroidManifest.xml文件中用到
            manifestPlaceholders = [app_icon: "@mipmap/ic_launcher"]
        }
        // app2
        app2 {
            // 解释同app1
            applicationId "com.example.app3"
            resValue "string", "app_name", "app222"
            manifestPlaceholders = [app_icon: "@mipmap/ic_launcher1"]
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    android.applicationVariants.all {
        variant ->
            variant.outputs.all {
                //这里修改apk文件名
                outputFileName = "${productFlavors[0].name}_v${defaultConfig.versionName}_${buildType.name}_${releaseTime()}.apk"
            }
    }
}

static def releaseTime() {
    return new Date().format("yyMMdd", TimeZone.getTimeZone("UTC"))
}


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

猜你喜欢

转载自blog.csdn.net/qq_28261207/article/details/83147347