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

你可能感兴趣的文章
nodejs 运行CMD命令
查看>>
Nodejs+Express+Mysql实现简单用户管理增删改查
查看>>
nodejs+nginx获取真实ip
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs下的express安装
查看>>
nodejs与javascript中的aes加密
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
nodejs中express的使用
查看>>
Nodejs中搭建一个静态Web服务器,通过读取文件获取响应类型
查看>>
Nodejs中的fs模块的使用
查看>>
NodeJS使用淘宝npm镜像站的各种姿势
查看>>
NodeJs入门知识
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs在Liunx上的部署生产方式-PM2
查看>>
nodejs基于art-template模板引擎生成
查看>>