Android studio的gradle

1. gradle的基本概念

gradle构建
* Android Studio使用`Gradle`构建工具,Eclipse的ADT插件使用的是`Ant`构建工具
* 构建:生成app的过程,执行一些的命令(appt,aidl,javac,dex,apkbuilder,jarsinger,zipalign)

* 依赖管理:管理依赖的jar包
* 仓库:简单理解就是存的一些jar包云端
* maven
* ivy
* jcenter仓库地址
* 常见写法

mavenCentral()别名,表示依赖是从Central Maven 2 仓库中获取的。
jcenter()别名,表示依赖是从Bintary’s JCenter Maven 仓库中获取的(Android一般使用的是这个仓库)。
mavenLocal()别名,表示依赖是从本地的Maven仓库中获取的

* 仓库地址举例

repositories {
ivy {
url "http://ivy.petrikainulainen.net/repo"
}
}

repositories {
maven {
url "http://maven.petrikainulainen.net/repo"
}
}
repositories {
mavenCentral()
}
repositories {//默认仓库
jcenter()//它是当前世界上最大的Java和Android开源软件构件仓库
}

* Gradle构建脚本(build.gradle)指定了一个`项目`和它的`任务`,分Project和Module两个级别的脚本文件

* Gradle属性文件(gradle.properties)用来配置构建属性。
* Gradle设置文件(gradle.settings)对于只有一个项目的构建而言是可选的,如果我们的构建中包含多于一个项目,那么它就是必须的,因为它描述了`哪一个项目参与构建`。每一个多项目的构建都必须在项目结构的根目录中加入一个设置文件。

2. build.gradle

1. project:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    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()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 classpath 'com.android.tools.build:gradle:2.3.2'

插件的版本,没有则会自己去下载,也可以手动改成自己有的

 

注意:在manifest.xml中也可以去配置,当manifest.xml中的配置和gradle中的不一致,则以gradle为准,所以不建议在manifest.xml中去配置

2. module

apply plugin: 'com.android.application'    //application:这个module是一个应用程序,如果是其他的,也可以是库之类的

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig { //默认配置
        applicationId "com.expample.test"   //包名
        minSdkVersion 15             //SDK的最小版本
        targetSdkVersion 25           //编译SDK的版本
        versionCode 1             //版本号
        versionName "1.0"           //版本名
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {   //构建类型
        release {  //release版本,也就是发型版本
            minifyEnabled false  //是否压缩:这里不压缩
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  //进行混淆的规则的文件:用来防止反编译的11
        }
    }
}

//这个APP所依赖的东西 dependencies { compile fileTree(
dir: 'libs', include: ['*.jar']) //依赖于libs目录下所有的.jar包 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' }

 compileSdkVersion 25

就是sdk目录下的/platforms目录下的Android版本号,没有则会去下载

buildToolsVersion "25.0.2"

就是sdk目录下build-tools目录下的版本号,没有则会去下载

 

3. gradle.properties

gradle.properties:用来修改gradle的一些参数和属性的

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

4. settings.gradle

settings.gradle:这个工程包含了哪些模块

include ':app'

 

posted on 2017-11-08 19:11  maogefff  阅读(259)  评论(0编辑  收藏  举报

导航