Search code examples
javaandroidandroid-studiogradleaar

Add gradle dependencies(build.gradle) in .aar file Android


I am having an android library project which is using third party libraries. I have created it's .aar file and imported the .aar file in a different project. Classes and resources are imported but the gradle libraries are not imported. I don't want to mention those dependencies again in the project. Is there any way to import that project with .aar file or something else which include build.gradle or dependencies.


Solution

  • AAR file does not include external dependencies which are mention in app/build.gradle so instead of adding it directly we upload to local maven and integrate it.

    To upload it in your local maven you can add the following function in app/build.gradle

    apply plugin: 'maven'
    
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "file://localhost" + System.getenv("HOME") + "/.m2/repository")
                pom.version = '1.0-SNAPSHOT'
                pom.groupId = 'your.package'
                pom.artifactId = 'sdk-name'
            }
        }
    }
    

    You can change the pom version, groupId and artifactId according to how you want to use it.

    If this process is completed you can integrate the library.

    • In the app/build.gradle of the project in which you want to add the library specify the path.

      implementation 'your.package:sdk-name:1.0-SNAPSHOT'

    • In the project/build.gradle add the following

      allprojects{
          ...
          mavenLocal()
      }
      

      This will search for the library in the local machine.

    If still, you get some error try to invalidate caches and restart the android-studio from the File Menu.