构建发行版时更改常量值 [英] Change constant values when building a release edition

查看:83
本文介绍了构建发行版时更改常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ADT for android在eclipse中进行开发。

在我的应用程序中,我有一些常量可以帮助我轻松调试应用程序。

作为示例我有:

公共静态最终布尔值DEBUG_TOAST_LOGS = true;

帮助我在屏幕上烘烤一些日志。

每次我打算构建发行版时,我都必须遍历
常量并将其值设置为适合发行版的值,这在某种程度上是很痛苦的。

现在,我想要的是一种使用两种配置来构建我的应用程序的方法:一种用于调试模式,另一种用于发布模式发布模式应将我的常数设置为适当的值。我怎么能那样做?你有什么建议?


任何帮助将不胜感激。

I'm developing in eclipse using ADT for android.
In my application I have some constants which help me to to debug my app easily.
As an example I have:
public static final boolean DEBUG_TOAST_LOGS = true;
which help me to toast some logs on the screen.
Each time I intend to build a release, I have to go through my constants and set their values to what is appropriate for the release edition, which is somehow painful.
Now what I want is a way to build my app, using two configurations: one for debug mode and the other for release mode. The release mode should set my constants to the appropriate values. How cant I do that? What's your suggestion? What's the best way to accomplish my need?

Any help would be appreciated.

推荐答案

我不确定您是否将 Gradle 用作构建系统。如果这样做,则可以设置特定于构建类型的资源,例如布尔值 debug 的值对于调试构建类型为 true ,对于发行版本类型为false。

I'm not sure if you are using Gradle as your build system. If you do, you can set build-type specific resources, e.g. a boolean debug value will be true for debug build type, and false for release build type.

build.gradle

android {

    defaultConfig {
        ...
        resValue "bool", "debug", "true"
    }

    buildTypes {
        release {
            ...
            resValue "bool", "debug", "false"
        }
    }

    ...
}

Application.java

public class Application extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        if (getResources().getBoolean(R.bool.debug)) {
            ... // debug logic here
        }
        ...
    }
}

这篇关于构建发行版时更改常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