Android Studio's Buildconfig class

Source: Internet
Author: User

71601451

Android Studio Development, a module output packaged as a jar file, we will find that there is a buildconfig class, but throughout the development process we have never written this class, what is the matter?

Originally, the Buildconfig class is a class that is automatically generated according to the Build.gradle configuration file, a bit similar to R.class, and cannot be modified after it is generated. It has the following characteristics:

1. The position under ApplicationID (Application package name), such as

2, skillfully use the Build_type attribute for version viewing, debug attribute log control, and so on.

[Java]View PlainCopy
    1. LOG.D ("Com.emp.unionpay", Buildconfig.build_type);


Production Package Buildconfig.build_type = "Release";

Test Package Buildconfig.build_type = "Debug";

Log Output control:

[Java]View PlainCopy
    1. if (buildconfig.debug) {
    2. LOG.D ("<span style=" font-family:arial, Helvetica, Sans-serif;" >com.emp.unionpay</span><span style= "font-family:arial, Helvetica, Sans-serif; ">", "I am a Debug");</span>
    3. }


3. Customizing the contents of the Buildconfig class

The following code shows us how to add some of the values you want to buildconfig. To modify the Build.gradle file:

[Java]View PlainCopy
  1. Android {
  2. Buildtypes {
  3. Debug {
  4. Buildconfigfield "String", "URL", "\" http://www.baidu.com/\ ""
  5. Buildconfigfield "int", "money", " $"
  6. }
  7. Release {
  8. Minifyenabled false
  9. Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.txt '
  10. }
  11. }
  12. }

We do this by entering the Buildconfigfield syntax in any type (including custom) in BuildType, which has three parameters Buildconfigfield (String type,string name,string Value), explained below:

String type The type of field to create, such as the above string and int
String Name The name of the field to create, such as base_url and date above
String value Create a value for this field, such as \ "Http://www.baidu.com/\" above


But one thing to note here is that when you create a type of string, you need to be careful when you define value with a double quote "" that cannot be missing from the string, so we add an escape character when adding the type, because the argument itself is a string.

Android Studio's Buildconfig class

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.