Gradle学习(三):Android Gradle插件

    xiaoxiao2025-11-07  18

    本篇将讲述Android Gradle插件在实际项目中的使用,以实际使用为例

    1、引如Android Gradle插件

    引入第三方插件的方法: 根build.gradle中:

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

    引入第三方gradle库的方法:先在根build.gradle中使用classpath引入路径,因为是在根工程中配置,所有子工程都不用配置了

    在其他模块中使用,如App模块,在模块下的build.gradle文件中:

    apply plugin : 'com.android.application' android{ compileSdkVersion 23 buildToolsVersion "23.0.1" }

    apply plugin: ‘com.android.application’ 创建应用项目 apply plugin: ‘com.android.library’ 创建库工程 apply plugin: ‘com.android.test’ 创建测试工程

    2、常用Android配置

    编译SDK版本和构建工具版本

    android { //编译Android工程的SDK版本 compileSdkVersion 23 //Android构建工具的版本 buildToolsVersion "23.0.1" }

    defaultConfig:默认配置,它是一个ProductFlavor

    defaultConfig { //配置包名 applicationId "com.breeze.qrcode" //最低支持的Android版本 minSdkVersion 15 //基于哪个Android版本开发 targetSdkVersion 26 //版本号 versionCode 1 //版本名称 versionName "1.0.0" }

    配置签名信息

    signingConfigs { release { //签名证书文件路径 storeFile file("myrelease.keystore") //签名证书文件密码 storePassword "password" //签名证书中的密钥别名 keyAlias "Alias" //签名证书中密钥的密码 keyPassword "password" } //配置多项 debug { //.... } } //使用 defaultConfig{ signingConfig signingConfigs.release }

    构建的应用类型

    android { buildTypes { release { //是否混淆 minifyEnabled true //配置是否启用自动拆分多个Dex multiDexEnabled true // 配置是否自动清理未使用的资源,默认未false shrinkResources true //签名配置 signingConfig signingConfigs.release //混淆文件配置 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //启用zip对齐 zipAlignEnabled true } //配置其他编译类型 debug { //不混淆 minifyEnabled false zipAlignEnabled true signingConfig signingConfigs.debug } } }

    修改生成的apk的名称 先介绍一个概念: 变体:打包渠道和编译类型的组合,对应一个apk包

    this.afterEvaluate { tasks['build'].doLast { android.applicationVariants.all { variant -> variant.outputs.each { output-> def fileName = "xxxxx.apk" if(variant.buildType.isDebuggable()) { fileName = "yyyy.apk" } def outputFile = output.outputFile File targetFile = new File(outputFile.parent, fileName) if (targetFile.exists()) { targetFile.delete() } if (outputFile != null && outputFile.name.endsWith(".apk")) { output.outputFile.renameTo(targetFile) } } } } }

    多渠道打包

    android { productFlavors { google { applicationId "com.xxx.game.google" manifestPlaceholders.put("UMENG_CHANNEL", "google") } } } //manifest文件中使用 <meta-data android:value="${UMENG_CHANNEL}" android:name="UMENG_CHANNEL"/>

    3.模块化时使用

    做模块化框架时会遇到将module独立成应用编译运行,和将module当作libiary的情况,并且使用不同的Manifest,不同的源码

    //isBuildModule定义的布尔值代表当前module要做lib还是application if (IsBuildModule.toBoolean()) { apply plugin: 'com.android.application' } else { apply plugin: 'com.android.library' } //配置相应的manifest sourceSets { main { if (IsBuildModule.toBoolean()) { manifest.srcFile 'src/main/debug/AndroidManifest.xml' } else { manifest.srcFile 'src/main/release/AndroidManifest.xml' //集成开发模式下排除debug文件夹中的所有Java文件 java { exclude 'debug/**' } } } }
    最新回复(0)