Adding local .aar files to my gradle build
Asked Answered
A

7

71

So I have created an Android library and successfully compiled it into a .aar file. I called this aar file: "projectx-sdk-1.0.0.aar". Now I want my new project to depend on this aar so what I have done is follow this post.

But the post confuses me since I do not get the desired result:

The package name of the aar is : com.projectx.photosdk and the module inside is called sdk

Here is my current project structure:

|-SuperAwesomeApp
|--.idea
|--gradle
|--App
|---aars
|----projectx-sdk-1.0.0.aar
|---build
|---jars
|---src
|---build.gradle

And here is my Gradle build file:

apply plugin: 'android'

buildscript {
    repositories {
        mavenCentral()
        flatDir {
            dirs 'aars'
        }
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:gridlayout-v7:19.0.1'
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'


    compile 'com.projectx.photosdk:sdk:1.0.0@aar'
//    compile files( 'aars/sdk-1.0.0.aar' ) // Does not work either
}

EDIT

The errors I am getting:

Failed to refresh Gradle project 'SuperAwesomeApp'
     Could not find com.projectx.photosdk:sdk:1.0.0.
     Required by:
     SuperAwesomeApp:App:unspecified
Addressee answered 19/2, 2014 at 14:3 Comment(2)
I too had a same problem to solve that i'm going on with ur solution actually i'm new to android studio i'ad a doubt what do you say about module inside is called sdk means i can't get u then how can i use this line compile 'com.projectx.photosdk:sdk:1.0.0@aar' can u plz help me to solve.We
You can refer this working solution https://mcmap.net/q/50076/-adding-local-aar-files-to-gradle-build-using-quot-flatdirs-quot-is-not-workingImprobity
S
72

You put your flatDir block in the wrong repostories block. The repositories block inside buildscript tells Gradle where to find the Android-Gradle plugin, but not the rest of the dependencies. You need to have another top-level repositories block like this:

repositories {
    mavenCentral()
    flatDir {
        dirs 'aars'
    }
}

I tested this and it works okay on my setup.

Sandra answered 19/2, 2014 at 19:53 Comment(2)
I'm impressed this worked! Any official documentation about that?Committeewoman
its not working for me , i have added flatDir{dirs 'aars'} still i am getting the error on compile line of aar.... :(Guttersnipe
U
43

With recent versions of Android Studio, tested with 1.3, to use local .AAR file and not one fetched from maven/jcenter repository, just go to File > New > New module and choose Import .JAR/.AAR Package.

What you will end up with is a new module in your project that contains very simple build.gradle file that looks more or less like this:

configurations.create("default")
artifacts.add("default", file('this-is-yours-package-in-aar-format.aar'))

Of course, other projects have to reference this new module with regular compile project directive. So in a project that uses this new module which is simple a local .aar file has this in it's build.gradle

[...]
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    [...]

    compile project(':name-of-module-created-via-new-module-option-described-above')
}
[...]
Unaccomplished answered 19/10, 2015 at 12:46 Comment(5)
Nice! If you are intimidated by editing the gradle (or can be bothered writing the module name out), double click on the new module you just added and it will open up the project structure window. From there, you can select your app module and click the + icon at the bottom and select whatever modules you please.Leah
have imported the module, and in my project it got created as well with only an .aar file of the same module and a simple gradle file as described in the answer. Has it been imported correctly? If yes, then how to start using the classes within it?Neva
This is right. Here is the doc.Flambeau
There is no " Import .JAR/.AAR Package" option.Sideman
this used to work up until last year, not sure when it happened, but it's broken now. The jar classes are recognized by the app but not by the IDE.Hepner
M
24

In Android Studio 3.1.3 with gradle 3.0.1.
Simply adding implementation fileTree(dir: 'libs', include: ['*.aar']) or implementation files('libs/app-release.aar') without any other flatdir works.

Molly answered 1/8, 2018 at 19:2 Comment(0)
B
5

These days (over 1 year after this question) with Android Studio >1.0, local dependency does work properly:

  • The android sdk looks for dependencies in a default local repo of: $ANDROID_HOME/extras/android/m2repository/
  • In a local library project you can publish the aar to this directory. Here's a snippet that can be added to your module's build.gradle file (ex: sdk/build.gradle)

    apply plugin: 'maven'
    
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
                    + "/extras/android/m2repository/")
                pom.version = '1.0-SNAPSHOT'
                pom.groupId = 'your.package'
                pom.artifactId = 'sdk-name'
            }
        }
    }
    
  • In your library project, run ./gradlew uploadArchives to publish the aar to that directory
  • In the application project you want to use the library in, add the dependency to your project/app/build.gradle. compile 'your.package:sdk-name:1.0-SNAPSHOT'

