Written in Jane 2017-07-14 15:58
Hi ~ I am Gradle
The project address
Github.com/iamludaxu/g…
Gradle project structure
Gradle ├ ─ ─ build. Gradle ├ ─ ─ Settings. Gradle └ ─ ─ app ├ ─ ─ build. Gradle ├ ─ ─ build ├ ─ ─ libs └ ─ ─ the SRC └ ─ ─ the main ├ ─ ─ Java │ └ ─ ─ └── exercises ── custom exercises ─ etc.Copy the code
settings.gradle
include ':app', ':library', ':wear'
Copy the code
Set the structure of the whole project, including a three-module APP, Library and WEAR.
build.gradle
apply plugin: 'com.android.application' android {/** * compiled SDK version */ compileSdkVersion 23 /** * compiled build-tools version */ buildToolsVersion '25.0.1'... /** * Resource mapping */ sourceSets {...... } /** * Default configuration */ defaultConfig {applicationId "gift.witch.gradle"...... } /** * Signature information */ signingConfigs {...... } /** * version type */ buildTypes {debug {...... } release { ...... } /** * Different versions */ productFlavors {...... } /** * dependencies */ dependencies {...... }}Copy the code
Gradle has a build.gradle for each module, and different plug-ins are used for each module type
App module
apply plugin: 'com.android.application'
Copy the code
The library module
apply plugin: 'com.android.library'
Copy the code
Java module
apply plugin: 'java'
Copy the code
DefaultConfig Default configuration
Contains the core attributes of the app, which will override their counterparts in Androidmanifest.xml.
DefaultConfig {/** * applicationId */ applicationId "gift.witch.gradle" /** * applicationId suffix */ applicationIdSuffix ".dev" /** * Minimum SDK version */ minSdkVersion 14 /** * maximum SDK version */ maxSdkVersion 23 /** * targetSdkVersion */ targetSdkVersion 23 /** * application version */ VersionCode 1 /** * App version */ versionName "1.0"}Copy the code
SigningConfigs Signature information
Defines signature information, which can be used in defaultConfig, buildTypes, and productFlavors.
signingConfigs {
staging.initWith(signingConfigs.debug)
release {
storeFile file("keystore.jks")
storePassword"123456"
keyAlias "aliastest"
keyPassword "123456"
}
}
Copy the code
BuildTypes buildTypes
Define different versions to change the BuildConfig and Resources information in your app. Here’s an example
- Incrementing the value of the variable API_URL in BuildConfig with the buildConfigField method.
- Increment the appName value in string.xml with the resValue method.
- Whether the minifyEnabled method is obfuscated
- Add the suffix of the appId with the applicationIdSuffix method
- Add the suffix to the version name using the versionNameSuffix method
- Set the signature using the signingConfig method
- When adding strings, do not have the same definition in string.xml at the same time; otherwise, an error will be reported
buildTypes {
debug {
buildConfigField("String","API_URL","\"debug_API_URL\"")
resValue "string","appname","debug AppName"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
buildConfigField("String","API_URL","\"release_API_URL\"")
resValue "string","appname","release AppName"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
staging {
buildConfigField("String","API_URL","\"staging_API_URL\"")
resValue "string","appname","staging AppName"
applicationIdSuffix ".staging"
versionNameSuffix "-staging"
debuggable true
signingConfig signingConfigs.staging
}
}
Copy the code
ProductFlavors is a variety of flavors
- Add string and color via the resValue method
- It has the same methods as defaultConfig to modify the core properties of app
- When adding string and color attributes, color. XML and String. XML must not have the same definition or an error will be reported
/** * productFlavors {red {resValue "string", "flavor_string", "FLAVor_color ", "#ff0000"} "flavor_string", "flavor_string", "#ff0000"}Copy the code
Dependencies management
< span style = "box-sizing: border-box! Important; word-wrap: break-word! Important;" 'libs') testCompile' junit: junit: 4.12 '/ long-range dependence concise form * * * * / compile' com. Android. Support: appcompat - v7:25.3.1 '/ * * * */ compile group:"com.google.code.gson", name:"gson", Version: "2.3" testCompile "org. Robolectric: robolectric: 3.3.2 rainfall distribution on 10-12"/rely on * * * * / compile project stagingCompile (' : library ') 'junit: junit: 4.12}Copy the code
Configure project. Gradle
Buildscript {/** * Central repository */ repositories {jCenter ()} dependencies {classpath 'com. Android. Tools. Build: gradle: 2.3.2' / / NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } }Copy the code
Predefined warehouse
repositories {
mavenCentral()
jcenter()
mavenLocal()
}
Copy the code
Adding remote warehouses
maven { url "https://jitpack.io" }
Copy the code
Adding a private repository requires a user name and password
maven {
url "http://repo.acmecorp.com/maven2"
credentials {
username 'user'
password 'secretpassword'
}
}
Copy the code
Local dependence on a single JAR file
Dependencies {compile files('libs/ picasso-2.1.0.jar ')}Copy the code
Local dependence on multiple JAR files
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Copy the code
Remote dependencies come in two forms
Dependencies {/ long-range dependence concise form * * * * / compile 'com. Android. Support: appcompat - v7:25.3.1' remote rely on complete form / * * * * / compile Group: "com. Google. Code. Gson," name: "gson," version: "2.3"}Copy the code
Relying on library projects
Dependencies {/** */ compile project(':library')}Copy the code
Build variant
I. The build follows the following rules
- Build types
- Product flavors
- Build variants
- Signing configurations
Add Source sets
A new source set is created. By default, this folder is not automatically created for you, so you need to create it manually. Add a staging folder, for example.
3. Add dependency packages
- For each BuildType, you can add a dependency configuration named BuildTypeNameCompile to the Dependencies container
- For each ProductFlavor, you can add a dependency configuration called ProductFlavorNameCompile to the Dependencies container
Dependencies {stagingCompile 'junit:junit:4.12'Copy the code
4. Merge resource files and manifest
Before packaging the app, the Android plug-in merges the code in Main with the code built. Of course, dependent projects can also provide additional resources, which can also be combined. You may need additional Android permissions for the Debug variant. For example, you don’t want to declare this permission in main as this can cause some problems, so you can add an extra Mainfest file in debug’s folder to declare the extra permission. The priorities of resources and Mainfests are as follows:If a resource is defined in the main and flavor, that resource in the flavor has higher priority. Resources in the Flavor folder will be packaged into the APK. Resources declared in dependent projects always have the lowest priority.
Other knowledge points
Execute task by default
defaultTasks 'clean'
Copy the code
Using run-time./gradlew calls Task clean directly
2. Rename APK
/ * * * to rename output apk. * / android applicationVariants. All {variant - > the variant. The outputs. Each {output - > def file = output.outputFile output.outputFile = new File(file.parent,file.name.replace(".apk","-${variant.versionName}.apk")) } }Copy the code
Three, filtering,
/ filter * * * * / android variantFilter {variant - > if (variant. BuildType. Name. Equals (" release ")) {variant. SetIgnore (true); variant.getFlavors().each() { flavor -> if (flavor.name.equals('blue')) { } } } }Copy the code
The resources
Gradle for Android series
Gradle Build Variants (BuildVariant)
Android Plugin DSL Reference
My name is Lu Daxu.
A boring programmer who knows something about psychology. Tip, follow and like the article whether or not you get anything from it!