Android Studio开发Android(一)——build.gradle文件详解

Android Studio采用Gradle构建项目。项目中又两个build.gradle文件,一个在最外层的目录中,一个在app目录下。

最外层目录的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter() //代码托管仓库
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"  //构建Android项目,需要引用

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter() //代码托管仓库
    }
}

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

app目录下的build.gradle

apply plugin: 'com.android.application' //应用程序模块(直接运行),还有一个值可选com.android.library库模块(依赖别的应用程序模块)

android {  //android闭包,配置项目构建的各种属性。
    compileSdkVersion 29    //项目的编译版本
    buildToolsVersion "30.0.0"  //项目构建工具的版本

    defaultConfig {                 //对项目更多细节配置
        applicationId "com.et.helloworld"          //项目的包名,如果想要修改包名,直接在这修改
        minSdkVersion 23            //项目最低兼容的Android系统版本
        targetSdkVersion 29         //在目标版本上做过充分的测试,系统为应用程序启用一些最新的功能和特性。
        versionCode 1               //项目版本号
        versionName "1.0"           //项目的版本名。

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"  //testInstrumentationRunner表明要使用AndroidJUnitRunner进行单元测试
    }

    buildTypes {    //用于指定生成安装文件的相关配置,两个包,一个是debug,一个是release。debug闭包用于指定生成测试版安装文件的配置。release用于生成正式版安装文件的配置。debug可以忽略不写。
        release {
            minifyEnabled false    //用于指定是否对项目的代码进行混淆
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            //指定混淆时的规则文件。proguard-android-optimize.txt是Android SDK目录下的。proguard-rules.pro项目根目录下的。
        }
    }
}

dependencies { //指定当前项目所有的依赖关系。一共又三种依赖方式:本地依赖,库依赖,远程依赖。
    implementation fileTree(dir: "libs", include: ["*.jar"])  //本地依赖声明,表示将libs目录下所有.jar后缀的文件都添加到项目构建路径中。
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

猜你喜欢

转载自blog.51cto.com/470462/2507474