Search code examples
androidgradleaar

How to tell if external dependency (.aar) has been added properly?


I'm adding my first local library for work (named "libprinter-release" for your reference). I imported it using "Add new module and then declared it as a dependency in my app directory:

enter image description here

None of the methods that should be in that library are being made available when I go to use it. Have I done this right? Is there something else I need to do to get the library working?


Solution

  • If you're using an aar file for your project, you should add the file to the libs directory under your module name. Then you need to update your app build.gradle by adding the libs flatDir and the library like this:

    android {
    
       ...
    
    }
    
    allprojects {
      repositories {
        flatDir {
          dirs 'libs'
        }
      }
    }
    
    dependencies {
      // change your-library to your library name.
      implementation(name: 'your-library', ext: 'aar')
    
      ...
      // other dependencies.
    
    }
    

    But if you're using a project module, you need to make sure if the module are using implementation or api with the dependencies. When you're using implementation with the dependencies, you need to add the same dependencies to your app module.

    Last, if your libprinter-release and libprinter-debug are the same except the debug version has the debug flag on, you need to tell the build.gradle about it by using the following in your dependencies block:

    dependencies {
      ...
    
      debugImplementation project(':libprinter-debug')
      releaseImplementation project(':libprinter-release')
    
      ...
    }