简体   繁体   中英

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

Add this to your app's build.gradle:

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

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

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

    }

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')
    // ...
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM