Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle include module dependencies in another module

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's with implementation's. Here's my project structure:

Project:

  • module1

    • module2

Module1 depends on some libraries, module2 depends on module1. However, the libraries from module1 are not visible in module2. I don't want to copy-paste dependencies and would rather have the library dependencies declared only once. Is there a simple solution to this? Thank you.

module1's build gradle:

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

module2's build gradle:

implementation project(':module1')
like image 471
Євген Гарастович Avatar asked Oct 02 '17 13:10

Євген Гарастович


People also ask

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your build.gradle file. For example, the following build.gradle file for an app module includes three different types of dependencies: android { ... } android { ...

Can a module consume a dependency from another module in Android?

Warning: It is not currently possible for a module to consume a dependency produced by another module in the same project. See Bug 120166563 for more information. Starting with Android Gradle plugin 4.0, C/C++ dependencies can be imported from AARs linked in your build.gradle file.

What is the Android plugin for Gradle for Android?

The Android plugin for Gradle provides a task that displays a list of the dependencies Gradle resolves for a given module. For each module, the report also groups the dependencies based on build variant, testing source set, and classpath.

How to include a native library project as a Gradle build dependency?

To include your native library project as a Gradle build dependency, you need to provide Gradle with the path to your CMake or ndk-build script file.


1 Answers

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!!!

like image 132
matrix Avatar answered Nov 15 '22 00:11

matrix