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
- 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
- if (buildconfig.debug) {
- 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. 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
- Android {
- Buildtypes {
- Debug {
- Buildconfigfield "String", "URL", "\" http://www.baidu.com/\ ""
- Buildconfigfield "int", "money", " $"
- }
- Release {
- Minifyenabled false
- Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.txt '
- }
- }
- }
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