博客
关于我
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.js基于Express框架搭建一个简单的注册登录Web功能
查看>>
node.js学习之npm 入门 —8.《怎样创建,发布,升级你的npm,node模块》
查看>>
Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
查看>>
Node.js安装及环境配置之Windows篇
查看>>
Node.js安装和入门 - 2行代码让你能够启动一个Server
查看>>
node.js安装方法
查看>>
Node.js官网无法正常访问时安装NodeJS的方法
查看>>
node.js模块、包
查看>>
node.js的express框架用法(一)
查看>>
Node.js的交互式解释器(REPL)
查看>>
Node.js的循环与异步问题
查看>>
Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
查看>>
nodejs + socket.io 同时使用http 和 https
查看>>
NodeJS @kubernetes/client-node连接到kubernetes集群的方法
查看>>
NodeJS API简介
查看>>
Nodejs express 获取url参数,post参数的三种方式
查看>>
nodejs http小爬虫
查看>>
nodejs libararies
查看>>
nodejs npm常用命令
查看>>
nodejs npm常用命令
查看>>