我该如何调试Android应用程序时,使用不同的设置? [英] How can I use different settings when debugging an android app?

查看:162
本文介绍了我该如何调试Android应用程序时,使用不同的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用特定URL在生产中使用时,与我们的服务器进行通信的Andr​​oid应用程序。我希望能够使用,当我在调试不同的URL,这样我可以对我的机器上的服务器的本地实例中运行的应用程序。

我怎么能做到这一点,而无需手动编辑网址是什么?


解决方案

使用Android Studio中,您可以定义构建变种,并确定在这种情况下字符串:

生成Java常量

插件版本比0.7.x大于(当前方法)

 安卓{
    buildTypes {
        调试{
            buildConfigFieldINT,富,42
            buildConfigField字符串,FOO_STRING,\\富\\
        }        发布 {
            buildConfigFieldINT,富,52
            buildConfigField字符串,FOO_STRING,\\栏\\
        }
    }
}

插件版本低于0.7(旧)

 安卓{
    buildTypes {
        调试{
            buildConfig公共最后静态INT FOO = 42;
        }        发布 {
            buildConfig公共最后静态INT FOO = 52;
        }
    }
}

您可以用 BuildConfig.FOO

访问它们

生成的Andr​​oid资源(因为插件0.8.3)

 安卓{
    buildTypes {
        调试{
            resValue串,APP_NAME,我的应用程序名称调试
        }
        发布 {
            resValue串,APP_NAME,我的应用程序名称
        }
    }
}

您可以用 R.string.app_name

来源: http://stackoverflow.com/a/17201265/1096905 (所有学分他,我只是复制和放大器;粘贴)

I have an android app that uses a certain url to communicate with our server when used in production. I want to be able to use a different url when I am debugging, so that I can run the app against a local instance of the server on my machine.

How can I do this without manually editing the url?

解决方案

Using Android Studio, you can define build variants, and define strings in that case:

Generate Java Constants

Plugin version greater than 0.7.x (current method)

android {
    buildTypes {
        debug {
            buildConfigField "int", "FOO", "42"
            buildConfigField "String", "FOO_STRING", "\"foo\""
        }

        release {
            buildConfigField "int", "FOO", "52"
            buildConfigField "String", "FOO_STRING", "\"bar\""
        }
    }
}

Plugin version less than 0.7 (old)

android {
    buildTypes {
        debug {
            buildConfig "public final static int FOO = 42;"
        }

        release {
            buildConfig "public final static int FOO = 52;"
        }
    }
}

You can access them with BuildConfig.FOO

Generate Android resources (since plugin 0.8.3)

android {
    buildTypes {
        debug{
            resValue "string", "app_name", "My App Name Debug"
        }
        release {
            resValue "string", "app_name", "My App Name"
        }
    }
}

You can access them in the usual way with @string/app_name or R.string.app_name

source: http://stackoverflow.com/a/17201265/1096905 (All credits to him, i just copy&paste)

这篇关于我该如何调试Android应用程序时,使用不同的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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