If you haven’t read the first one, you canGradle for Android (1) Basic configuration, dependency management
Global Settings
If you have a number of projects, you can set global to manage version numbers or dependent libraries.
Ext {compileSdkVersion = 23 buildToolsVersion = "23.0.2" minSdkVersion = 14 targetSdkVersion = 23}Copy the code
app/build.gradle
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.wuxiaolong.gradle4android"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}Copy the code
Gradle in the root directory, then simply add the following line to the top of build.gradle in the root directory, and synchronize. This means that all subprojects or modules can read from this configuration file.
apply from: "config.gradle"Copy the code
config.gradle
Ext {android = [compileSdkVersion: 23, buildToolsVersion: "23.0.2", minSdkVersion: 14, targetSdkVersion: Dependencies, 22] = [appcompatV7 ':' com. Android. Support: appcompat - v7:23.2.1 ', the design: 'com. Android. Support: design: 23.2.1']}Copy the code
app/build.gradle
android { compileSdkVersion rootProject.ext.android.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { applicationId "com.wuxiaolong.gradle4android" 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 }Copy the code
Custom BuildConfig
A server in real development may have a formal environment and a test environment, which Gradle can configure with buildConfigField.
defaultConfig {
buildConfigField 'String','API_SERVER_URL','"http://wuxiaolong.me/"'
}Copy the code
BuildConfigField takes three arguments. The first is the data type, which is equivalent to the Java type. The second argument is the constant name, in this case API_SERVER_URL; The third parameter is the value you want to configure.
There is a constant API_SERVER_URL in the path shown below. How to obtain this constant value in code:
Log.d("wxl", "API_SERVER_URL=" + BuildConfig.API_SERVER_URL);Copy the code
Enable ProGuard obfuscation
Generally, the release version needs to be obfuscated, so it will be difficult for others to analyze your code after decomcompiling. However, when we develop and debug ourselves, we do not need to obfuscate, so debug does not enable obfuscation. The configuration to enable obfuscation for release is as follows:
Android {buildTypes {release {minifyEnabled true// Whether to start obfuscation proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }Copy the code
If minifyEnabled is true, obfuscation is enabled. ProguardFile is the configuration file used for obfuscation. Here is the proGuard-rules.pro file in the root directory of the Module
Complete configuration
Github.com/WuXiaolong/…
To learn more, click “Read the original article” at the bottom to jump to my blog.
Wechat public account: AndroidProgrammer
Long press qr code recognition one key attention