簡體   English   中英

在Android Studio中使用自定義名稱格式構建發行版APK

[英]Build release apk with customize name format in Android Studio

我希望.apk使用以下名稱格式(帶有時間戳)構建。

我該如何設置?

格式: {app_name}{yyyymmddhis}.apk

現在,它已使用名稱{app_name}-{release}.apk進行了修復。

build.gradle文件中,您應該像這樣更改/添加buildTypes

buildTypes {
      release {
        signingConfig signingConfigs.release
        applicationVariants.all { variant ->
            def file = variant.outputFile
            def date = new Date();
            def formattedDate = date.format('yyyyMMddHHmmss')
            variant.outputFile = new File(
                                    file.parent, 
                                    file.name.replace("-release", "-" + formattedDate)
                                    )
        }
    }       
}


======使用Android Studio 1.0編輯======

如果您使用的是Android Studio 1.0,則會收到如下錯誤:

Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@67e7625f.

您應該將build.Types部分更改為此:

buildTypes {
        release {
            signingConfig signingConfigs.releaseConfig
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def date = new Date();
                    def formattedDate = date.format('yyyyMMddHHmmss')
                    output.outputFile = new File(output.outputFile.parent, 
                                            output.outputFile.name.replace("-release", "-" + formattedDate)
                                            )
                }
            }
        }
    }

作為附帶說明,我真的很想知道人們在哪里獲得gradle構建對象的對象結構。

為了構造它,我使用了一些試驗和錯誤,一些谷歌搜索,並且這樣做 (已過時)

android {
    ...

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def flavor = "default";
                if (variant.productFlavors.size() > 0)
                    flavor = variant.productFlavors.get(0);

                def initials = "DefaultFlavor";
                if (flavor.name == "flavor1")
                    initials = "F1";
                else if (flavor.name == "flavor2")
                    initials = "F2";

                def build = "Debug";
                if (variant.buildType.name == "release")
                    build = "Release"
                def finalName = variant.versionCode + "-" + initials + "-" + build + "-v" + flavor.versionName + "-MyAppName.apk";
                output.outputFile = new File(output.outputFile.parent, finalName)
            }
        }
    }
    ...
}

好的,作為旁注,如果您想確定您的libs項目的名稱,這里是一個快速腳本,請確保將其放在每個庫gradle構建文件中。

android {
    ...

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 16
        versionCode 1
        versionName "1.0.4"
        project.version = versionName
    }

    libraryVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.aar')) {
                def fileName = "${archivesBaseName}-v${version}.aar"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

    ...
}

您應該更改這樣的內容。 動態地做。

project.archivesBaseName = {app_name}{yyyymmddhis};

但是我已經讀到,這將被棄用。

按需創建屬性(又稱動態屬性)已被棄用,並計划在Gradle 2.0中刪除。 不推薦使用的動態屬性:“根項目'myapp'”上的“ archivesBaseName”,值:“ AnotherName”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM