Android: How to change specific name of the genera

2019-02-16 13:08发布

问题:

By default IDE genarate a apk like app-debug.apk or app-release.apk file but I need to generate specific name of the Apk of the App.

For Example: My application name is iPlanter so i need to generate iPlanter-debug.apk or iPlanter-release.apk instead of app-debug.apk or app-release.apk respectively.

Thanks,

回答1:

Step 1: Go to root of the main project, under app , right click on app and refactor the app into specific name (example iPlanter) and press ok

Step 2: Go to Project Setting file which is setting.gradle file

setting.gradle file contains

include ':app'

Now need to replace app by specific name.

For Example app replace by iPlanter in include ':app' it looks like below

include ':iPlanter'

then Sync project, after that run your application. Finally, App generate an apk like iPlanter-debug.apk or iPlanter-release.apk file.



回答2:

Just add

   archivesBaseName = "NAME_YOU_WANT"

in the android{} part of your gradle file.

You'll get "NAME_YOU_WANT-release.apk" as name of the generated file.



回答3:

You can use this for app name with current date and version

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);
           }
       }
   }


回答4:

Try this code:

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")
    }
}


回答5:

For Android Studio 3, this works for me:

applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File("AppName-" + variant.versionName + ".apk");
        }
}


回答6:

On my PC, it suffices to rename (through refactor) app to the desired name yourName. After that, include ':app' in setting.grandle file is changed to include ':yourName'. However, in my case I need to close/reopen Android Studio because of sync error. As a result, obtain apk something like yourName-debug.apk and yourName-release.apk.



回答7:

This will help you. This code will create app name like iPlanter-release.apk or 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)
     }
  }

}



回答8:

For Android Studio 3.1 this works for me:

android {
     ...............
     ...............

     applicationVariants.all { variant ->
            changeAPKName(variant, defaultConfig)
        }

  compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
       }

      .................................
      .................................
     }

and

def changeAPKName(variant, defaultConfig) {
    variant.outputs.all { output ->
        outputFileName = new File("xxxxx" + variant.versionName +".apk")
    }
}