博客
关于我
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/

你可能感兴趣的文章
Springboot中@SuppressWarnings注解详细解析
查看>>
Panalog 日志审计系统 sprog_deletevent.php SQL 注入漏洞复现
查看>>
Panalog 日志审计系统 sprog_upstatus.php SQL 注入漏洞复现(XVE-2024-5232)
查看>>
Panalog 日志审计系统 前台RCE漏洞复现
查看>>
PANDA VALUE_COUNTS包含GROUP BY之前的所有值
查看>>
pandas - 如何将所有列从对象转换为浮点类型
查看>>
Pandas - 按列分组并将数据转换为 numpy 数组
查看>>
Pandas - 有条件的删除重复项
查看>>
pandas -按连续日期时间段分组
查看>>
pandas -更改重新采样的时间序列的开始和结束日期
查看>>
SpringBoot+Vue+Redis前后端分离家具商城平台系统(源码+论文初稿直接运行《精品毕设》)15主要设计:用户登录、注册、商城分类、商品浏览、查看、购物车、订单、支付、以及后台的管理
查看>>
pandas :to_excel() float_format
查看>>
pandas :从数据透视表中的另一列中减去一列
查看>>
pandas :加入有条件的数据框
查看>>
pandas :将多列汇总为一列,没有最后一列
查看>>
pandas :将时间戳转换为 datetime.date
查看>>
pandas :将行取消堆叠到新列中
查看>>
pandas :设置编号.最大行数
查看>>
pandas DataFrame 中的自定义浮点格式
查看>>
Pandas DataFrame 的 describe()方法详解-ChatGPT4o作答
查看>>