Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to resolve local AAR built from local source module

I have a library module that I want to include as an AAR dependency into a sample app:

:my-library
:sample-app

So in sample/build.gradle, I do the following:

repositories {
    flatDir {
        dirs "../my-library/build/outputs/aar" 
    }
}

// I have different flavors that specify whether to use the source or binary (aar) dependency

flavorDimensions "SOURCE_OR_BINARY"
    productFlavors {
        source { }
        binary { }
}

dependencies {
    sourceImplementation project(':my-library')
    binaryImplementation(name: 'my-library-release', ext: 'aar') // <-- this line fails with error
}

tasks.whenTaskAdded { task ->
    def taskName = task.name.toLowerCase()
    if (taskName.toLowerCase().contains("binary")) {
        // Prepare libs as binaries
        task.dependsOn ('my-library:assembleRelease')
    }
}

This works fine with ./gradlew on the command line, but Android Studio reports a Failed to resolve: :my-library-release: during gradle sync. If I do a ./gradlew assemble on the command line, then sync Android Studio, the the AS Gradle sync succeeds.

The issue has to do with the timing of binaryImplementation(name: 'my-library-release', ext: 'aar'). When Gradle Sync is executed, the aar does not exist yet because it has yet to be built.

Is there a better way to do this that will avoid the Failed to resolve Android Studio Gradle sync error?

like image 764
ZakTaccardi Avatar asked Nov 16 '18 15:11

ZakTaccardi


2 Answers

You need to add this to your app main build.gradle.

    repositories {
          /...
          /...
         flatDir {
            dirs 'libs'
        }
    }

Lets say if you .aar file in the lib folder,then you could do something like this.

implementation files('libs/assembleRelease.aar')
like image 80
Ramesh Yankati Avatar answered Oct 07 '22 23:10

Ramesh Yankati


You can try import with this way,

File -> New Module -> Import .Jar/.AAR package

like image 40
jefry jacky Avatar answered Oct 07 '22 23:10

jefry jacky