Import app compat dependency in all libraries

前端 未结 1 1010
既然无缘
既然无缘 2021-01-07 12:42

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 ad

1条回答
  •  时光说笑
    2021-01-07 13:24

    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
    }
    

    0 讨论(0)
提交回复
热议问题