10

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.

DontPanic
  • 2,164
  • 5
  • 29
  • 56

2 Answers2

25

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

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
7

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.

Paamand
  • 626
  • 1
  • 6
  • 17
Bhavita Lalwani
  • 903
  • 1
  • 9
  • 19