zoukankan      html  css  js  c++  java
  • Android Gradle统一依赖管理

    目的:

    避免在依赖包出新版本时,需要对每个module中的build.gradle文件都进行修改(如appcompat-v7包),使用这种方式即只需一次修改。

    方法一

    在项目的根目录创建一个gradle配置文件config.gradle,格式如下(内容根据需要进行修改):
    项目中所有的依赖只要在这个文件中统一做一次修改即可。

    其次在根目录的build.gradle文件中添加内容(apply from:”config.gradle”),如图: 

    加上这一行代码,意思就是所有的module都可以从这个(config.gradle)配置文件里读取内容

    在app(module)目录下的build.gradle文件中使用如下:

    android {  
              compileSdkVersion rootProject .ext.android.compileSdkVersion
              buildToolsVersion rootProject .ext.android.buildToolsVersion
              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 {
              ...
              compile rootProject .ext.dependencies[ "design"]
              compile rootProject .ext.dependencies[ "appcompat-v7"]
              compile rootProject .ext.dependencies[ "recyclerview-v7"]
              ...
          }

    方法二:

    config.gradle 文件如下

    ext {
        // Version of compile sdk
        COMPILE_SDK_VERSION = 23
        // Version of Android build tool
        BUILD_TOOLS_VERSION = "23.0.3"
        // Min version of Android sdk
        MIN_SDK_VERSION = 9
        // Version of target Android sdk
        TARGET_SDK_VERSION = 23
        // Use progurad or not
        MINIFY_ENABLED = true
        MINIFY_DISABLED = false
        // Version of "com.android.support:appcompat-v7", refer it as folow:
        //  compile "com.android.support:appcompat-v7:${APPCOMPAT_VERSION}"
        APPCOMPAT_VERSION = '23.2.0'
        // Version of "junit", refer it as folow:
        //  compile "junit:junit:${JUNIT_VERSION}"
        JUNIT_VERSION= '4.12'
    }
    ext {
        // Version of compile sdk
        COMPILE_SDK_VERSION = 23
        // Version of Android build tool
        BUILD_TOOLS_VERSION = "23.0.3"
        // Min version of Android sdk
        MIN_SDK_VERSION = 9
        // Version of target Android sdk
        TARGET_SDK_VERSION = 23
        // Use progurad or not
        MINIFY_ENABLED = true
        MINIFY_DISABLED = false
        // Version of "com.android.support:appcompat-v7", refer it as folow:
        //  compile "com.android.support:appcompat-v7:${APPCOMPAT_VERSION}"
        APPCOMPAT_VERSION = '23.2.0'
        // Version of "junit", refer it as folow:
        //  compile "junit:junit:${JUNIT_VERSION}"
        JUNIT_VERSION= '4.12'
    }

    其次在根目录的build.gradle文件中添加内容(apply from:”config.gradle”),如图:
    config.gradle 文件如下

    在app(module)目录下的build.gradle文件中使用如下:

    apply plugin : 'com.android.library'
    android {
        compileSdkVersion COMPILE_SDK_VERSION
        buildToolsVersion BUILD_TOOLS_VERSION
        defaultConfig {
            minSdkVersion MIN_SDK_VERSION
            targetSdkVersion TARGET_SDK_VERSION
            versionCode 1
            versionName "1.0.0"
        }
        buildTypes {
            release {
                minifyEnabled MINIFY_DISABLED
                proguardFiles getDefaultProguardFile('proguard-android.txt' ), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        compile fileTree(dir : 'libs', include: ['*.jar' ])
        testCompile "junit:junit: ${JUNIT_VERSION} "
        compile "com.android.support:appcompat-v7: ${APPCOMPAT_VERSION} "
    }

    方法三

    在属性文件(gradle.properties)中配置(google i/o 2015用的是这种方式):

    如:在gradle.properties中配置相应的版本号(可以理解为定义一个常量参数) 
    这里写图片描述

    在app/build.gradle中使用如:

    这里写图片描述

  • 相关阅读:
    将Apache2.4手动安装成Windows的服务
    [译文]PHP千年虫(y2k compliance)
    Apache2.4 authz_core_module模块使用
    Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.错误
    [转载]开启debug调试模式
    thinkphp 去掉URL 里面的index.php
    在WINDOWS下安装PEAR
    php5.5.15注释问题PHP Deprecated: Comments starting with '#' are deprecated in *.ini 警告解决办法
    Maven 与 IntelliJ IDEA 的完美结合
    JavaRebel 2.0 发布,一个JVM插件
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6841052.html
Copyright © 2011-2022 走看看