Android Gradle include module dependencies in another module

前端 未结 2 2013
不思量自难忘°
不思量自难忘° 2021-02-06 10:42

I am using the latest Android Studio 3.0.0-beta6 to build my Android project and there\'s this dependency issue. Gradle encouraged me to replace all compile\'

相关标签:
2条回答
  • 2021-02-06 10:54

    In your Root Project -> build.gradle you should have something like:

    allprojects {
        repositories {
            jcenter()
        }
    }
    

    Add dependencies there!!

    UPDATE

    If you do not want all your modules to have the common dependencies but only specific modules then do this:

    1. Create a folder in your Root Project/gradleScript
    2. Create a .gradle file in this folder (e.g. gradleScript/dependencies.gradle) that looks like:

      ext {
      
      //Version
      
      supportLibrary = '22.2.1'
      
      //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}",
      ]
      }
      
    3. In your root project build.gradle add this line:

      buildscript {
      repositories {
          jcenter()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:1.2.3'
      }
      }
      
      // Load dependencies
      apply from: 'gradleScript/dependencies.gradle'
      
    4. In your modules accordingly add this:

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

    Hope this helps!!!

    0 讨论(0)
  • 2021-02-06 11:02

    Old question, but it sounds to me like what you're after is simply to use api instead of implementation in your gradle files?

    If a module X declares an api dependency, it's transitively available to any other module that depends on X. If it however declares an implementation dependency, the classes from that dependency is put on the class path for module X, but not for any modules in turn depending on module X.

    From Gradle's documentation:

    Dependencies appearing in the api configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers. Dependencies found in the implementation configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath.

    So to be concrete:

    module 1:

    dependencies {
        ....
        api 'io.reactivex.rxjava2:rxandroid:2.0.1'
        api 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
        ...
    }
    

    module 2:

    implementation project(':module1')
    

    Isn't this the simplest solution to what you're trying to do?

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