How to manually include external aar package using new Gradle Android Build System

I’ve been experimenting with the new android build system and I’ve run into a small issue. I’ve compiled my own aar package of ActionBarSherlock which I’ve called ‘actionbarsherlock.aar’. What I’m trying to do is actually use this aar to build my final APK. If I include the whole ActionBarSherlock library as an android-library module to my main project using compile project (‘:actionbarsherlock’) I’m able to build successfully without any problems.

But my problem is that I want to provide that dependency as a aar file package MANUALLY just if I would a JAR then I can’t seem to figure out how to properly include it into my project. I’ve attempted to use the compile configuration but this doesn’t seem to work. I keep on getting cannot find symbol during compile which tells me that the classes.jar from aar package isn’t getting included in the classpath.

  • Click on notification to enter my app in android
  • Remove space between buttons
  • Passing JSON data in get request as request body
  • Android Emulator Error Message: “PANIC: Missing emulator engine program for 'x86' CPUS.”
  • Unable to resolve host “<insert URL here>” No address associated with hostname
  • HAXM configuration in android studio
  • Does anyone know of the syntax to manually include an aar package as a file?

    build.gradle

    buildscript {
    
     repositories {
         mavenCentral()
      }
      dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
      }
    }
    apply plugin: 'android'
    
    repositories {
       mavenCentral()
    }
    dependencies {
        compile files('libs/actionbarsherlock.aar')
    }
    
    android {
        compileSdkVersion 15
        buildToolsVersion "17.0"
    }
    

    EDIT: So the answer is that it’s not currently supported, here’s the issue if you want to track it.

    EDIT: Currently as this is still not supported directly the best alternative seems to be the proposed solution from @RanWakshlak

    EDIT: Also simpler by using the syntax proposed by @VipulShah

    Related posts:

    How do I start my app on startup?
    How to downsample images correctly?
    &ldquo;The signing configuration should be specified in Gradle build scripts&rdquo;&hell...
    KeyCode_Enter to next edittext
    Android: how to convert byte array to Bitmap?
    NullPointerException at android.widget.AbsListView.contentFits(AbsListView.java:722)
  • Android HorizontalScrollView with right layout_gravity working wrong
  • Android singletop singleinstance and singletask
  • Javadoc in Eclipse failing to recognize packages
  • How to know smallest width(sw) of android device?
  • Activity Recognition in Android is not working
  • How to get a PERMANENT application notifier in the status bar?
  • 12 Solutions collect form web for “How to manually include external aar package using new Gradle Android Build System”

    Please follow below steps to get it working ( I have tested it up to Android Studio 2.2)

    Lets say you have kept aar file in libs folder. ( assume file name is cards.aar )

    then in app build.gradle specify following and click sync project with Gradle files.
    Open Project level build.gradle and add flatDir{dirs 'libs'} like did below

    allprojects {
       repositories {
          jcenter()
          flatDir {
            dirs 'libs'
          }
       }
    }
    

    and now open app level build.grdle file and add .aar file

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

    If everything goes well you will see library entry is made in build -> exploded-aar

    Also note that if you are importing a .aar file from another project that has dependencies you’ll need to include these in your build.gradle, too.

    1. Right click on your project and choose “Open Module Settings”.

    Open module settings

    1. Click the “+” button in the top left to add a new module.

    Add new module

    1. Choose “Import .JAR or .AAR Package” and click the “Next” button.

    Import AAR

    1. Find the AAR file using the ellipsis button (“…”) beside the “File name” field.

    Find AAR file

    1. Keep the app’s module selected and click on the Dependencies pane to add the new module as a dependency.

    Dependencies pane

    1. Use the “+” button of the dependencies screen and choose “Module dependency”.

    Add module dependency

    1. Choose the module and click “OK”.

    Choose module

    You can reference an aar file from a repository.
    Maven is an option, but there is a simpler solution: put the aar file in your libs directory and add a directory repository.

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

    Then reference the library in the dependency section:

    dependencies {
      compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    }
    

    You can check out Min’an blog post for more info.

    The below approach works with latest Android studio (> v0.8.x):

    • Save the aar file under app module’s libs folder (eg: <project>/<app>/libs/myaar.aar)

    • Add the below to build.gradle of your “app” module folder (not your project root build.gradle). Note the name in compile line, it is myaar@aar not myaar.aar.

      dependencies {
          compile 'package.name.of.your.aar:myaar@aar'
      }
      
      repositories{
          flatDir{
              dirs 'libs'
          }
      }
      
    • Click Tools -> Android -> Sync Project with Gradle Files

    Currently referencing a local .aar file is not supported (as confirmed by Xavier Ducrochet)

    What you can do instead is set up a local Maven repository (much more simple than it sounds) and reference the .aar from there.

    I’ve written a blogpost detailing how to get it working here:

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

    I’ve just succeed!

    1. copy mylib-0.1.aar file under libs/ folder

    2. add these lines to bottom of build.gradle [should be app, not project]

      repositories {
         flatDir {
             dirs 'libs'
         }
      }
      dependencies {
          compile 'com.example.lib:mylib:0.1@aar'
      }
      
    3. So far so good. Here comes the most important point:

    Gradle needs to access network for dependencies unless offline mode enabled.

    So you have to be sure that you have enabled Offline work checkbox in Project Structures/Gradle

    — OR —

    configured proxy settings in order to access network.

    In order to configure proxy settings you have to modify gradle.properties file of project http and https separately as below:

    systemProp.http.proxyHost=proxy.example.com
    systemProp.http.proxyPort=8080
    systemProp.http.proxyUser=user
    systemProp.http.proxyPassword=pass
    systemProp.http.nonProxyHosts=localhost
    systemProp.http.auth.ntlm.domain=example <for NT auth>
    
    systemProp.https.proxyHost=proxy.example.com
    systemProp.https.proxyPort=8080
    systemProp.https.proxyUser=user
    systemProp.https.proxyPassword=pass
    systemProp.https.nonProxyHosts=localhost
    systemProp.https.auth.ntlm.domain=example <for NT auth>
    

    Hope this works..

    Unfortunately none of the solutions here worked for me (I get unresolved dependencies). What finally worked and is the easiest way IMHO is: Highlight the project name from Android Studio then File -> New Module -> Import JAR or AAR Package. Credit goes to the solution in this post

    You can add multiple aar dependencies with just few lines of code.

    Add local flatDir repository:

    repositories {
        flatDir {
            dirs 'libs'
        }
    } 
    

    Add every aar in libs directory to compile dependency configuration:

    fileTree(dir: 'libs', include: '**/*.aar')
            .each { File file ->
        dependencies.add("compile", [name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
    }
    

    I’ve also had this problem. This issue report: https://code.google.com/p/android/issues/detail?id=55863 seems to suggest that directly referencing the .AAR file is not supported.

    Perhaps the alternative for now is to define the actionbarsherlock library as a Gradle library under the parent directory of your project and reference accordingly.

    The syntax is defined here http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Referencing-a-Library

    In my case I have some depencies in my library and when I create an aar from it I failed, because of missed depencies, so my solution is to add all depencies from my lib with an arr file.

    So my project level build.gradle looks so:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.1.2'
        }
    }
    
    allprojects {
        repositories {
            mavenCentral()
            //add it to be able to add depency to aar-files from libs folder in build.gradle(yoursAppModule)
            flatDir {
                dirs 'libs'
            }
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    build.gradle(modile app) so:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
    
        defaultConfig {
            applicationId "com.example.sampleapp"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        //your project depencies
        ...
        //add lib via aar-depency
        compile(name: 'aarLibFileNameHere', ext: 'aar')
        //add all its internal depencies, as arr don't have it
        ...
    }
    

    and library build.gradle:

    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
    
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        //here goes library projects dependencies, which you must include
        //in yours build.gradle(modile app) too
        ...
    }
    

    I found this workaround in the Android issue tracker:
    https://code.google.com/p/android/issues/detail?id=55863#c21

    The trick (not a fix) is to isolating your .aar files into a subproject and adding your libs as artifacts:

    configurations.create("default")
    artifacts.add("default", file('somelib.jar'))
    artifacts.add("default", file('someaar.aar'))
    

    More info:
    Handling-transitive-dependencies-for-local-artifacts-jars-and-aar

    I tried all solution here but none is working, then I realise I made a mistake, I put the .aar in wrong folder, as you can see below, I thought I should put in root folder, so I created a libs folder there (1 in picture), but inside the app folder, there is already a libs, you should put in second libs, hope this help those who has same issue as mine:

    enter image description here

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.