Import app compat dependency in all libraries

2019-02-16 01:47发布

问题:

I have 2 modules in my application, I want to modify both of them to use AppCompat Widgets for which I have to extend them with same. The problem is that I do not want to add appcompat dependency to each of them, So how could I possibly add Dependency to both modules and my app. If I do add

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

to every module, would it affect the application size?

回答1:

Using

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

in every module doesn't mean to add it twice or more times.

Gradle handles it for you adding the library only one time.

Working with multi-modules project, you can centralize the support libraries dependencies in gradle.

A very good way is to separate gradle build files, defining something like:

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

In 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}",
    ]
}

In the top level file 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'

In the module1/build.gradle:

// Module build file

dependencies {
    //......
    compile supportDependencies.appCompat
    compile supportDependencies.design
}