默認情況下,IDE會生成一個apk文件,如app-debug.apk
或app-release.apk
文件,但我需要生成該應用程序的Apk的特定名稱。Android:如何更改Android Studio中生成的apk文件的具體名稱?
例如: 我的應用程序名稱爲iPlanter所以我需要分別產生iPlanter-debug.apk
或iPlanter-release.apk
代替app-debug.apk
或app-release.apk
。
感謝,
默認情況下,IDE會生成一個apk文件,如app-debug.apk
或app-release.apk
文件,但我需要生成該應用程序的Apk的特定名稱。Android:如何更改Android Studio中生成的apk文件的具體名稱?
例如: 我的應用程序名稱爲iPlanter所以我需要分別產生iPlanter-debug.apk
或iPlanter-release.apk
代替app-debug.apk
或app-release.apk
。
感謝,
第1步: 轉到根主體工程的,下應用,右鍵單擊應用和重構應用到特定名稱(例如iPlanter)並按確定
步驟2: 轉到項目設置文件,該文件是setting.gradle
文件
setting.gradle
文件包含
include ':app'
現在需要通過特定的名稱來代替應用程序。
對於實例 應用程序通過iPlanter在取代包括「:應用程序」 它看起來像下面
include ':iPlanter'
然後同步項目,後運行應用程序。 最後,App生成一個apk,如iPlanter-debug.apk
或iPlanter-release.apk
文件。
試試這個代碼:
defaultConfig{
applicationVariants.all { variant ->
changeAPKName(variant, defaultConfig)
}
}
def changeAPKName(variant, defaultConfig) {
variant.outputs.each { output ->
if (output.zipAlign) {
def file = output.outputFile
output.packageApplication.outputFile = new File(file.parent, "Your APK NAME")
}
def file = output.packageApplication.outputFile
output.packageApplication.outputFile = new File(file.parent, "Your APK NAME")
}
}
您可以用當前的日期和版本
android {
def version = "2.4";
def milestone = "1";
def build = "0";
def name = getDate()+"APP NAME WHAT YOU WANT"+"v"+version
signingConfigs {
config {
….
}
}
compileSdkVersion Integer.parseInt(COMPILE_SDK)
buildToolsVersion BUILD_TOOLS_VERSION
defaultConfig {
applicationId "com.PACKAGENAME"
minSdkVersion Integer.parseInt(MIN_SDK_LIBRARY)
targetSdkVersion Integer.parseInt(TARGET_SDK)
versionCode 11
versionName "2.3"
multiDexEnabled true
}
buildTypes {
debug {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def apk = output.outputFile;
def newName;
newName = apk.name.replace("-" + variant.buildType.name, "")
.replace(project.name, name);
newName = newName.replace("-", "-" + version + "-" + milestone +
"-" + build + "-");
output.outputFile = new File(apk.parentFile, newName);
}
}
}
在我的電腦用這個應用的名字,就足夠了(通過重構)應用來重命名所需的名稱yourName。之後,將setting.grandle文件中的include ':app'
更改爲include ':yourName'
。但是,在我的情況下,我需要關閉/重新打開Android Studio,因爲同步錯誤。因此,獲得apk類似yourName-debug.apk和yourName-release.apk。
在gradle這個文件的Android {}部分只需添加
archivesBaseName = "NAME_YOU_WANT"
。
您將獲得「NAME_YOU_WANT-release.apk」作爲生成文件的名稱。
這會幫助你。此代碼將像iPlanter-release.apk或iPlanter-debug.apk
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
project.ext { appName = 'iPlanter' }
def newName = output.outputFile.name
newName = newName.replace("app-", "$project.ext.appName-")
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
瞭解Android Studio 3創建應用程序的名稱,這個工作對我來說:
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = new File("AppName-" + variant.versionName + ".apk");
}
}
錯誤:項目「在'root'項目'iPlanter'中找不到'app'。我得到這個錯誤@kgsharathkumar – PriyankaChauhan
@pcpriyanka,現在檢查,我添加完整的步驟.. :) – kgsharathkumar
我可以選擇重命名目錄或模塊? – PriyankaChauhan