How to add .aar dependency in library module?

后端 未结 5 728
北海茫月
北海茫月 2020-12-14 00:54

I am having one .aar file of one library module.
I want to use it as a library or dependency in my other project\'s library module.
How do I do it?

相关标签:
5条回答
  • 2020-12-14 01:11
    1. File -> New Module -> Import .JAR/.AAR
    2. import the .AAR file.
    3. in library module build.gradle add the dependency dependencies{compile project(':Name-Of-Your-Module-aar')}

    http://tools.android.com/tech-docs/new-build-system/tips#TOC-Handling-transitive-dependencies-for-local-artifacts-jars-and-aar-

    0 讨论(0)
  • 2020-12-14 01:19

    In all modules (library or application) where you need the aar file you have to add in your build.gradle the repository:

    repositories {
        flatDir {
            dirs 'libs'
        }
    }
    

    and add the dependency:

    dependencies {
       compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
     }
    

    You can use the top-level file to add the repositories, but you can't add the dependencies in the top-level file.
    Pay attention to the relative path of the libs folder that you are using in the module.

    0 讨论(0)
  • 2020-12-14 01:19

    Sorry process involves 10 steps but these are super simple and easy.

    1- New->Module

    2- Select import .jar/aar package

    3- Select your .aar file from location

    4- We can see module is added but not configured with app module yet.

    5- Go to Project Structure

    6- Current project structure is like this one

    7- Go to app module and press '+' icon

    8- Select 3rd option Module dependency

    9- Select newly added .arr module

    10- you can see the new module attached with app module. Now click apply.

    you can see we are good to go.

    0 讨论(0)
  • 2020-12-14 01:24

    I did it a bit differently than others have posted here...

    My main goal was to create a library module that contained all the jar's and aar's that I would need. My main project would then depend on this library module - I only wanted a single line in the main project's build.gradle to link this dependency.

    1. Add a new library module to the project: File -> New -> New Module -> Android Library

    2. within the library module, open the build.gradle and add:

    // so that the library project can locate the aar in /libs
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation fileTree(include: ['*.aar'], dir: 'libs')
    }
    

    now you can pop all the jar's and aar's into the /libs folder of your library module. Bonus: whatever the library name is doesn't matter, it's automatically discovered

    0 讨论(0)
  • 2020-12-14 01:29

    Follow this setting and you will able to add .aar dependency to library module

    build.gradle (Project: ....)

    allprojects {
        repositories {
            jcenter()
            mavenCentral()
            flatDir {
                dirs 'libs'
                dirs project(':library_module').file('libs')
            }
        }
    }
    

    build.gradle (Module: app)

    dependencies {
        ...
        compile project(':library_module')
    }
    

    build.gradle (Module: library_module)

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

    settings.gradle (Project Settings)

    include ':app', ':library_module'
    
    0 讨论(0)
提交回复
热议问题