博客
关于我
Android Gradle配置说明
阅读量:612 次
发布时间:2019-03-12

本文共 5283 字,大约阅读时间需要 17 分钟。

Android Gradle 最佳实践指南

1. Android 项目整体结构

Android Gradle 项目采用模块化设计,核心文件包括:

  • build.gradle:定义项目依赖和构建工具版本
  • module.gradle:定义模块具体配置
  • config.gradle:全局配置中心,统一管理项目属性

2. build.gradle 配置

2.1 项目依赖

buildscript {    repositories {        google()        jcenter()    }    dependencies {        // 第三方插件版本控制        classpath "com.android.tools.build:gradle:4.1.2"    }}allprojects {    repositories {        google()        jcenter()    }}// 定义清理任务task clean(type: Delete) {    delete rootProject.buildDir}

2.2 模块配置

plugins {    id 'com.android.application'}android {    compileSdkVersion 30    buildToolsVersion "30.0.3"    defaultConfig {        applicationId "com.example.myapplication"        minSdkVersion 21        targetSdkVersion 30        versionCode 1        versionName "1.0"    }        signingConfigs {        release {            storeFile file('./key.jks')            storePassword "111111"            keyAlias "app"            keyPassword "123456"        }        debug {            storeFile file('./key.jks')            storePassword "111111"            keyAlias "app"            keyPassword "123456"        }    }    buildTypes {        release {            signingConfig signingConfigs.release            minifyEnabled true            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'            shrinkResources true            zipAlignEnabled true        }        debug {            signingConfig signingConfigs.debug            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'            applicationIdSuffix '.debug'            versionNameSuffix "_debug"        }    }    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}dependencies {    implementation fileTree(include: ['*.jar'], dir: 'libs')    implementation 'androidx.appcompat:appcompat:1.2.0'    implementation 'com.google.android.material:material:1.2.1'    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'    testImplementation 'junit:junit:4.+'    androidTestImplementation 'androidx.test.ext:junit:1.1.2'    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'}

3. 依赖管理

3.1 禁止依赖传递

configurations {    all*.exclude group: 'com.android.support', module: 'support-annotations'}

3.2 依赖冲突处理

all*.exclude group: 'com.android.support', module: 'support-annotations'

4. 全局配置

4.1 config.gradle 文件

ext {    isModule = false    android = [        compileSdkVersion: 30,        applicationId: "com.example.myapplication",        minSdkVersion: 21,        targetSdkVersion: 30,        versionCode: 1,        versionName: "1.0"    ]    dependencies = [        "appcompat": 'androidx.appcompat:appcompat:1.2.0',        "material": 'com.google.android.material:material:1.2.1',        "circleimageview": 'de.hdodenhof:circleimageview:3.0.0'    ]}

4.2 build.gradle 应用

apply from: "config.gradle"

4.3 模块使用

android {    compileSdkVersion rootProject.ext.android["compileSdkVersion"]    defaultConfig {        applicationId rootProject.ext.android["applicationId"]        minSdkVersion rootProject.ext.android["minSdkVersion"]        targetSdkVersion rootProject.ext.android["targetSdkVersion"]        versionCode rootProject.ext.android["versionCode"]        versionName rootProject.ext.android["versionName"]    }}dependencies {    implementation rootProject.ext.dependencies["appcompat"]    implementation rootProject.ext.dependencies["material"]    implementation rootProject.ext.dependencies["circleimageview"]}

5. implementation vs api

  • api:支持传递依赖,模块间依赖会影响所有模块编译。
  • implementation:不支持传递依赖,模块间依赖只影响直接依赖模块编译。

6. 多渠道构建

6.1 单维度配置

flavorDimensions 'channel'productFlavors {    xiaomi {        applicationId 'com.test.appid_xiaomi'        buildConfigField "String", "baseUrl", '"www.xiaomi.com"'        resValue "string", "tip", "hello 小米"        manifestPlaceholders = [CHANNEL_VALUE: "xiaomi", APP_NAME: "小米"]    }    huawei {        applicationId 'com.test.appid_huawei'        buildConfigField "String", "baseUrl", '"www.huawei.com"'        resValue "string", "tip", "hello 华为"        manifestPlaceholders = [CHANNEL_VALUE: "huawei", APP_NAME: "华为"]    }}

6.2 多维度配置

flavorDimensions 'channel', 'app'productFlavors {    v1 {        dimension "app"        applicationId 'com.test.appid_v1'    }    v2 {        dimension "app"        applicationId 'com.test.appid_v2'    }    xiaomi {        dimension "channel"        applicationId 'com.test.appid_xiaomi'        manifestPlaceholders = [CHANNEL_VALUE: "xiaomi", APP_NAME: "小米"]        buildConfigField "String", "baseUrl", '"www.xiaomi.com"'    }    huawei {        dimension "channel"        applicationId 'com.test.appid_huawei'        manifestPlaceholders = [CHANNEL_VALUE: "huawei", APP_NAME: "华为"]        buildConfigField "String", "baseUrl", '"www.huawei.com"'    }}

7. 定制apk名称

applicationVariants.all {    variant ->    variant.outputs.all {        output ->        def outputFile = output.outputFile        if (outputFile != null && outputFile.name.endsWith(".apk")) {            def flavorsName = variant.productFlavors[0].name            outputFileName = "app_${flavorsName}.apk"        }    }}

通过以上配置,开发者可以高效管理Android项目的构建过程,实现代码复用和依赖管理,同时支持多渠道构建和版本控制。

转载地址:http://zrqxz.baihongyu.com/

你可能感兴趣的文章
Node-RED中建立Websocket客户端连接
查看>>
Node-RED中建立静态网页和动态网页内容
查看>>
Node-RED中解析高德地图天气api的json数据显示天气仪表盘
查看>>
Node-RED中连接Mysql数据库并实现增删改查的操作
查看>>
Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
查看>>
Node-RED中配置周期性执行、指定时间阶段执行、指定时间执行事件
查看>>
Node-RED安装图形化节点dashboard实现订阅mqtt主题并在仪表盘中显示温度
查看>>
Node-RED怎样导出导入流程为json文件
查看>>
Node-RED订阅MQTT主题并调试数据
查看>>
Node-RED通过npm安装的方式对应卸载
查看>>
node-request模块
查看>>
node-static 任意文件读取漏洞复现(CVE-2023-26111)
查看>>
Node.js 8 中的 util.promisify的详解
查看>>
node.js debug在webstrom工具
查看>>
Node.js RESTful API如何使用?
查看>>
node.js url模块
查看>>
Node.js Web 模块的各种用法和常见场景
查看>>
Node.js 之 log4js 完全讲解
查看>>
Node.js 函数是什么样的?
查看>>
Node.js 函数计算如何突破启动瓶颈,优化启动速度
查看>>