android - 使用 Gradle 插件 v4.10.1 重命名 Apk

标签 android gradle gradle-plugin android-studio-3.3

我今天将我的 Android Studio 更新到了 3.3 版本,它附带了 Gradle 插件版本 4.10.1。

以前,我的 build.gradle 将使用此代码的 apk 重命名为以下结构:

app-{buildType[release|debug]}-{flavor[prod|stage]}-{versionName[1.2.4]-{versionCode[43]}.apk

app-release-prod-1.1.4-45.apk

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        outputFileName = output.outputFile.name.replace(".apk", "-${variant.versionName}-${variant.versionCode}.apk").replace("-unsigned", "")
    }
}

但是更新后出现了这个错误。

WARNING: API 'variantOutput.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variantOutput.getPackageApplication(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace. Affected Modules: app

问题出在 output.outputFile.name,因为您无法访问此插件版本的输出数据。

到目前为止,我已经尝试过这种方法但没有成功。

applicationVariants.all { variant ->
    variant.flavors*.name.all { flavor ->
        outputFileName = "${flavor}-${variant.buildType.name}-${variant.versionName}-${variant.versionCode}.apk".replace("-unsigned", "")
    }
}

有什么想法吗?

============================================= ========

更新

我重新考虑了这个问题,我尝试了以下代码 fragment ,但我在检索该变体的味道时遇到了问题。

android.applicationVariants.all { variant ->
    def flavor = variant.flavorName
    variant.outputs.all { output ->
        def builtType = variant.buildType.name
        def versionName = variant.versionName
        def versionCode = variant.versionCode
        outputFileName = "app-${flavor}-${builtType}-${versionName}-${versionCode}.apk"
    }
}

outputs: app--release-1.0.4-88.apk

谢谢

最佳答案

试试这个:

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def builtType = variant.buildType.name
        def versionName = variant.versionName
        def versionCode = variant.versionCode
        def flavor = variant.flavorName
        outputFileName = "app-${flavor}-${builtType}-${versionName}-${versionCode}.apk"
    }
}

这会输出以下 apk 名称:app-release-myFlavor-0.0.1-1.apk。

关于android - 使用 Gradle 插件 v4.10.1 重命名 Apk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54196783/

相关文章:

android - 上下移动 float 操作按钮以避免被 snackbar 挡住

android - 动画切换按钮错误膨胀类 android.widget.ToggleButton

java - 使用 Gradle 将 WAR 文件发布到 Maven Repo

Spring - 来自其他 Jar 的存储库为空。如何初始化它?

android - 不工作 : Android Studio 3. 2 Canary11 + 3.2.0-alpha11 for kapt with com.android.databinding

android - 仅在尝试检索 task.getResult() 时抛出 Firebase 电子邮件/密码身份验证异常

android - 转换为 Dalvik 格式失败,出现错误 2 adt 16.0.1

groovy - 如何从build.gradle执行git命令?

java - Gradle 覆盖 zip 存档的默认 'artifactId'

gradle - gradle将不再同步插件