Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'])
like image 272
Alexander Voloshyn Avatar asked Jun 26 '15 20:06

Alexander Voloshyn


3 Answers

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'
    ])
}
like image 144
mixel Avatar answered Oct 23 '22 04:10

mixel


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 *.

like image 21
Gabriele Mariotti Avatar answered Oct 23 '22 05:10

Gabriele Mariotti


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 ))
}
like image 3
Matt House Avatar answered Oct 23 '22 03:10

Matt House