Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 如何修改 APK 打包後的預設檔名

編輯:關於 Android 程式設計

用 Android Studio 打包時,輸出的檔案預設就叫 app-release.apk(已簽名)或 app-debug.apk(測試版)。當專案要交付給多個客戶或需要區分版本時,這個名字實在不夠用。

要改預設檔名,可以打開 build.gradle(module: app),在 android 區塊中加入以下片段:

 android.applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { //這裡修改apk文件名 def fileName = outputFile.name.replace("app", "你想要的名字") output.outputFile = new File(outputFile.parent, fileName) } } }

把「你想要的名字」替換成實際名稱即可。寫完直接點工具列的 Build - Build APK,或使用簽名打包,輸出的檔名就會變成你設定的樣子。

效果如下:

當然,如果只是臨時要改名,最偷工減料的方式就是直接在檔案總管裡右鍵重新命名:

不過正式專案還是建議用 gradle 配置處理,這樣每次打包都會自動套用命名規則,不必手動改。若想更進一步,也可以在檔名中帶入 versionName 或時間戳,方便後續追蹤是哪一次建置的產物。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved