如何在 build.gradle 中获取资源值?

时间:2023-02-12
本文介绍了如何在 build.gradle 中获取资源值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

resValue 方法(或其他任何名称)允许您在 buildTypesproductFlavors设置资源值>.有没有相应的方法获取resValue设置的资源值?

The resValue method (or whatever it's called) allows you to set a resource value in buildTypes or productFlavors. Is there a corresponding way to get a resource value that was set by resValue?

似乎 productFlavorsbuildTypes 之前进行评估,因此 buildTypes 中设置的 resValue 优先.我想在调试版本中将Debug"附加到应用程序名称,但我需要获取在产品风味中设置的值才能附加到它.

It appears that productFlavors is evaluated before buildTypes, so a resValue set in buildTypes takes precedence. I want to append "Debug" to the app name in debug builds, but I need to get the value that was set in the product flavor in order to append to it.

我尝试了 Marcin Koziński 的使用变量的建议,但所有产品风格在任何构建类型之前进行评估.因此,这不起作用:

I tried Marcin Koziński's suggestion to use a variable, but all product flavors are evaluated before any build type. Therefore, this does not work:

android {
    String appName = ""

    productFlavors {
        Foo {
            appName = "Foo"
        }

        Bar {
            appName = "Bar"
        }
    }

    buildTypes {
        release {
            resValue "string", "app_name", appName 
        }

        debug {
            resValue "string", "app_name", appName + " Debug"
        }
    }
}

buildTypes 中,appName 始终具有 last 产品风格的值.所以在这个例子中,所有构建都接收到名称 "Bar""Bar Debug".

In buildTypes, appName always has the value from the last product flavor. So in this example, all builds receive the name "Bar" or "Bar Debug".

基本上,我需要一个类似于 applicationIdSuffixresValueSuffix.显然不存在这样的动物.com.android.application 插件是否公开了我可以用来实现此目的的任何东西?

Basically, I need a resValueSuffix analogous to applicationIdSuffix. Apparently no such animal exists. Does the com.android.application plugin expose anything that I could use to achieve this?

推荐答案

如果您只是尝试设置 App Label(或其他清单值),您可以使用 清单占位符.

If you are only trying to set the App Label (or other manifest values) you can solve this with manifest placeholders.

android {

    productFlavors {
        Foo {
             applicationId "com.myexample.foo"
             manifestPlaceholders.appName = "Foo"
        }

        Bar {
             applicationId "com.myexample.bar"
             manifestPlaceholders.appName = "Bar"
        }
    }

    buildTypes {
        release {
            manifestPlaceholders.appNameSuffix =""
        }

        debug {
            manifestPlaceholders.appNameSuffix =".Debug"
            applicationIdSuffix ".debug"
        }
    }
}

然后在您的 Android Manifest 中,您只需将两个占位符用于您的应用名称(或其他值)

Then in your Android Manifest you simply use both placeholders for your app name (or other values)

 <application
        android:label="${appName}${appNameSuffix}"
        ...
 </application>

这允许您在单个设备上并排安装所有 4 个变体,并在应用程序抽屉/启动器中为它们赋予不同的名称.

This allow you to install all 4 variants side by side on a single device as well as give them different names in the app drawer / launcher.

编辑 2019 年 11 月 22 日

EDIT 11/22/2019

根据@javaxian 的反馈更新了占位符值的设置方式

Updated how placeholders values are set based on feedback from @javaxian

这篇关于如何在 build.gradle 中获取资源值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Android Wear 应用程序打包失败 下一篇:Android Studio - Gradle 同步项目失败

相关文章

最新文章