【问题标题】:Import app compat dependency in all libraries在所有库中导入应用程序兼容依赖项
【发布时间】:2016-06-19 20:54:09
【问题描述】:

我的应用程序中有 2 个模块,我想修改它们以使用 AppCompat 小部件,我必须对其进行扩展。问题是我不想为每个模块添加 appcompat 依赖项,那么我怎么可能将依赖项添加到模块和我的应用程序中。如果我添加了

compile 'com.android.support:appcompat-v7:23.1.1'

对于每个模块,它会影响应用程序的大小吗?

【问题讨论】:

    标签: android gradle android-gradle-plugin android-support-library build.gradle


    【解决方案1】:

    使用

    compile 'com.android.support:appcompat-v7:23.1.1'
    

    在每个模块中并不意味着添加两次或更多次。

    Gradle 只为您添加一次库处理它。

    使用多模块项目,您可以将支持库依赖集中在 gradle 中。

    一个很好的方法是分离 gradle 构建文件,定义如下:

    root
      --gradleScript
      ----dependencies.gradle
      --module1
      ----build.gradle
      --module2
      ----build.gradle
      --build.gradle
    

    gradleScript/dependecies.gradle:

    ext {
        //Version
        supportLibrary = '23.2.0'
    
        //Support Libraries dependencies
        supportDependencies = [
                design           :         "com.android.support:design:${supportLibrary}",
                recyclerView     :         "com.android.support:recyclerview-v7:${supportLibrary}",
                cardView         :         "com.android.support:cardview-v7:${supportLibrary}",
                appCompat        :         "com.android.support:appcompat-v7:${supportLibrary}",
                supportAnnotation:         "com.android.support:support-annotations:${supportLibrary}",
        ]
    }
    

    在顶层文件build.gradle:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'
        }
    }
    
    // Load dependencies
    apply from: 'gradleScript/dependencies.gradle'
    

    module1/build.gradle

    // Module build file
    
    dependencies {
        //......
        compile supportDependencies.appCompat
        compile supportDependencies.design
    }
    

    【讨论】:

    • 使用排除组添加依赖项怎么样。
    猜你喜欢
    • 1970-01-01
    • 2021-09-11
    • 2012-01-22
    • 2021-10-24
    • 1970-01-01
    • 2015-04-28
    • 2017-01-30
    • 1970-01-01
    • 2018-06-08
    相关资源
    最近更新 更多