【问题标题】:renaming apk in gradle在gradle中重命名apk
【发布时间】:2014-05-16 05:38:01
【问题描述】:

我希望将我的 apk 从 gradle 重命名。我在构建中有以下几行

applicationVariants.all { variant ->      
            def file = variant.outputFile
            def filename = file.name.replace("SomeXXX", "SomeYYY")
            variant.outputFile = new File(file.parent, filename)

                    }

这会成功重命名 apk,但不会重命名未对齐的 apk。请有人对此有所了解。

【问题讨论】:

  • 可能applicationVariants 没有关于未对齐变体的信息。但是据我所知,当您知道变体名称时,很容易找到未对齐的变体。

标签: android gradle apk


【解决方案1】:

自从您发布此内容后,gradle 插件已继续使用,但是要使用当前插件 (v1.0.0) 进行此操作,您可以使用以下内容:-

    variant.outputs.each { output ->
        def alignedOutputFile = output.outputFile
        def unalignedOutputFile = output.packageApplication.outputFile

        // Customise APK filenames (to include build version)
        if (variant.buildType.zipAlignEnabled) {
            // normal APK
            output.outputFile = new File(alignedOutputFile.parent, alignedOutputFile.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk"))
        }
        // 'unaligned' APK
        output.packageApplication.outputFile = new File(unalignedOutputFile.parent, unalignedOutputFile.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk"))
    }

【讨论】:

  • 感谢您的回答。但是上面的 sn-p 到哪里去了呢?当我在我的 build.gradle 文件中的 android {...} 中发布它时,我得到一个无法解析符号“变体” - 运行 gradle 2.2.1 和 Android Studio 1.0.2
  • @Zainodis 应该进入 buildTypes 部分。
  • @karl 非常感谢您的评论!同时我意识到Android Studio中的gradle编辑器还没有完成,这就是为什么variant变量无法解析的原因。
  • 这对我在命令行中使用 gradle 2.12 不起作用。我将 sn-p 放在 buildTypes 下,得到Could not find property 'outputs' on BuildType_Decorated{name=variant...
猜你喜欢
  • 2013-05-23
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 2017-03-08
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多