Current location - Quotes Website - Signature design - How does android studio configure gradle?
How does android studio configure gradle?
① The contents of build.gradle in the root directory are as follows:

Build script {

Repository {

jcenter()

}

Dependency {

Classpath' com.android.tools.build: gradle:1.0.0-RC4'

}

}

All projects {

Repository {

jcenter()

maven {

url '/mvn/repository '

}

}

}

Explanation: classpath' com.' Android.tools.build: gradle:1.0.0-RC4' is a plug-in unique to Android, and the address of maven warehouse is obtained through method jCenter (), which is also the default maven warehouse. Of course, you can also add additional maven warehouse addresses, such as those in the above file.

maven {

url '/mvn/repository '

}

The contents of build.gradle in the app directory are as follows:

//Set the running environment of the script

Build script {

//java dependency library management (maven/ivy) supporting project dependency.

Repository {

mavenCentral()

}

//Dependent package definition. Support maven/ivy, remote and local libraries, and also support individual files.

Dependency {

Classpath "com.android.tools.build: gradle: 0.4"

}

}

//Declare the type of project to be generated. Of course it's Android.

Application plug-in: "android"

//Set the parameters for compiling android projects.

android {

Compiled version 17

buildToolsVersion " 17 "

defaultConfig {

minSdkVersion 8

targetSdkVersion 17

}

//Android default configuration

Source set {

Main {

manifest . src file ' Android manifest . XML '

java.srcDirs = ['src']

resources.srcDirs = ['src']

aidl.srcDirs = ['src']

renderscript.srcDirs = ['src']

res.srcDirs = ['res']

assets.srcDirs = ['assets']

}

//The path where the test is located, assuming that this is the tests folder, there is no need not to write this line.

instrumentTest.setRoot('tests ')

}

//This is the code to solve the error reported by lint.

lintOptions {

abortOnError false

}

/**

* signature settings

*/

Signature configuration {

My configuration {

Storefile ("signature file address")

Koyalias "…"

Keyword "..."

Store password "..."

}

}

/**

* chaotic settings

*/

Build type {

Publish {

Signature configuration signature configuration. myConfigs

runProguard true

proguardFiles getDefaultProguardFile(' proguard-Android . txt '),' proguard-rules.pro '

}

}

/**

* Channel packaging (different packaging names)

*/

Product flavor {

qqqq {

Application Id =' package name'

}

hhhhh {

Application Id=' package name'

}

}

}

/**

* imports. So filing

*/

Task copyNativeLibs (type: Copy) {

From fileTree (directory:' libs', including:' armeabi/*. So') into "build/lib"

}

Tasks.withType (compile) {

options.encoding = "UTF-8 "

}

Tasks.withType (compile) {

Compiler task->; compile task . dependsoncopynativelibs

}

clean . depends on ' cleanCopyNativeLibs '

tasks . with type(com . Android . build . gradle . tasks . package application){ pkgTask-& gt;

Pkg task. JNI folders =[ new file (buildDir,' lib')]

}

//dependency library

Dependency {

Compile the file tree (directory:' libs', including: ['*. jar'])

}

When using aar, package dependencies are divided into local dependencies and remote dependencies, as shown below:

Local dependency:

As a build tool, Gradle can easily use local jar packages. The following code blocks are used:

Dependency {

//Single file dependency

Compile file ('libs/android-support-v4.jar')

//All dependencies under the folder

Compile the file tree (directory:' libs', including:' *. Jar')

}

android {

}

Remote dependency:

Gradle supports both maven and ivy. Let's take maven as an example. Here is the code block:

Repository {

//Get dependencies from the central repository

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"

}

}

Dependency {

//Application format: package name: artifactId: version

Compile' com.google.android: support-v4: r13'}

android {

}

If the project depends on the android library, it is not as simple as relying on a jar. Here, we need to use the gradient multi-project mechanism. In the past, Android library didn't have a good package management method. Simply put, before gradle appeared, there was no official way to manage the dependency packages of android libraries. Generally, we directly download the source code of other people's android library projects for integration, and the third party android-maven-plugin uses apklib format. Now, the official has finally launched an android.

The packaging format of the library has an extension of *.aar. As mentioned above, at present, the android gradle plug-in does not support local direct use *. Aar file. Just package the android library and use gradle build directly under the library project. Then, you will see two *. Aar files in the build/libs directory, one for debugging packages and the other for publishing. We use the released version of. Aar, the file is here.

The reference script is similar to the dependency library mentioned earlier:

Dependency {

Compile (name:' pulltorefresh', extension:' aar')

}

Package dependency jar:

When multiple jars are dependent:

Compilation group: "com.alibaba", module: "fastjson", version: "latest.integration"

We can actually abbreviate it to:

Compile' com.alibaba: fastjson: latest.integration'

And latest.integration can be replaced with a specific version number, here is to get the latest version on the server.

If you want to delete duplicate dependencies, you can do this:

Compile' com.alibaba.fastjson.latest.integration' {

Exclusion module: "annotation", group: "com.google.android"

}

3. Command execution script

A shell script -gradlew will be automatically generated in the root directory of the Android project. Remember to add the x attribute -chomod +x gradlew before execution.

Gradle script contains many tasks, and you can specify the task to be executed by the task name.

. /gradlew build。 /gradlew assembly. /grad Lew assembliennderdebug

4. Concluding statement

I estimate that maven is widely used in most development projects now, but I don't know why, when using Gradle, I often encounter some problems that I can't get remote dependency packages. The simplest solution is to download the dependency package locally. So I suggest you try to use local dependence. If you don't know much about the introduction, you can search some related knowledge on the Internet or find some information yourself. The best way to learn is to challenge yourself instead of relying on others.