Android Gradle 使用

182 阅读2分钟

一、什么是Gradle

1、概念

Gradle是基于Ant和Maven自动化构建开源工具,使用Groovy语言来设置

2、什么叫构建工具?

负责编译、运行、签名、打包、包依赖管理的工具

二、Gradle和Android Studio关系

Gradle其实本来和Android Stuio没啥关系。 但是Google在推Android Stuio的时候选中了Gradle,于是Google就做了一个Android Gradle Plugin的插件,用来支持Android项目的编译、运行、签名、打包、包依赖管理

三、执行顺序和常规设置介绍

1、项目Gradle执行顺序

settings.gradle->build.gradle(工程级)->build.gradle(app目录下)->build.gradle(其它module)

2、每个gradle负责内容

2.1 settings.gradle:指定工程所依赖的所有module

2.2 build.gradle(工程级)
2.2.1 指定maven仓库(jCenter、google、aliyun、公司自己的私有仓库等)
2.2.2 强制指定依赖库的版本号
2.2.3 引用自定义gradle文件

2.3 build.gradle(app目录下)
2.3.1、指定各种编译条件1、最低支持版本,target版本,包名等

android {
    defaultConfig {
       //指定包名
        applicationId "com.xxx.yyy"
        //最低支持版本
        minSdkVersion 17
        //系统兼容target版本
        /*高版本手机兼容:当运行在系统10.0,SDK_INT=29的设备上时,调用SDK 20的版本API,及时当10.0手机的特性已经改变,但是还是运行SDK20的API。低版本手机兼容:比如targetSdkVersion 29,当运行在系统8.0,SDK_INT=27的设备上时,调用SDK 29的版本API,用到新API要做好兼容。if( Build.VERSION.SDK_INT<29){.....}else{......}*/
        targetSdkVersion 27
        //版本号
        versionCode 109
        //版本名
        versionName "1.0.9"
    }
}

2.3.2、包依赖管理

dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'com.jakewharton:butterknife:10.2.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.10"
    implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar'
    implementation 'com.google.android.material:material:1.2.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}

2.3.3、签名

signingConfigs {
   release {
       keyAlias 'xxx'
       keyPassword '123456'
       storeFile file("${rootProject.rootDir}/yyy.keystore")
       storePassword '123456'
   }
   debug {
       keyAlias 'zzz'
       keyPassword '123456'
       storeFile file("${rootProject.rootDir}/yyy.keystore")
       storePassword '123456'
   }
}

2.3.4、混淆

buildTypes {
   release {
       //是否启动混淆 ture:打开 false:关闭
       minifyEnabled false
       //指定混淆文件
       proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
   }
}

2.3.5、指定java版本、指定ndk版本

compileOptions {
   sourceCompatibility JavaVersion.VERSION_1_8
   targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
   multiDexEnabled true
   //指定ndk版本
   ndkVersion = "21.1.6352462"
   ndk {
       //指定cpu架构
       abiFilters "armeabi-v7a", "armeabi-v8a" , "arm64-v8a"/*,"armeabi", "x86_64", "x86", "mips"*/
   }
}

四、实际配置使用

4.1 通过脚本读取参数

-P:指定参数并赋值,这个参数可在gradle里面读取到

gradle54 -PversionName=${BUILD_SID} -PbuildSource=${BUILD_SOURCE} -PversionCode=${BUILD_SID}  clean assembleRelease --stacktrace --no-daemon

参数读取:

//在settings.gradle里面读取
def currentVersionName = ""
if (hasProperty('versionName')) {
    currentVersionName = versionName
}

//在build.gradle(app)里面读取
def ossVersion = '2.1.0'

if (project.hasProperty('ossVersion')) {
    ossVersion = project.ossVersion
}

4.2 引用自定义gradle文件

apply from: 'publish_sdk.gradle'
//引入插件
apply plugin: 'maven'

4.3 将gradle变量写入Java的config文件,供java文件读取

buildConfigField "String","stage", ""${stageconfig}""
//java来读取
if(BuildConfig.stage.equals("test")){
    this.host = "xxxxxx";
}else{
    this.host = "yyyyyy";
}