观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Module的build.gradle的使用记录

区分module是library还是application

application是项目的壳工程,是打包的主工程

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

library是项目的库工程

plugins {
    id 'com.android.library'
    id 'kotlin-android'
}

将打包apk重命名

android {
    
    //命令打包apk重命名
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "YT-${variant.name}-${variant.versionName}.apk"
        }
    }
}

设置签名密钥

 

android {  
   //略...
    
// 配置签名信息
    signingConfigs {
        release {
            storeFile file('keystore/abc.jks')
            keyAlias "abc"
            keyPassword "abc"
            storePassword "abc"
        }
        debug {
            storeFile file('keystore/abc.jks')
            keyAlias "abc"
            keyPassword "abc"
            storePassword "abc"
        }
    }

 

manifestPlaceholders妙用

https://www.cnblogs.com/zhaohongtian/p/6808962.html 参考

       String jpush_appkey;
        try {
            ApplicationInfo appInfo = getPackageManager()
                    .getApplicationInfo(getPackageName(),
                            PackageManager.GET_META_DATA);

            jpush_appkey = appInfo.metaData.getString("JPUSH_APPKEY");

            Logger.d("jpush_appkey=" + jpush_appkey);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();

        }

在build.gradle配置常量

    defaultConfig {
       //略...

        /**
         * 自定义配置
         */
        buildConfigField "boolean", "isHorizontalScreen", "true" //是否横竖屏幕

    }

调用

if (BuildConfig.isHorizontalScreen){ 
    //略... 
}

资源名增加前缀

android {
   //略...
    resourcePrefix "snake_" //给 Module内的资源名增加前缀, 避免资源名冲突
}

buildTypes编译类型配置

 buildTypes {
        release {
            minifyEnabled true //混淆
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' //指定混淆规则
            buildConfigField "String", "base_url", "\"www.test.com\""
            //启用zipalign优化zipalign是Android提供的一个整理优化apk文件的工具,可在一定程度上上提高系统和应用的运行效率,更快的读取apk中的资源,降低内存的使用.
            //开启zipalign优化只需要在buildTypes{}中对应的构建类型下开启zipalign优化即可
            zipAlignEnabled true
            //... 同下
        }
        debug{
            minifyEnabled false //混淆
            //指定混淆规则
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            buildConfigField "String", "base_url", "\"www.baidu.com\""
            //启用zipalign优化zipalign是Android提供的一个整理优化apk文件的工具,可在一定程度上上提高系统和应用的运行效率,更快的读取apk中的资源,降低内存的使用.
            //开启zipalign优化只需要在buildTypes{}中对应的构建类型下开启zipalign优化即可
            zipAlignEnabled true
            //配置是否自动清理未使用的资源,默认为false
            shrinkResources false
            //配置签名
            signingConfig signingConfigs.debug
            //配置在当前构建类型下applicationId的后缀,构建生成Apk的包名会在applicationId的基础上添加后缀
            applicationIdSuffix '.debug'
            //配置是否生成一个可供调试的Apk
            denbuggable true
            //配置是否生成一个可供调试jni(c/c++)代码的Apk
            jniDebuggable true
            //是否启用proguard混淆
            minifyEnabled true
            //配置当程序中方法数超过65535个时,是否启用自动拆分多个dex的功能,
            multiDexEnabled true
        }
    }

解决依赖冲突

在build里添加冲突的库强制所使用的版本

defaultConfig {  
   //略... configurations.all{
//因为fastjson与com.aliyun.alink.linksdk依赖冲突,这个库使用1.2.40的fastjson.在这里强制统一使用1.1.67版本 resolutionStrategy{ force 'com.alibaba:fastjson:1.1.67.android' } }

根 build.gradle的使用

使用一个统一的配置文件进行多个module的项目配置

根目录下创建common.gradle

ext {
    //说明:
    //所有的编译变量全在这个文件定义,否则可能编译不过。
    //本文件定义的变量是默认值,需要修改默认值需要在对应的config下面重新定义,会做覆盖
    //属于公共的属性才放这里,否则放在各自的config下面

    println("config base.gradle is loaded ...\n")

    //标识当前是那种编译模式,false: 模块作为Lib组件存在, true: 模块作为application存在
    isAlone = false

    //标识当前的壳APP是哪个,定义在这里,为子模块的脚本做好相应配置,字母小写
    // "s01" 智能门S01; "s06" 智能门S06; "tuya" 涂鸦智能门; "know" 如影智能门; "yt08" snake横板智能门;"s16" snake竖版智能门;"test" 测试用;
    app_name = "yt08"

    //编译的apk版本号,单数测试版本,双号正式版本
    version_code = 1
    version_name = "0.0.1"

    //编译环境,是否是测试环境版本
    debug_mode = true

    //是否支持普通的设备,如果设置为true,则会写死SN号等避免修改平台的一些API,在普通手机上运行,这样可以在没有设备的情况下调试UI这些。
    support_normal_device = true

    //是否支持内存泄漏工具
    leakcanary = false

    //是否支持ANR检测工具
    blockcanary = false

    //gradle版本
    gradle_version = "4.1.1"

    //kotlin插件版本
    kotlin_version = "1.4.10"


    //android工具编译版本
    common_android = [
        compileSdkVersion: 30,
        buildToolsVersion: '30.0.2',
        minSdkVersion    : 23,
        targetSdkVersion : 30
    ]

    //把常用的稳定的经过长时间市场检验的库放到Config.gradle文件配置,统一这些都需要用到的第三方库,这样各个业务模块尽量是使用一样的库版本,减少差异性
    dependencies = [
        material                  : "com.google.android.material:material:1.2.1",
        appcompatx                : "androidx.appcompat:appcompat:1.2.0",
        annotationx               : "androidx.annotation:annotation:1.1.0",
        constraintlayoutx         : "androidx.constraintlayout:constraintlayout:1.1.3",
        swiperefreshlayoutx       : "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-rc01",

        //谷歌导航
        "navigation_fragment"     : "androidx.navigation:navigation-fragment:2.3.0",
        "navigation_ui"           : "androidx.navigation:navigation-ui:2.3.0",

        gson                      : "com.google.code.gson:gson:2.8.6",
        retrofit2                 : "com.squareup.retrofit2:retrofit:2.6.2",
        retrofit2_converter_gson  : "com.squareup.retrofit2:converter-gson:2.4.0",//okhttp
        okhttp3                   : "com.squareup.okhttp3:okhttp:4.4.0",
        //okhttp网络日志拦截器
        okhttp3_logging           : "com.squareup.okhttp3:logging-interceptor:4.4.0",

        multidex                  : "com.android.support:multidex:1.0.1",

        //路由通讯
        arouter_api               : "com.alibaba:arouter-api:1.5.0",
        arouter_compiler          : "com.alibaba:arouter-compiler:1.2.2",

        //内存泄露检测工具
        leakcanary                : 'com.squareup.leakcanary:leakcanary-android:1.6.3',
        leakcanary_no_op          : 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3',
        leakcanary_fragment       : 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3',

        //腾讯崩溃和ANR检测工具bugly
        bugly                     : "com.tencent.bugly:crashreport:3.3.3",
        bugly_ndk                 : 'com.tencent.bugly:nativecrashreport:3.7.7',

        //谷歌数据库room
        room_testing              : "androidx.room:room-testing:2.2.5",
        room_runtime              : "androidx.room:room-runtime:2.2.5",
        room_compiler             : "androidx.room:room-compiler:2.2.5",

        lifecycle_extensions      : "androidx.lifecycle:lifecycle-extensions:2.2.0",
        lifecycle_compiler        : "androidx.lifecycle:lifecycle-compiler:2.3.0-alpha01",

        //Facebook的图片缓存框架
        fresco                    : "com.facebook.fresco:fresco:2.1.0",

        //通用工具类,超全
        utilcodex                 : "com.blankj:utilcodex:1.29.0",

        //二维码识别工具,采用zbar识别,识别率比zxing高和快
        zbar                      : "cn.bingoogolapple:bga-qrcode-zbar:1.3.7",
        zxing                     : 'cn.bingoogolapple:bga-qrcode-zxing:1.3.7',

        //屏幕适配方案,采用今日头条的适配算法
        autosize                  : "me.jessyan:autosize:1.2.1",

        //ANR检测工具
        blockcanary               : "com.github.markzhai:blockcanary-android:1.5.0",
        blockcanary_no_op         : "com.github.markzhai:blockcanary-no-op:1.5.0",

        //WorkManager
        "work_runtime"            : "androidx.work:work-runtime:2.4.0",
    ]

}

导入common.gradle在根目录下的build

buildscript {
    //引入放这里,这里是第一个首先调用的
    apply from: "common.gradle"
    ext.kotlin_version = '1.3.61'

//略...

在子module项目下的build里使用它:

请注意在使用  buildConfigField "boolean", "BUILD_ALONE", rootProject.ext.isAlone.toString()  的时候一定需要使用toString() 

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
}
android {
    compileSdkVersion common_android.compileSdkVersion
    buildToolsVersion common_android.buildToolsVersion
    defaultConfig {
        minSdkVersion common_android.minSdkVersion
        targetSdkVersion common_android.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        buildConfigField "boolean", "BUILD_ALONE", rootProject.ext.isAlone.toString()
        buildConfigField "String", "APP_NAME", "\"" + rootProject.ext.app_name.toString() + "\""
        buildConfigField "boolean", "SUPPORT_NORMAL_DEVICE", rootProject.ext.support_normal_device.toString()
        buildConfigField "boolean", "SHOW_NAVIGATE_KEY", rootProject.ext.show_navigate_key.toString()
        buildConfigField "boolean", "DEBUG_MODE", rootProject.ext.debug_mode.toString()
        buildConfigField "boolean", "LEAK_CANARY", rootProject.ext.leakcanary.toString()
        buildConfigField "boolean", "BLOCK_CANARY", rootProject.ext.blockcanary.toString()
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    resourcePrefix "snakecommon_" //给 Module 内的资源名增加前缀, 避免资源名冲突

}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    api rootProject.ext.dependencies.zxing
    api rootProject.ext.dependencies.material
    api rootProject.ext.dependencies.appcompatx
    api rootProject.ext.dependencies.annotationx
    api rootProject.ext.dependencies.constraintlayoutx
    api rootProject.ext.dependencies.swiperefreshlayoutx
    api rootProject.ext.dependencies.gson
    api rootProject.ext.dependencies.retrofit2
    api rootProject.ext.dependencies.retrofit2_converter_gson
    api rootProject.ext.dependencies.rxjava2
    api rootProject.ext.dependencies.rxandroid2
    api rootProject.ext.dependencies.okhttp3
    api rootProject.ext.dependencies.okhttp3_logging
    api rootProject.ext.dependencies.lifecycle_extensions
    annotationProcessor rootProject.ext.dependencies.lifecycle_compiler
    api  rootProject.ext.dependencies.room_runtime
    annotationProcessor rootProject.ext.dependencies.room_compiler
    //Facebook的图片缓存框架
    api rootProject.ext.dependencies.fresco
    api rootProject.ext.dependencies.eventBus
    api rootProject.ext.dependencies.arouter_api
    //navigation_ui
    api rootProject.ext.dependencies.navigation_fragment
    api rootProject.ext.dependencies.navigation_ui
    //内存泄漏
    if(rootProject.ext.leakcanary){
        api rootProject.ext.dependencies.leakcanary
        api rootProject.ext.dependencies.leakcanary_fragment
    }else{
        api rootProject.ext.dependencies.leakcanary_no_op
    }
    //二维码
    api rootProject.ext.dependencies.zbar
    api rootProject.ext.dependencies.zxing
    //字节跳动自适应屏幕
    if(rootProject.ext.app_name == "yt08"){
        implementation 'me.jessyan:autosize:1.2.1'
    }//WorkManager后面必须跟着implementation 'com.google.guava:guava:27.0.1-android',不然后面会类重复定义的错误,而且要放在依赖的最后
    api  rootProject.ext.dependencies.work_runtime
    api 'com.google.guava:guava:27.0.1-android'
    //滚轮view
    api 'com.cncoderx.wheelview:library:1.2.5'
}

 

 

 

 

end

posted on 2021-05-28 11:27  观心静  阅读(475)  评论(0编辑  收藏  举报