Search code examples
javaandroidandroid-studiojaraar

Creating Android AAR file from open source and add it to my own project


Hey all I am having a difficult time figuring out why the export of an .aar file does not seem to be working when I add it to my own project.

I am using the Expanable-tab project and it compiles the library it has to an .aap file via the build.gradle:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

ext.versionMajor = 1
ext.versionMinor = 2
ext.versionPatch = 1

android {
    compileSdkVersion 30

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 30
        versionCode versionMajor * 100000 + versionMinor * 100 + versionPatch * 1
        versionName "$versionMajor.$versionMinor.$versionPatch"
        setProperty("archivesBaseName", "expandable-fab")

        // Changes the name of generated AAR artifacts to be more descriptive
        libraryVariants.all { variant ->
            variant.outputs.all { output ->
                if (outputFileName.endsWith('.aar')) {
                    outputFileName = "${archivesBaseName}-${versionName}.aar"
                }
            }
        }
    }

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

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
}

apply from: 'generate-docs-and-update-website.gradle'
apply from: 'publish-artifacts-to-maven-repo.gradle'

When I rebuild the project I navigate to the expandable-fab-master\library\build\outputs\aar directory where expandable-fab-1.2.1.aar file has been produced.

enter image description here

I then copy the .aap file to my libs folder in my own project:

enter image description here

Then I proceed to add this to the build.gradle:

enter image description here

All of this compile when selecting the rebuild option - no errors.

However, when it gets to the point in my app that uses the expandable-fab code it crashes with this as the error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp, PID: 19960
    android.view.InflateException: Binary XML file line #36 in com.myapp:layout/_fragvideoplay1: Binary XML file line #36 in com.myapp:layout/_fragvideoplay1: Error inflating class com.nambimobile.widgets.efab.ExpandableFabLayout
    Caused by: android.view.InflateException: Binary XML file line #36 in com.myapp:layout/_fragvideoplay1: Error inflating class com.nambimobile.widgets.efab.ExpandableFabLayout
    Caused by: java.lang.reflect.InvocationTargetException

And the code its referencing (videoPlay.java:99):

enter image description here

If I comment out the implementation files('libs/expandable-fab-1.2.1.aar') and uncomment the original implementation 'com.nambimobile.widgets:expandable-fab:1.2.1' and then run the app it works as it should...

What am I missing here?


Solution

  • This is because of how you're using the aar file as a gradle dependency. Since you already have

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

    You don't have to do

    implementation("libs/library_name")
    

    instead do this:

    implementation(name: "library_name", ext: "aar")
    

    I hope this helps.