当前位置 博文首页 > 振华OPPO的博客世界:Android Studio的build.gradle里面的各种版

    振华OPPO的博客世界:Android Studio的build.gradle里面的各种版

    作者:[db:作者] 时间:2021-08-25 21:38

    Android studio 是采用 Gradle 来构建项目。Gradle 是一个非常先进的项目构建工具。
    我们在导入Android项目后,只要项目同步成功,就会出现以下文件夹。
    在这里插入图片描述
    如图是build.gradle(Module:app)文件的代码,其中的几个属性分别介绍一下:

    1、apply plugin,声明是 Android 应用程序还是库模块。

    2、android 闭包,配置项目构建的各种属性:
    (1)compileSDKVersion 用于指定项目的变异 SDK 版本,
    (2)buildToolsVersion 用户指定项目构建工具的版本。
    (3)defaultConfig 闭包:默认配置、应用程序包名、最小sdk版本、目标sdk版本、版本号、版本名称。
    (4)buildTypes 闭包:指定生成安装文件的配置,是否对代码进行混淆;
    (5)signingConfigs 闭包:签名信息配置;
    (6)sourceSets 闭包:源文件路径配置;
    (7)lintOptions 闭包:lint 配置;

    3、dependencies 闭包,指定当前项目的所有依赖、本地依赖,库依赖以及远程依赖;

    4、repositories 闭包,仓库配置。
    在这里插入图片描述

    //声明时Android程序
    //com.android.application 表示这是一个应用程序模块,可直接运行
    // com.android.library 标识这是一个库模块,是依附别的应用程序运行
    apply plugin: 'com.android.application'
    
    android {
    	// 编译sdk的版本,也就是API Level,例如API-20、API-28等等。
        compileSdkVersion 28
    	// build tools的版本,其中包括了打包工具aapt、dx等
    	// 这个工具的目录位于你的sdk目录/build-tools/下    
        buildToolsVersion "28.0.2"
    	// 默认配置
        defaultConfig {
            applicationId "zj.dzh.qq" //应用程序的包名
            minSdkVersion 16  //最小sdk版本,如果设备小于这个版本或者大于maxSdkVersion将无法安装这个应用
            targetSdkVersion 28 //目标sdk版本,充分测试过的版本(建议版本)
            versionCode 1//版本号,第一版是1,之后每更新一次加1
            versionName "1.0"//版本名,显示给用户看到的版本号
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"//Instrumentation单元测试
        }
    // 指定生成安装文件的配置,常有两个子包:release,debug,注:直接运行的都是debug安装文件
        buildTypes {
        // release版本的配置,即生成正式版安装文件的配置
            release {
                minifyEnabled false// 是否对代码进行混淆,true表示混淆
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'//是否支持调试
            }
        }
    
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])// 本地依赖
        // 远程依赖,androidx.appcompat是域名部分,appcompat是组名称,1.0.2是版本号
        implementation 'androidx.appcompat:appcompat:1.0.2'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'// 声明测试用列库
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }
    
    
    cs