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

你可能感兴趣的文章
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—5.Pipeline和Handler二
查看>>
Netty源码—6.ByteBuf原理一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理一
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Netty遇到TCP发送缓冲区满了 写半包操作该如何处理
查看>>
Netty:ChannelPipeline和ChannelHandler为什么会鬼混在一起?
查看>>
Netty:原理架构解析
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
Network 灰鸽宝典【目录】
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>