For local dependency, the next gradle build should find the previously deployed archive and that's it!


In my case, I use the above for local dev, but also have a Bamboo continuous integration server for the Library that publishes each build to a shared Nexus artifact repository. The full library code to deploy the artifact then becomes:

uploadArchives {
    repositories {
        mavenDeployer {
            if (System.getenv("BAMBOO_BUILDNUMBER") != null) {
                // Deploy to shared repository
                repository(url: "http://internal-nexus.url/path/") {
                    authentication(userName: "user", password: "****")
                }
                pom.version = System.getenv("BAMBOO_BUILDNUMBER")
            } else {
                // Deploy to local Android sdk m2repository
                repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
                        + "/extras/android/m2repository/")
                pom.version = '1.0-SNAPSHOT'
            }

            pom.groupId = 'your.package'
            pom.artifactId = 'sdk-name'
        }
    }
}

In order to tell applications to download from my internal Nexus repository, I added the internal Nexus maven repository just above jcenter() in both "repositories" blocks in the project/build.gradle

repositories {
    maven {
        url "http://internal-nexus.url/path/"
    }
    jcenter()
}

And application dependency then looks like compile 'your.package:sdk-name:45' When I update the 45 version to 46 is when my project will grab the new artifact from the Nexus server.

Banting answered 6/5, 2015 at 19:16 Comment(1)
I just tried Stan Kurdziel's suggestion and was able to resolve my question: <#34445899>Theorem
M
2

With the newest Gradle version there is now a slightly updated way of doing what Stan suggested (see maving publishing)

apply plugin: 'maven-publish'

publishing {
    publications {
        aar(MavenPublication) {
            groupId 'org.your-group-id'
            artifactId 'your-artifact-id'
            version 'x.x.x'

            // Tell maven to prepare the generated "*.aar" file for publishing
            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
        }
    }
    repositories {
        maven {
            url("file:" + System.getenv("HOME") + "/.m2/repository")
        }
    }
}
Mass answered 8/1, 2018 at 21:1 Comment(0)
N
0

It seems adding .aar files as local dependency is not yet supported(Planned to be supported in 0.5.0 Beta)

https://code.google.com/p/android/issues/detail?id=55863

But the way you are using your library in dependency will only work if your library is on central maven repository or in the local maven repository.

Refer this for How to use local maven repository to use .aar in module dependencies.

http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds

Natatory answered 19/2, 2014 at 19:13 Comment(1)
The entire point of the blog post he linked to is how to get around the fact that the Android-Gradle plugin doesn't support local AARs by creating a fake repository that it can look in when using Maven coordinate-style dependency includes.Sandra
M
0

This is for Kotlin DSL (build.gradle.kts) assuming you put the files in my-libs subdirectory relative to where the build file is located:

dependencies {
    implementation(
        fileTree("my-libs/") {
            // You can add as many include or exclude calls as you want
            include("my-first-library.aar")
            include("another-library.aar")
            // You can also include all files by using a pattern wildcard
            include("*.jar")
            exclude("the-bad-library.jar")
        }
    )
    // Other dependencies...
}

For more ways to do this, see Gradle documentations and this post and this post.

Midtown answered 20/4, 2022 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.