android-studio - Android Studio 中有没有类似C#的#if DEBUG功能
黄舟
黄舟 2017-04-17 17:31:01
0
7
624

VisualStudio中如果当前模式为DEBUG,那么我在代码中可以在#if DEBUG中执行一些代码,比如Debug模式中访问的API为www.aaa.com。而正式发布release时API是www.bbb.com,这在vs中是可以。不知道Android Studio中有没有类似的功能。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all (7)
小葫芦

Android Studio will automatically add the BuildConfig class. This class has a field called DEBUG. Its value is automatically generated based on the current compilation mode. When the compilation mode is debug, the value is true, and when the compilation mode is release, the value is false. Therefore, you can add the if (BuildConfig.DEBUG) class to the function based on this feature to achieve similar functions. A common example is to customize the Log class so that the log is output during debug and not output during release:

public void d(String tag, String message) { if (BuildConfig.DEBUG) { Log.d(tag, message); } }
    Ty80

    No, Java does not support macro definition, and all debugging information will be included in the compilation result. You can only set variables in the program so that the debugger will not be executed in the production environment.
    There is version information in the Android Manifest. Debug and Release versions are produced by default. You can switch parameters through program judgment.

      迷茫

      Variable values can be dynamically set during AS debugging~

        黄舟

        This function is very useless. You can obviously write code to implement it yourself (it can be done with no more than 3 lines of code). It has to be implemented natively in Android. If you want, Android development will be more complicated. We need to remember more useless functions. It’s functional (the gain outweighs the loss)

          大家讲道理

          Can be achieved using BuildConfig

            左手右手慢动作

            Yes, use gradle to configure: variables, version information

              大家讲道理

              Look at the debugging skills of android studio, http://weishu.me/2015/12/21/a..., don’t be too awesome

                Latest Downloads
                More>
                Web Effects
                Website Source Code
                Website Materials
                Front End Template
                About us Disclaimer Sitemap
                php.cn:Public welfare online PHP training,Help PHP learners grow quickly!