簡體   English   中英

在Android Studio中生成apk名稱作為包名稱

[英]Generate apk name as package name in Android Studio

Android studio將生成默認的apk名稱為app-(release | debug).apk。
如何生成apk文件名,與com.example-debug.apk等應用程序的包名相同。

您可以在不使用其他任務的情況下執行此操作,設置archivesBaseName

例如:

 defaultConfig {
      ....
      project.ext.set("archivesBaseName", "MyName-" + defaultConfig.versionName);

  }

輸出:

MyName-1.0.12-release.apk

在你的情況下:

project.ext.set("archivesBaseName", "com.example" );

嘗試將它放在模塊的build.gradle中

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def file = output.outputFile
        def appId = android.defaultConfig.applicationId
        def fileName = appId + "-" variant.buildType.name +".apk"
        output.outputFile = new File(file.parent, fileName)
    }
}

你可以看到這個鏈接 release|debug.apk選項重命名您的release|debug.apk在文件瀏覽器中使用您想要的名稱。

此代碼可能對您有用:

buildTypes {
release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def formattedDate = new Date().format('yyyyMMddHHmmss')
            def newName = output.outputFile.name
            newName = newName.replace("app-", "$rootProject.ext.appName-") //"MyAppName" -> I set my app variables in the root project
            newName = newName.replace("-release", "-release" + formattedDate)
            //noinspection GroovyAssignabilityCheck
            output.outputFile = new File(output.outputFile.parent, newName)
        }
    }
}
    debug {
    }
}

享受你的代碼:)

在項目的頂級目錄中創建名為customname.gradle的文件。將此代碼放入其中。

android.applicationVariants.all { variant ->;
def appName
//Check if an applicationName property is supplied; if not use the name of the parent project.
if (project.hasProperty("applicationName")) {
    appName = applicationName
} else {
    appName = parent.name
}

variant.outputs.each { output ->;
    def newApkName
    //If there's no ZipAlign task it means that our artifact will be unaligned and we need to mark it as such.
    if (output.zipAlign) {
        newApkName = "${appName}-${output.baseName}-${variant.versionName}.apk"
    } else {
        newApkName = "${appName}-${output.baseName}-${variant.versionName}-unaligned.apk"
    }
    output.outputFile = new File(output.outputFile.parent, newApkName)
}}

然后在您的app模塊的gradle中添加此代碼

apply from: "../customname.gradle"

這可能對你有所幫助。 此代碼將創建應用程序名稱,如applicationId-release.apkapplicationId-debug.apk ,其中applicationId可以是您的包名稱。

buildTypes {

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def newName = output.outputFile.name
            newName = newName.replace("app-", applicationId)
            output.outputFile = new File(output.outputFile.parent, newName)
        }
    }
}

暫無
暫無

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

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