1. gradle
Gradle is a new generation Build System that can be used for Android development. It is also the default build tool for Android Studio. In fact, the Gradle script is based on a JVM language, Groovy, and DSL. Because Groovy is the JVM language, most Java language libraries can be used. DSL is a plug-in specially developed for Android, such as some new methods and Closure other than standard Gradle. With a simple understanding, we know that Gradle's syntax is concise and simple, and most java packages can be used, so it is well deserved to become a new generation of Build System.
All users who want to use Android Studio know that after creating a project using Android Studio, two build. gralde files are generated by default, one in the project root directory and the other in the app directory. As follows:
$ Find-name build. gradle
./App/build. gradle
./Build. gradle
There is another file -- settings. gradle.
The script file in the root directory is configured globally for the module. All modules contained in its scope are configured through settings. gradle.
The app folder is a module. If a new module-lib is added to the current project, you need to include the new module in the settings. gralde file.
2. Have a deep understanding of its basic structure
Let's use engineering examples to gain a deeper understanding of its basic structure.
① The content of build. gradle in the root directory is as follows:
Buildscript {
Repositories {
Jcenter ()
}
Dependencies {
Classpath 'com. android. tools. build: gradle: 1.0.0-rc4'
}
}
Allprojects {
Repositories {
Jcenter ()
Maven {
Url 'HTTP: // mvnrepo.xxx.com/mvn/repository'
}
}
}
Explanation: classpath 'com. android. tools. build: gradle: 1.0.0-rc4 'is an Android-specific plug-in. The maven repository address is obtained through jCenter (), which is also the default maven repository. You can also add an additional maven repository address, for example
Maven {
Url 'HTTP: // mvnrepo.xxx.com/mvn/repository'
}
② The content of build. gradle in the app directory is shown as follows:
// Set the running environment of the script
Buildscript {
// Supports java dependency library management (maven/ivy) for project dependencies.
Repositories {
MavenCentral ()
}
// Define the dependency package. Supports maven/ivy, remote, local library, and single file
Dependencies {
Classpath 'com. android. tools. build: gradle: 100'
}
}
// Declare the type of the constructed project. android is used here.
Apply plugin: 'android'
// Set parameters for compiling android Projects
Android {
CompileSdkVersion 17
BuildToolsVersion "17"
DefaultConfig {
MinSdkVersion 8
TargetSdkVersion 17
}
// Default Android Configuration
SourceSets {
Main {
Manifest. srcFile 'androidmanifest. xml'
Java. srcDirs = ['src']
Resources. srcDirs = ['src']
Aidl. srcDirs = ['src']
Renderscript. srcDirs = ['src']
Res. srcDirs = ['res']
Assets. srcDirs = ['assets']
}
// Path of the test, which is assumed to be the tests folder. No such row can be left empty.
InstrumentTest. setRoot ('tests ')
}
// This is the code for resolving lint errors
LintOptions {
AbortOnError false
}
/**
* Signature settings
*/
SigningConfigs {
MyConfigs {
StoreFile file ("signature file address ")
KeyAlias "..."
KeyPassword "..."
StorePassword "..."
}
}
/**
* Obfuscation settings
*/
BuildTypes {
Release {
SigningConfig signingConfigs. myConfigs
RunProguard true
ProguardFiles getDefaultProguardFile('proguard-android.txt '), 'proguard-rules. Pro'
}
}
/**
* Channel packaging (Different Package Names)
*/
ProductFlavors {
Qqqq {
ApplicationId = 'package name'
}
Hhhhh {
ApplicationId = 'package name'
}
}
}
/**
* Import of the. so file
*/
Task copyNativeLibs (type: Copy ){
From fileTree (dir: 'libs', include: 'armeabi/*. so') into 'build/lib'
}
Tasks. withType (Compile ){
Options. encoding = "UTF-8"
}
Tasks. withType (Compile ){
CompileTask-> compileTask. dependsOn copyNativeLibs
}
Clean. dependsOn 'cleancopynativelibs'
Tasks. withType (com. android. build. gradle. tasks. PackageApplication) {pkgTask->
PkgTask. jniFolders = [new File (buildDir, 'lib')]
}
// Dependent Database
Dependencies {
Compile fileTree (dir: 'libs', include: ['*. jar'])
}
When the package dependency uses aar, the local dependency and remote dependency are as follows:
Local dependency:
As a build tool, gradle can easily use local jar packages. The following code blocks are used:
Dependencies {
// Single file dependency
Compile files ('libs/android-support-v4.jar ')
// All Dependencies under a folder
Compile fileTree (dir: 'libs', include: '*. jar ')
}
Android {
}
Remote dependency:
Gradle also supports maven and ivy. We use maven as an example. The following is a code block:
Repositories {
// Obtain the dependency from the central database
MavenCentral ()
// Or use the specified local maven Library
Maven {
Url "file: // F:/githubrepo/releases"
}
// Or use the specified remote maven Library
Maven {
Url "remote library address"
}
}
Dependencies {
// Application format: packageName: artifactId: version
Compile 'com. google. android: support-v4: r13 '}
Android {
}
If the project depends on the android library, it is not dependent on a jar. It is easy to use the gradle muw.project mechanism here. In the past, android library did not have a good package management method. In short, before gradle appeared, there was no official method for managing android library dependency packages, generally, we directly download other people's android library project source code for integration, and the third-party android-maven-plugin uses the apklib format. Now, the official release of an android library packaging format with the extension *. aar. As mentioned above, the android gradle plug-in does not support direct local use *. aar file. Package the android library and use gradle build under the library project. Then, you will see two files in the build/libs directory *. the aar file is used in a debug package and a release file. for personal needs, we use the release version here. aar file.
The reference script is similar to the dependency Library mentioned above:
Dependencies {
Compile (name: 'pulltorefresh ', ext: 'aar ')
}
Package dependency jar:
When multiple jar files are dependent:
Compile group: 'com. alibaba ', module: 'fastjson', version: 'latest. integration'
In fact, it can be abbreviated:
Compile 'com. alibaba: fastjson: latest. integration'
Latest. integration can be replaced with a specific version number. Here, the latest version on the server is obtained.
To remove repeated dependencies, you can:
Compile 'com. alibaba. fastjson. latest. integration '{
Exclude module: 'annotations ', group: 'com. google. android'
}
3. Run the script
A shell script-gradlew is automatically generated under the root directory of the Android project. Before executing the script, remember to add the x Attribute-chomod + x gradlew.
The gradle script contains many tasks. You can use the task name to specify the tasks to be executed.
./Gradlew build./gradlew assemble./gradlew assembleInnderDebug
4. Summary
I guess maven is widely used in most development projects, but I don't know why. When I use Gradle, I often encounter problems where I cannot obtain remote dependency packages, the simplest solution is to download the dependent package locally. Therefore, we recommend that you use local dependencies whenever possible. If you are not clear about this, you can search for related knowledge on the Internet or find some materials to learn about it, the best way to learn is to challenge yourself rather than relying on others.
I personally organized a high-end Android communication group. If you are interested, you can first share this article to your circle of friends, add me, and send it to your circle of friends. I will pull you in. If you learn more, you can receive the dry goods push immediately.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.