build.gradle文件

1. 前言

Android studio采用的是gradle来构建项目。

2. Project下的build.gradle文件

但由于gradle这个项目构建工具却不是专门为了Android开发而写的,故而可以支持其他的语言,如JavaC++等。故而在当前项目的build.gradle文件中需要指定该项目所依赖的gradle版本。
如:

buildscript {
    
    
    repositories {
    
    
        jcenter()
    }
    dependencies {
    
    
        classpath 'com.android.tools.build:gradle:3.5.2'
    }
}

allprojects {
    
    
    repositories {
    
    
        jcenter()
    }
}

而,上面所见的repositories是指明仓库地址,因为我们会使用到jcenter这个开源库中的一些开源项目。
当前的gradle版本指定为3.5.2

3. app下的build.gradle文件

apply plugin: 'com.android.application' 

android {
    
    
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
    defaultConfig {
    
    
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    
    
        release {
    
    
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    
    
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    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.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

第一行指定是应用程序模块还是库模块,一般有两个值可以选择:

  • com.android.application
  • com.android.library

比较有意思的是plugin是插件意思。

defaultConfig中我们对项目进行简单的配置。如:applicationId用于指定项目的包名
buildTypes中,指定生成安装文件的相关配置。如:minifyEnabled 用于指定是否对项目的代码进行混淆,而proguardFiles用于指定在混淆时候的规则文件。'proguard-android-optimize.txt是在Andorid SDK目录下,而proguard-rules.pro是在当前项目根目录下,该文件的具体细节有一个官方的地址:here

dependencies中,用于指定当前项目的依赖关系,一共有三种依赖方式:

  • 本地依赖;
  • 库依赖;
  • 远程依赖;

本地依赖也就是对本地的Jar或者目录添加依赖关系;
库依赖是指可以对项目中的库模块添加依赖;
远程依赖,则是指对jcenter中的开源项目的依赖;

implementation fileTree是本地依赖声明;
implementation是远程依赖声明,如:implementation 'androidx.appcompat:appcompat:1.0.2'
implementation project(':helper')库依赖;

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/113438726