组件化之gradle配置(一)

目录

  • gradle语法
  • 项目详细部署

gradle语法

开发环境 android studio 3.4.1

gradle版本 5.1.1

gradle是什么

lRHp7Q.md.png

gradle在AndroidStudio中的书写

在项目的主模块的gradle文件中写入如下代码:
lR7znS.md.png

则会在控制台中输入如下日志信息:
在这里插入图片描述

gradle在多模块代码中进行配置统一
练习gradle

准备:创建一个app项目,使得该项目包含app模块和library模块。
要求:通过创建一个新的gradle文件来统一各个模块中共有的配置。

操作步骤:

在项目的根目录下,新建名为config.gradle的配置文件,并且在项目根目录下,引入该配置文件。
在这里插入图片描述

测试:

在config.gradle中声明属性 username。并且在模块lirary中的gradle使用username属性,编译项目,查看输出日志信息。

config.gradle文件内容如下:
    //添加多个自定义属性,可以使用ext代码块
    ext{
        username = "zfc"
    }

lirarymodule下的build.gradle内容如下:
    ...
    println "${username}"
    rootProject.ext.username = 1234
    println "${rootProject.ext.username}"
    ...

编译项目得结果如下:
在这里插入图片描述

抽取统一配置
//声明变量
isRelease = true

//打印变量的值
print isRelease 

//定义字典
androidId=[
    compileSdkVersion:29,
    buildToolsVersion:"29.0.2",
    applicationId:"com.canjun.components",
    minSdkVersion:22,
    targetSdkVersion:29,
    versionCode:1,
    versionName:"1.0"
]

appId = [
        "app":"com.canjun.components",
        "order":"com.canjun.components.order",
        "personal":"com.canjun.components.personal"
]

url = [
        debug: "https://10.1.1.1/debug/rest",
        release: "https://10.1.1.1/release/rest",
]

supportLirary = "28.0.1"

dependencies = [
        appcompat:'androidx.appcompat:appcompat:1.1.0',
        constraintlayout:'androidx.constraintlayout:constraintlayout:1.1.3'
]
使用config.gradle中的统一配置
apply plugin: 'com.android.application'

def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies

