Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add and Use an AAR in AndroidStudio Project

I am trying to use a custom aar in my android project. I found dozens of examples in StackOverflow and the Web. Many failed at build, none worked. The clearest was at http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/
That came closest to working.

Here's what I did

  1. Successfully created a very simple AAR (Ref.aar) from Ref.java

    // Ref.java  
    package com.ramrod.Ref;  
    public class Ref {
        // Square an integer
        public static int
        square(int val) {
            return (val * val);
        }
    }  
    
  2. Created a test project (RefTest)

  3. Created folder 'libs' under RefTest/app
  4. Added Ref.aar to libs
  5. File->New->New Module->Import .JAR/.AAR Package.
  6. Selected Ref.jar as filename->Finish (appeared successful).
  7. Modified build.gradle

    // build.gradle (Module: app)  
    
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 27  
        buildToolsVersion "27.0.3"
        defaultConfig {
            applicationId "com.ramrod.RefTest"
            minSdkVersion 11
            targetSdkVersion 15
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
        repositories {
            flatDir {
                dirs 'libs'
            }
        }
        dependencies {
            compile( name:'Ref', ext:'aar' )
       }
    }  
    
  8. Sync build.gradle (all)
  9. Added reference to Ref.aar method (square) to onCreate in RefTest main activity.

    int sq = Ref.square( 2 );
    
  10. Build->Clean then Build->Rebuild.
    This produced error: cannot find symbol variable Ref

I'm sure I'm doing something naive or just plain dumb, but I can't see it.
Any help appreciated.

like image 465
DontPanic Avatar asked Jul 02 '18 17:07

DontPanic


People also ask

How do I open AAR files on Android?

In android studio, open the Project Files view. Find the . aar file and double click, choose "arhcive" from the 'open with' list that pops up. This will open a window in android studio with all the files, including the classes, manifest, etc.

What is AAR file in Android?

An AAR file contains a software library used for developing Android apps. It is structurally similar to an . APK file (Android Package), but it allows a developer to store a reusable component that can be used across multiple different apps.


2 Answers

You should:

1) create aar library and just put it in libs directory ( without "File->New->New Module->Import .JAR/.AAR Package" )

see picture

2) add to build.gradle (Module: app)

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

}

After that you can use Ref.square(int);

Your apk will contain after build:

enter image description here

like image 146
Dima Kozhevin Avatar answered Oct 16 '22 17:10

Dima Kozhevin


When you import an AAR from built in helper tools using Import aar/jar option, studio creates a module with this aar.

So at this state you can see something similar to the state mentioned below. When display panel is Android,

enter image description here

Change your panel mode to Project and open your testaar , you can actually see a build.gradle file for your module and the corresponding aar.

enter image description here

That is why your statement

compile( name:'Ref', ext:'aar' )

was not working.

To add this aar to your project(after using import aar/jar), what you can do is to first add the module to the settings.gradle (Project settings file)

include ':app', ':testaar'

then directly add to your application level build.gradle file

implementation project(':testaar')

2)Another way is to

Right-click on your Application Module ->Select Open Module Settings -> Select the Module -> Go to Dependencies tab

P.S you can also open this window from Build->Edit Libraries and Dependencies

You will come across a window as below

enter image description here

Click on the small + icon, then Module option and finally add the required module(testaar)

enter image description here

Sync your code and voila it will start working now.

like image 7
Bhavita Lalwani Avatar answered Oct 16 '22 18:10

Bhavita Lalwani