zoukankan      html  css  js  c++  java
  • gradle配置优化及dependencies中各种依赖方式说明

    一.前言
    当我们用AndroidStudio新建项目时候发现默认的compile已经改为了implementation.
    implementation以前并没有接触过,这里干脆总结一下dependencies中各种依赖方式的区别.

    二.各种依赖方式说明
    implementation
    这个指令的特点就是,对于使用了该命令编译的依赖,对该项目有依赖的项目将无法访问到使用该命令编译的依赖中的任何程序,也就是将该依赖隐藏在内部,而不对外部公开。
    api
    完全等同于compile指令。

    compile
    这种是我们最常用的方式,使用该方式依赖的库将会参与编译和打包。

    testCompile
    testCompile 只在单元测试代码的编译以及最终打包测试apk时有效。
    debugCompile
    debugCompile 只在debug模式的编译和最终的debug apk打包时有效。
    releaseCompile
    releaseCompile 仅仅针对Release模式的编译和最终的Release apk打包。这里比如
    这里比如我们使用的leakcanary

    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.+'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.+'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.+'
     
    provided
    只在编译时有效,不会参与打包,可以在自己的moudle中使用该方式依赖。比如com.android.support,gson这些使用者常用的库,避免冲突。

    apk(runtimeOnly)
    只在生成apk的时候参与打包,编译时不会参与,很少用。

    三.依赖版本号处理
    正常我们引入一个库可能直接指定了版本号
    compile ‘com.google.code.gson:gson:2.8.0’
    那么可以不指定版本号吗?答案是可以的,比如:
    compile ‘com.google.code.gson:gson:2.+’ 引入gson 大版本为2的包
    compile ‘com.google.code.gson:gson:latest.release’引入gson 最新的包

    四.统一管理版本号
    1.在根目录下的build.gradle文件下添加 ext{ …. } 中的内容

    ext{
    //dependencies
    supportLibraryVersion ='26.1.0'
    gsonVersion = '2.8.0'
    }
     
    使用示例
    compileSdkVersion rootProject.ext.COMPILE_SDK_VERSION

    2.使用自定义gradle
    当然以上方式还有更好的解决方案
    首先我们在项目根目录下创建一个任意命名的xxx.gradle文件. 例如 : config.gradle

    ext {
    android = [
    compileSdkVersion: 26,
    buildToolsVersion: "25.0.0",
    minSdkVersion : 14,
    targetSdkVersion : 22,
    versionCode : 17,
    versionName : "1.7",
    applicationId : "com.king.headline",
    applicationId2222: "com.king.headline",
    ]

    dependencies = [
    appcompatv7: "com.android.support:design:22.2.0",
    loadtoast: "net.steamcrafted:load-toast:1.0.6",
    constraintlayout: "com.android.support.constraint:constraint-layout:1.0.2"
    ]

    }
     
    使用

    根目录下的build.gradle于引用当前gradle
    apply from : “config.gradle”
    app下的build.gradle先定义出引用
    def cfg = rootProject.ext.android
    def dpc = rootProject.ext.dependencies
    使用
    compileSdkVersion cfg.compileSdkVersion
    buildToolsVersion cfg.buildToolsVersion
    4.完整示例
    apply plugin: 'com.android.application'
    def cfg = rootProject.ext.android
    def dpc = rootProject.ext.dependencies
    android {
    compileSdkVersion cfg.compileSdkVersion
    buildToolsVersion cfg.buildToolsVersion

    defaultConfig {
    applicationId "com.king.headline"
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 17
    versionName "1.7"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    }

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile dpc.loadtoast
    compile dpc.appcompatv7
    compile dpc.constraintlayout
    }
    ————————————————
    版权声明:本文为CSDN博主「金福林」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/jinfulin/article/details/80421927

  • 相关阅读:
    TP5手动引入PHPEXCEL的方法
    分库分表的基本思想
    FIFO、LRU、LFU的含义和原理
    Memcache 内存分配策略和性能(使用)状态检查
    php soapclient 超时 设置
    jsonrpc.js -- 原生js实现 JSON-RPC 协议
    前端编译原理 笔记 -- BISON
    前端编译原理 移进规约自动机
    前端编译原理 parser.js源码解读
    react-rotuer动态加载模块
  • 原文地址:https://www.cnblogs.com/javalinux/p/14791042.html
Copyright © 2011-2022 走看看