【问题标题】:Generate apk name as package name in Android Studio在 Android Studio 中生成 apk 名称作为包名称
【发布时间】:2015-12-03 15:39:14
【问题描述】:

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

【问题讨论】:

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


【解决方案1】:

试着把它放在你模块的 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)
    }
}

【讨论】:

  • 这只会为我吐出一个 APK 而不会为每个构建变体吐出一个 APK。
  • 当然不是,如果你想要另一个变体的apk,你必须先改变构建变体。或者使用命令来构建所有版本。此代码仅更改 apk 的名称,仅此而已。
  • 在终端中使用 './gradlew assemble' 应该吐出所有变体的所有 buildTypes。否则,您可以为这些特定的 buildType 执行 './gradlew assembleRelease' 或 './gradlew assembleDebug'
【解决方案2】:

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

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

    然后在您的应用模块的 gradle 中添加此代码

    apply from: "../customname.gradle"
    

    【讨论】:

      【解决方案4】:

      您可以在不使用其他任务的情况下执行此操作,设置 archivesBaseName

      例如:

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

      输出:

      MyName-1.0.12-release.apk
      

      在你的情况下:

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

      【讨论】:

      • 您也许可以访问 rootProject 名称并像这样使用它:project.ext.set("archivesBaseName", rootProject.getName() + "-" + defaultConfig.versionName);
      • 不幸的是,当您使用变体时无法扩展,因为 archivesBaseName 对所有变体都是全局的
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-02
        • 1970-01-01
        • 1970-01-01
        • 2013-12-14
        • 1970-01-01
        • 2021-07-02
        相关资源
        最近更新 更多