214

I'd like to create an aar file for my library in Android Studio, i would've gone with a jar option but my library has resources.

Any idea how to create an aar file from a library?

Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
  • This worked for me: https://developer.android.com/studio/projects/android-library –  Aug 17 '21 at 12:42

10 Answers10

281

If your library is set up as an Android library (i.e. it uses the apply plugin: 'com.android.library' statement in its build.gradle file), it will output an .aar when it's built. It will show up in the build/outputs/aar/ directory in your module's directory.

You can choose the "Android Library" type in File > New Module to create a new Android Library.

If you are still not seeing your aar file, select Build > Rebuild Project.

Firzen
  • 1,909
  • 9
  • 28
  • 42
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • I know this is an old topic, but is there any way to "version" this? While it's pretty straightforward to get the .aar created, is there any way to apply a version number to it? We want to create a distributable library at our company to put in Artifactory and would need versioning. – TheIcemanCometh Apr 09 '15 at 13:48
  • 3
    The versioning is really external to the AAR itself. You would typically set up a Maven pom file that has version info. – Scott Barta Apr 10 '15 at 02:13
  • Or you can create a gradle task that copies the AAR file from the outputs directory and adds a version to it. – MikeL Sep 11 '15 at 19:51
  • 1
    I have aar file in the libs folder of the library so it is not included in the build/outputs/aar/ directly – Ganesh Jogam Apr 18 '16 at 10:46
  • 4
    aar file appeared in app/build/outputs/aar/ and not in the /build/ folder on the project level! in case you don't find it... – peresisUser Dec 14 '16 at 16:05
  • 65
    Note that to generate my `aar`, I had to do a `Build > Rebuild Project` in Android Studio. Simply running, debugging, or Building APK did not generate the `aar` for me. – Adam Johns Apr 09 '18 at 18:53
  • 13
    @AdamJohns the quicker way being `./gradlew moduleName:assembleRelease` in the terminal tab – Sameer J Mar 12 '19 at 19:20
197

Retrieve exported .aar file from local builds

If you have a module defined as an android library project you'll get .aar files for all build flavors (debug and release by default) in the build/outputs/aar/ directory of that project.

your-library-project
    |- build
        |- outputs
            |- aar
                |- appframework-debug.aar
                 - appframework-release.aar

If these files don't exist start a build with

gradlew assemble

for macOS users

./gradlew assemble

Library project details

A library project has a build.gradle file containing apply plugin: com.android.library. For reference of this library packaged as an .aar file you'll have to define some properties like package and version.

Example build.gradle file for library (this example includes obfuscation in release):

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "0.1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Reference .aar file in your project

In your app project you can drop this .aar file in the libs folder and update the build.gradle file to reference this library using the below example:

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    flatDir {
        dirs 'libs' //this way we can find the .aar file in libs folder
    }
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.0"

    defaultConfig {

        minSdkVersion 14
        targetSdkVersion 20
        versionCode 4
        versionName "0.4.0"

        applicationId "yourdomain.yourpackage"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
        }
    }
}

dependencies {
    compile 'be.hcpl.android.appframework:appframework:0.1.0@aar'
}

Alternative options for referencing local dependency files in gradle can be found at: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project

Sharing dependencies using maven

If you need to share these .aar files within your organization check out maven. A nice write up on this topic can be found at: https://web.archive.org/web/20141002122437/http://blog.glassdiary.com/post/67134169807/how-to-share-android-archive-library-aar-across

About the .aar file format

An aar file is just a .zip with an alternative extension and specific content. For details check this link about the aar format.

AbhinayMe
  • 2,399
  • 1
  • 20
  • 21
hcpl
  • 17,382
  • 7
  • 72
  • 73
14

just like user hcpl said but if you want to not worry about the version of the library you can do this:

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

as its kind of annoying to have to update the version everytime. Also it makes the not worrying about the name space easier this way.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
12

To create AAR

while creating follow below steps.

File->New->New Module->Android Library and create.

To generate AAR

Go to gradle at top right pane in android studio follow below steps.

Gradle->Drop down library name -> tasks-> build-> assemble or assemble release

AAR will be generated in build/outputs/aar/

But if we want AAR to get generated in specific folder in project directory with name you want, modify your app level build.gradle like below

defaultConfig {
    minSdkVersion 26
    targetSdkVersion 28
    versionCode System.getenv("BUILD_NUMBER") as Integer ?: 1
    versionName "0.0.${versionCode}"


    libraryVariants.all { variant ->

        variant.outputs.all { output ->
            outputFileName = "/../../../../release/" + ("your_recommended_name.aar")
        }
    }
}

Now it will create folder with name "release" in project directory which will be having AAR.

Updated Answer

In Latest releases specific path is not supported.Please add below code in library's build.gradle and rebuild project.After Rebuilding "aar",change project structure from Android to Project->navigate to your library->build->outputs->aar

android {
defaultConfig {
    minSdkVersion ..
    targetSdkVersion ..
    versionCode ...
    versionName "1"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    consumerProguardFiles 'consumer-rules.pro'
}

libraryVariants.all { variant ->
    variant.outputs.all { output ->
    outputFileName = "${archivesBaseName}_${variant.name}_${defaultConfig.versionName}.aar"
}

}}

To import "aar" into project,check below link.

How to manually include external aar package using Gradle for Android

Tarun Anchala
  • 2,232
  • 16
  • 15
5

After following the first and second steps mentioned in the hcpl's answer in the same thread, we added , '*.aar'], dir: 'libs' in the our-android-app-project-based-on-gradle/app/build.gradle file as shown below:

...

dependencies {
       implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')

...

Our gradle version is com.android.tools.build:gradle:3.2.1

Dhananjay M
  • 1,851
  • 22
  • 18
2

btw @aar doesn't have transitive dependency. you need a parameter to turn it on: Transitive dependencies not resolved for aar library using gradle

Community
  • 1
  • 1
cn123h
  • 2,302
  • 1
  • 21
  • 16
1

Build ---> Build bundle/apk

.aar file will be generated in build/outputs/aar folder.

Aishwarya
  • 151
  • 1
  • 5
0

Finally got the solution here - https://stackoverflow.com/a/49663101/9640177

implementation files('libs/aar-file.aar')

Edit I had one more complication - I had set minifyEnabled true for the library module.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
0

[JAR vs AAR]

.jar is generated by apply plugin: 'java-library'

.aar is generated by apply plugin: 'com.android.library'

File -> New -> New Module... -> Android Library
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
0

If you have correctly set up for publishing, then you can just run this command to generate aar files.

 ./gradlew publishReleasePublicationToMavenLocal 

This will generate a aar file inside <module-dir>/build/output/aar directory.

Now you can use this library in other local projects also. Add this in the project gradle in which you want to use this aar module

implementation fileTree(dir: '<location-to-library>/build/outputs/aar/', include: ['*.aar', '*.jar'], exclude: [])
philoopher97
  • 772
  • 1
  • 6
  • 18