4

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')
MRazaImtiaz
  • 1,964
  • 1
  • 13
  • 23
sathish
  • 1,375
  • 6
  • 17
  • 31

5 Answers5

1

Add this to your app's build.gradle:

dependencies {
    ....
    compile project(':module-name')
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • hi jay, i have added compile project(':module-name') to my app build.gradle. But my issue is i want to use the libraries of the app into my module. means, my app and module wanted to use same dependencies. – sathish May 03 '16 at 13:14
  • @sathish Check this solution will help you http://stackoverflow.com/questions/21170395/how-to-include-a-library-module-dependency-in-an-android-studio-project – Jay Rathod May 03 '16 at 13:20
1

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')
    // ...
}
ehartwell
  • 1,667
  • 19
  • 18
Jon Bryant
  • 205
  • 2
  • 12
0

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
Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • Hi Jaimin, thanks for your replay. But i am unable to find the checkbox before module name. i would like to add common libraries to both application and its module. – sathish May 03 '16 at 12:58
  • then, google out it, Hope GoogleBaba will helps you lot..! – Jaimin Modi May 03 '16 at 13:01
0

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')

    }
Dilip
  • 2,622
  • 1
  • 20
  • 27
0

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

dependencies {
    ....
    implementation(project(":your-library-module"))
}
Foroogh Varmazyar
  • 1,057
  • 1
  • 14
  • 18