The magical magic of Android Buildconfig.debug

Source: Internet
Author: User

In Android development, we use ANDROID.UTIL.LOG to print the log, which facilitates our development and debugging. However, the code does not want to be executed after the release, and we do not want the debug log to be seen by other developers after the release of the software, but now my approach is to set a global variable that marks whether the software is in debug mode or Release mode. Look at the code:

PublicClassLog{PrivateStaticFinalBooleanDEBUG=True;PublicStaticvoidI(StringTag,StringMsg){If(DEBUG)Android.Util.Log.I(Tag,Msg);}PublicStaticvoidE(StringTag,StringMsg){If(DEBUG)Android.Util.Log.E(Tag,Msg);}PublicStaticvoidD(StringTag,StringMsg){If(DEBUG)Android.Util.Log.D(Tag,Msg);}PublicStaticvoidV(StringTag,StringMsg){If(DEBUG)Android.Util.Logv (tagmsg} public static void w  (string tagstring msg) {if  (DEBUG ) android. Util. Log. W (tagmsg}}             /span>                

This will only be done before the release of Debug=false, but each time before the release of the manual to change the variable, not very convenient, and do not rule out the developers forget to change the situation. So is there a better and more convenient way to do it?

After ADT (R17) was released, Google provided us with a new debugging mechanism, Buildconfig.debug.

The second article of the new build features of ADT 17.0.0 is described below:

Added a feature that allows the run some code only in debug mode. Builds now generate a class called buildconfig containing a debugconstant that's automatically set according to your Buil D type. You can check the (buildconfig.debug) constant in your code to run Debug-only functions.

That is, a new feature is added that allows developers to run only part of the code in debug mode. Builds generates a class called Buildconfig, which contains a constant named debug, whose constant value is automatically set according to the developer's build type. In this way, you can use Buildconfig.debug to implement code that runs only in debug mode.

If your ADT has been updated to version 17 and above, you can try creating a new Android project in Eclipse, and you will find a class called Buildconfig.java in the R.java sibling directory with the following content:

/** Automatically generated file. DO NOT MODIFY */package com.boohee.one;public final class BuildConfig { public final static boolean DEBUG = true;}

So it's OK to just change one line of code,

private static final boolean DEBUG = BuildConifg.DEBUG;

As mentioned above, debug is automatically set according to the build type. So where does the build type differentiate? It's easy to see the Project menu that opens eclipse, such as:

As can be seen, the build type is divided into build project and build automatically, i.e. manual and automatic.

It is important to note that if you run project directly through Eclipse, debug will not be set to false regardless of whether the build is manual or automatic. What is this for? This involves the issue of Android signatures, here is simply a mention, do not repeat: run Project,eclipse directly through eclipse will be generated in the bin directory after the project build an APK, the APK signature is debug mode (debug Modes) , and the release mode signature generated by the APK is slightly different. As a result, the cause of the problem surfaced.

At this point someone will say that using the release mode APK, which is exported directly from Android Tools–>export signed application package, the debug is false. That's not right. When you build the release version, you need to differentiate the type of build. If you select Auto Build, Debug will still be set to true. So when generating the release version, please follow this step to package, Buildconfig.debug will be modified to false:

    1. Project, build automatically, cancel build automatically

    2. Project, clean

    3. Project-Build

    4. Android Tools, Export android application

The magical magic of Android Buildconfig.debug

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.