Gradle for Android (ii) Global settings, custom Buildconfig

Source: Internet
Author: User

Global Settings

If there are many projects, you can set the global to manage the version number or the dependent library, under the root directory under Build.gradle:

ext {    compileSdkVersion = 23    buildToolsVersion = "23.0.2"    minSdkVersion = 14    targetSdkVersion = 23}

App/build.gradle

android {    compileSdkVersion rootProject.ext.compileSdkVersion    buildToolsVersion rootProject.ext.buildToolsVersion    defaultConfig {        applicationId "com.example.android"        minSdkVersion rootProject.ext.minSdkVersion        targetSdkVersion rootProject.ext.targetSdkVersion        versionCode 1        versionName "1.0"    }

You can build a config.gradle in the root directory, and then just build.gradle the top of the root directory with the following line of code, and then synchronize, meaning that all sub-projects or all modules can read from this configuration file.

apply from: "config.gradle"

Config.gradle

ext {    android = [            compileSdkVersion: 23,            buildToolsVersion: "23.0.2",            minSdkVersion    : 14,            targetSdkVersion : 22,    ]    dependencies = [            appcompatV7‘: ‘com.android.support:appcompat-v7:23.2.1‘,            design      : ‘com.android.support:design:23.2.1‘    ]}

App/build.gradle

android {    compileSdkVersion rootProject.ext.android.compileSdkVersion    buildToolsVersion rootProject.ext.buildToolsVersion    defaultConfig {        applicationId "com.example.android"        minSdkVersion rootProject.ext.android.minSdkVersion        targetSdkVersion rootProject.ext.android.targetSdkVersion        versionCode 1        versionName "1.0"    }...dependencies {    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])    testCompile ‘junit:junit:4.12‘    compile rootProject.ext.dependencies.appcompatV7    compile rootProject.ext.dependencies.design}
Custom Buildconfig

The actual development of the server may have a formal environment and test environment, Gradle can be configured through Buildconfigfield.

 defaultConfig {        buildConfigField ‘String‘,‘API_SERVER_URL‘,‘"http://url/"‘    }

Buildconfigfield altogether has 3 parameters, the first is the data type, and the Java type is equivalent, the second parameter is the constant name, here is Api_server_url; The third parameter is the value you want to configure.
There is a constant api_server_url under the path, how to get this constant value in code:

  Log.d("wxl", "API_SERVER_URL=" + BuildConfig.API_SERVER_URL);
Enable Proguard Obfuscation

General release release is the need to enable confusion, so that others after the anti-compilation is difficult to analyze your code, and we do not need to confuse the development of debugging, so debug does not enable confusion. The configuration to enable obfuscation for release is as follows:

android {    buildTypes {        release {            minifyEnabled true//是否启动混淆            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘        }   }}

Minifyenabled is true to enable obfuscation, proguardfile is a confusing use of the configuration file, this is the Proguard-rules.pro file under the module root directory

Gradle for Android (ii) Global settings, custom Buildconfig

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.