sathish
sathish

Reputation: 1395

how to add libs dependency from project to module in android studio Gradle

I have created an android project its name is a Sample app I also created a library module inside the Sample app project. The sample app has a dependency on other libraries so I have added required jar files inside the libs folder of the Sample app. the same jar files are required inside the module as well.

how can I add the dependency for the module to refer to the sample app's libs folder? I have tried this below but every time giving a Duplicate copy file exception.

repositories {
    flatDir { dirs '../libs' }
}

compile fileTree(dir: '../libs', include: '*.jar')

Upvotes: 4

Views: 4924

Answers (5)

Foroogh Varmazyar
Foroogh Varmazyar

Reputation: 1565

if you are using kotlin DSL add this to build.gradle.kts file:

dependencies {
    ....
    implementation(project(":your-library-module"))
}

Upvotes: 0

Jon Bryant
Jon Bryant

Reputation: 225

If you want a library (*.jar / *.aar) to be visible to the entire project, then you will need to make it's location visible to all projects.

Project's gradle file:

allprojects {
    repositories {
        jcenter()
        flatDir {
            // This is where the *.jar or *.aar should be located
            dirs "$rootProject.projectDir/libs"
        }
    }
}

Example

With the following directory structure:

SampleApp/
|--- app/
|--- libs/
     \--- myLibrary.aar
|--- myModule/
|--- build.gradle (<-- Project's gradle file)
\--- settings.gradle

And the following dependencies graph:

compile - Classpath for compiling the main sources.
|--- project :myModule
    \--- :myLibrary:
\--- :myLibrary:

then

SampleApp's build.gradle:

dependencies {
    compile project (":myModule")
    compile (name: 'myLibrary', ext: 'aar')
    // ...
}

myModule's build.gradle:

dependencies {
    compile (name: 'myLibrary', ext: 'aar')
    // ...
}

Upvotes: 1

Jay Rathod
Jay Rathod

Reputation: 11245

Add this to your app's build.gradle:

dependencies {
    ....
    compile project(':module-name')
}

Upvotes: 1

Jaimin Modi
Jaimin Modi

Reputation: 1667

In order to add Module dependency in android studio, follow below instruction :

You should put your library modules inside the Application Project. In order to specify a module dependency, simply:

Right click on Application->Open Module Settings
Click on the '+' icon
Select the root directory for your library module you'd like to add.
Follow the prompts

Then, this module will show up in your project. Then, you need to add it to Application as a library dependency. Once again, in your Module Settings:

Select your Application module
Select the Dependencies tab on the right
Click the '+' icon on the bottom
Select Module Dependency
Select your desired library module

Upvotes: 0

Dilip
Dilip

Reputation: 2734

In app level build.gradel

 dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:support-v13:22.2.1'
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
    compile 'com.squareup.picasso:picasso:2.5.2'

    compile files('lib/universal-image-loader-1.9.3.jar')

    }

Upvotes: 0

Related Questions