安卓中的两个build.gradle文件介绍

版权声明:转载请注明出处 https://blog.csdn.net/FGY_u/article/details/84489559

当我们在build.script下面发现了两个bulid.gradle文件,需要用的时候不知道用哪个,也不知道他的含义,这里我们就来了解一下。这是在安卓模式下的截图。如果在project模式下,就是一个在根目录下,一个在app下(截图可以再后面找到)。
在这里插入图片描述

Google 推荐使用的 Android studio 是采用 Gradle 来构建项目。Gradle 是一个非常先进的项目构建工具。

1.根目录下的build.gradle
在这里插入图片描述

根目录下的build.gradle通常不需要修改这个文件中的内容,除非需要添加一些全局的项目构建配置。

buildscript {
     
    repositories {
        google()    //声明代码托管仓库Google
        jcenter()   //声明代码托管仓库,用于引用jcenter上的开源项目
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        //声明了一个Gradle插件用来作为Android开发。3.1.0为gradle版本号
 
    }
}
allprojects {
    repositories {
        google()    //声明代码托管仓库
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

2.APP目录下的build.gradle
APP目录下的build.gradle文件是app模块的gradle构建脚本,一般用来管理app包名、版本的以及添加和修改依赖库。
在这里插入图片描述

apply plugin: 'com.android.application' //应用 应用程序模块
 
android {
    compileSdkVersion 26    //指定使用项目的编译版本
    defaultConfig {
        applicationId "com.example.helloworld"  //包名
        minSdkVersion 15        //指定项目最低兼容的Android版本
        targetSdkVersion 26     //表示Android26版本上已经进行过充分的测试
        versionCode 1           //项目版本号
        versionName "1.0"       //版本名
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false //是否进行代码混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //指定混淆规则文件
        }
    }
}
 
dependencies {//*指定当前项目的所有依赖关系
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //本地依赖声明:将本地的jar包、目录添加依赖关系,添加到项目的构建路径当中
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    //远程依赖声明
    testImplementation 'junit:junit:4.12'   //用以声明测试用例库
}

猜你喜欢

转载自blog.csdn.net/FGY_u/article/details/84489559