build.gradle文件详解

android studio采用Gradle来构建项目,gradle是一种基于Groovy的领域特定语言来声明项目设置,摒弃了传统基于XML的各种繁琐配置


1、最外层的build.gradle:一般情况,开发者不需要修改这个文件中的内容

构建项目需要的插件声明:com.andriod.tools.build:gradle:2.3.0


2、APP里面的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.mycompany.myfirstapp"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.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'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'
}

第一行:应用了一个插件,一般有2种,com.android.application(应用程序模块,可以直接应用),com.android.library(库模块,只能左右代码库依附于别的应用程序模块)

android的闭包:配置项目构建中的各种属性

compileSdkVersion:指定项目的编译版本

buildToolsVersion:指定项目构建工具的版本

defaultConfig

applicationId:指定项目包名,后期可以在此处修改

targetSdkVersion:指定的值表示在该目标版本上已经做过了充分的测试,系统将会为你的应用程序启用一些最新的功能和特性

versionCode:指定项目的版本号

versionName:指定项目的版本名

dependencies:可以指定当前项目所有的依赖关系

android studio项目一共有3种依赖关系:本地依赖、库依赖、远程依赖

本地依赖:对本地的Jar包或目录添加依赖关系

库依赖:对项目中的库模块添加依赖关系

远程依赖:对jcenter(代码托管库,很多andriod的开源项目都会将代码托管到jcenter上)库上的开源项目添加依赖关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值