Alexander Voloshyn
Alexander Voloshyn

Reputation: 924

Android Studio include all aar files in a directory

I'm writing a build system for a framework which can be extended via plugins by users (developers). My problem is that I can't include all aar files with a mask. For example I can include all jar files like this:

compile fileTree(dir: 'libs', include: '*.jar')

But if I include all aar files like this:

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

I get an error:

Only Jar-type local dependencies are supported. Cannot handle: /path/to/file.aar

However it is possible to include each file separately like this:

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

As a workaround, during the build, I will need to list the files in 'libs' directory, insert all the libraries manually into my build.gradle and keep it in sync (or rewrite each time). it's a hackish way of doing it and thus I wonder if there is a better way of doing it. Maybe there is a gradle script which can do it automatically or some gradle beta which supports this:

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

Upvotes: 5

Views: 14625

Answers (3)

Matt House
Matt House

Reputation: 39

I know this answer is late to the game, but for others coming here, I have managed it without using the repository.flatDir.
Just add the fileTree() AFTER the dependencies section

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
fileTree(dir: 'libs', include: '*.aar')
    .each { File file ->
dependencies.add("compile", files( 'libs/'+file.name ))
}

Upvotes: 3

mixel
mixel

Reputation: 25876

First add flatDir repository:

repositories {
    flatDir {
        dirs 'libs'
    }
}

Then after dependencies section add:

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'
    ])
}

Upvotes: 17

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364868

You can put your aar files in a folder. You have to define this folder inside the repositories block. Of course you can use the libs folder

Something like:

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

Then you can use in your dependencies something like:

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

As I know there is no way to incluse all aars with a wildcard as *.

Upvotes: 3

Related Questions