android {
    compileSdkVersion androidId.compileSdkVersion
    buildToolsVersion androidId.buildToolsVersion
    defaultConfig {
        applicationId appId.app
        minSdkVersion androidId.minSdkVersion
        targetSdkVersion androidId.targetSdkVersion
        versionCode androidId.versionCode
        versionName androidId.versionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    ...
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
//    implementation 'androidx.appcompat:appcompat:1.1.0'
//    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

//    HashMap<String,String> map = new HashMap<>()

    support.each{ k,v-> implementation v}

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
设置常量属性到BuildConfig类中
buildConfigFiled("String","debug","\"${url.debug}\"")

apply plugin: 'com.android.application'
...
def url = rootProject.ext.url
...
android {
    ...
    buildTypes {
        debug{ buildConfigField("String","debug","\"${url.debug}\"")
        }
        release {
            buildConfigField("String","debug","\"${url.release}\"")
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

结果:会在主模块(app)目录下的BuildConfig中生成debug字段

安卓项目中常见的gradle配置
android{
    defaultConfig{
        ...
        ...
        //开启分包
        multiDexEnable true
        //设置分包配置
        multiDexKeepFile file('multidex-config.txt')
        
        //使用svg生成指定纬度的png图片
        vectorDrawbles.generatedDensities('xhdpi','xxhdpi')
        //使用v7包兼容(5.0以上版本)
        vectorDrawbles.useSupportLibrary = true
        //只保留指定和默认资源
        resConfig("zh-rCN")
        //ndk 默认保留架构
        ndk{
            abiFilters("armeabi","armeabi-v7a")
        }
        
        //设置源集属性
        sourceSets{
            main{
                if(!isRelease){
                    manifest.srcFile ['src/main/AndroidManifest.xml']
                    java.srcDirs ['src/main/java']
                    res.srcDirs ['src/main/res']
                    resources.srcDirs ['src/main/resources']
                    aidl.srcDirs ['src/main/aidl']
                    assets.srcDirs ['src/main/assets']
                }else{
                    mainfest.srcFile 'src/main/AndroidManifest.xml'
                }
            }
        }
     
    }
    
    //signingConfigs 的定义一定要放在buildTypes的前面
    signingConfigs{
        debug{
            storeFile file('xxx/xxxx/.android/debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
        release{
             // 签名证书文件
            storeFile file('D:/NetEase/netease.jks')
            // 签名证书的类型
            storeType "netease"
            // 签名证书文件的密码
            storePassword "net163"
            // 签名证书中密钥别名
            keyAlias "netease"
            // 签名证书中该密钥的密码
            keyPassword "net163"
            // 是否开启V2打包
            v2SigningEnabled true
        }
    }
    
    buildTypes{
        debug{
            signingConfig.debug   
        }
        
        release{
            signingConfig.release
            ...
        }
    }
    
    
}

//对adb配置
adbOptions{
    //操作超时时间为5秒
    timeOutInMs = 5 * 1000_0
    
    //adb install选项配置i
    installOptions '-r','-s'
}

// 对 dx 操作的配置,接受一个 DexOptions 类型的闭包,配置由 DexOptions 提供
dexOptions {
    // 配置执行 dx 命令是为其分配的最大堆内存
    javaMaxHeapSize "4g"
    // 配置是否预执行 dex Libraries 工程,开启后会提高增量构建速度,不过会影响 clean 构建的速度,默认 true
    preDexLibraries = false
    // 配置是否开启 jumbo 模式,代码方法是超过 65535 需要强制开启才能构建成功
    jumboMode true
    // 配置 Gradle 运行 dx 命令时使用的线程数量
    threadCount 8
    // 配置multidex参数
    additionalParameters = [
            '--multi-dex', // 多dex分包
            '--set-max-idx-number=50000', // 每个包内方法数上限
            // '--main-dex-list=' + '/multidex-config.txt', // 打包到主classes.dex的文件列表
            '--minimal-main-dex'
    ]
}
// 执行 gradle lint 命令即可运行 lint 检查,默认生成的报告在 outputs/lint-results.html 中
lintOptions {
    // 遇到 lint 检查错误会终止构建,一般设置为 false
    abortOnError false
    // 将警告当作错误来处理(老版本:warningAsErros)
    warningsAsErrors false
    // 检查新 API
    check 'NewApi'
}

项目详细部署

在这里插入图片描述

组件化的意义

在这里插入图片描述

PhoneModule 和 Android Libray区别
PhoneModule存在的配置:

apply plugin: 'com.android.application'
applicationId "com.canjun.components"

AndroidLibrary存在的配置:
apply plugin: 'com.android.library'
applicationId "com.canjun.components"(x)
集成化 和 组件化

集成化是一个project中只有一个为主模块,其他的模块为Lirary,这些Library不能单独运行。他们必须和主模块一起运行成为一个app

组件化是指一个Project中的各个module均能单独运行。

集成化用于生产环境,而组件化多用于开发测试阶段,其高度解耦的特性有利于开发测试。

项目组件化的配置

在这里插入图片描述

  1. 修改主模块的gradle配置

     apply plugin: 'com.android.application'
     def androidId = rootProject.ext.androidId
     def appId = rootProject.ext.appId
     def url = rootProject.ext.url
     def support = rootProject.ext.dependencies
     def isRelease = rootProject.ext.isRelease
     
     android {
         compileSdkVersion androidId.compileSdkVersion
         buildToolsVersion androidId.buildToolsVersion
         defaultConfig {
             applicationId appId.app
             minSdkVersion androidId.minSdkVersion
             targetSdkVersion androidId.targetSdkVersion
             versionCode androidId.versionCode
             versionName androidId.versionName
             testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
             buildConfigField("boolean","isRelease",String.valueOf(isRelease))
     //        multiDexEnable true
         }
         buildTypes {
             debug{
                 buildConfigField("String","debug","\"${url.debug}\"")
             }
             release {
                 buildConfigField("String","debug","\"${url.release}\"")
                 minifyEnabled false
                 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
             }
         }
     }
     
     dependencies {
         implementation fileTree(dir: 'libs', include: ['*.jar'])
         ...
         support.each{ k,v-> implementation v}
         implementation project(":common")
     
         //主模块不能依赖主模块
         if(isRelease){
             implementation project(":order")
             implementation project(":personal")
         }
         ...
     }
    
  2. 修改library模块的gradle配置

     // 要点一
     if(isRelease){
         apply plugin: 'com.android.library'
     }else {
         apply plugin: 'com.android.application'
     }
     
     def androidId = rootProject.ext.androidId
     def appId = rootProject.ext.appId
     def support = rootProject.ext.dependencies
     
     android {
         compileSdkVersion androidId.compileSdkVersion
         buildToolsVersion androidId.buildToolsVersion
         defaultConfig {
             minSdkVersion androidId.minSdkVersion
             // 要点二
             if(!isRelease){
                 applicationId appId.order
             }
     
             targetSdkVersion androidId.targetSdkVersion
             versionCode androidId.versionCode
             versionName androidId.versionName
     
             testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
             consumerProguardFiles 'consumer-rules.pro'
         }
     
         buildTypes {
             release {
                 minifyEnabled false
                 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
             }
         }
     
     }
     dependencies {
         implementation fileTree(dir: 'libs', include: ['*.jar'])
     
         support.each{ k,v -> implementation v}
     
         implementation project(":common")
     
         testImplementation 'junit:junit:4.12'
         androidTestImplementation 'androidx.test.ext:junit:1.1.1'
         androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     }
    

3.效果展示

集中式效果展示:

在这里插入图片描述

组件化效果展示:

在这里插入图片描述

组件化开发的临时代码,集中化打包时动态隔离

由于在组件化开发时,会在响应的moduel中写一些本地化代码,但是这些代码,不过不处理,会默认打包到主模块中。如下图所示:

lWD1vq.md.png

因此需要特殊处理一下:

  1. 配置gradle,进行代码隔离

在这里插入图片描述

  1. 将本地化代码按照gradle配置进行存储

在这里插入图片描述

3.进行验证

lWDNaF.md.png

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值