Android Stdio 如何自定義生成APK的名稱


    Android Stdio自動默認生成的app的名稱都是app-release或者app-debug,生成完后還要手動更改apk的名稱,很是麻煩。

   自定義生成APK的名稱的方法:在\app\build.gradle這個文件里添加如下內容

apply plugin: 'com.android.application'

def getTime() {
    return new Date().format("yyyyMMdd", TimeZone.getDefault());
}

android {
    compileSdkVersion 22
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // 打包后應用名稱
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            def fileName
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                if (variant.buildType.name.equals('release')) {
                    fileName = "LoverHeart_Release${defaultConfig.versionName}.${getTime()}.apk"
                } else if (variant.buildType.name.equals('debug')) {
                    fileName = "LoverHeart_Debug${defaultConfig.versionName}.${getTime()}.apk"
                }
                output.outputFile = new File(outputFile.parent, fileName)
            }

        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
}

 

生成的apk包名為:LoverHeart_Release1.0.20170417.apk

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM