在Android Studio中生成apk名称作为包名称

时间:2015-09-08 05:00:52

标签: android android-studio android-gradle apk package-name

Android studio会生成默认的apk名称为app-(release | debug).apk 如何生成apk文件名与应用程序的包名称相同,如com.example-debug.apk

5 个答案:

答案 0 :(得分:15)

您无需使用其他任务即可设置archivesBaseName

例如:

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

  }

输出:

MyName-1.0.12-release.apk

在你的情况下:

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

答案 1 :(得分:4)

尝试将其放入模块的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)
    }
}

答案 2 :(得分:2)

你可以看到这个link。 或者使用Illogical选项在文件浏览器中使用您想要的名称重命名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 {
    }
}

享受您的代码:)

答案 3 :(得分:0)

在项目的顶级目录中创建一个名为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"

答案 4 :(得分:0)

这可能会对你有所帮助。此代码将创建应用名称,例如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)
        }
    }
}