5

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'])
Alexander Voloshyn
  • 924
  • 2
  • 8
  • 21
  • You can find more information about advanced dependency management with Gradle in my question and answer here http://stackoverflow.com/q/31662781/746347 – mixel Jul 27 '15 at 20:42

3 Answers3

17

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'
    ])
}
mixel
  • 25,177
  • 13
  • 126
  • 165
  • How to add the transitive = true , to this script? – Kenenisa Bekele Apr 26 '16 at 14:24
  • @JavierVargas Dependency transitivity makes sense when there is some dependency resolver that builds dependency tree. But this script only adds `aar` library files and they do not have information (`pom.xml`) about dependencies so transitivity does not make sense in this case. You can read more in this answer http://stackoverflow.com/a/35311889/746347 – mixel Apr 26 '16 at 17:56
  • 1
    Getting `Only Jar-type local dependencies are supported. Cannot handle .aar` warnings, so it didn't work, I am on gradle 2.13. – Bob May 20 '16 at 18:02
  • @Bob I tested with Gradle 2.13. It works. That warning is given when you try to add aar dependencies like `compile fileTree(dir: 'libs/compile', include: '**/*.aar')`. Instead of that you should programmatically iterate over all aar files and add dependencies as `compile(name:'', ext: 'aar')`. And my answer does this. Just add code snippet from my answer after `dependencies` section. – mixel May 20 '16 at 18:26
3

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
3

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 ))
}
Matt House
  • 39
  • 3
  • Not work for me. Get error `Only Jar-type local dependencies are supported`. Which android gradle build plugin were you using? – jayatubi Sep 04 '18 at 03:37
  • This is the same solution from the accepted answer that was published only three years before... – Fran Marzoa Jul 26 '19 at 08:54