Android, Gradle and compile-time only dependencies

Android plugin for Gradle does not support Java-style compile time only dependencies.

After spending a few hours on trying to build android app targeted for Amazon SDK (without using Amazon’s Android specific plugin but just their jar stubs for maps, ADM and Home widget), I finally found that the one way to support compile-time dependencies is following.

For application project

configurations {
    provided
}

dependencies {
    // ...
    provided fileTree(dir: "${project.rootDir}/path_to_libs_dir", include: '*.jar')
}

// Android's version of sourceSets.main.compileClasspath
android.applicationVariants.each { variant ->
    variant.javaCompile.classpath += configurations.provided
}

For the library project

configurations {
    provided
}

dependencies {
    // ...
    compile fileTree(dir: 'libs', include: '*.jar')
    provided fileTree(dir: "${project.rootDir}/patch_to_libs_dir", include: '*.jar')
}

android.libraryVariants.all { variant ->
    // Exclude the jar files from making its way into the final apk.
    // Irrespective of what the path_to_libs_dir is the final jar files end up in libs dir.
    variant.packageLibrary.exclude('libs/lib1.jar')
    variant.packageLibrary.exclude('libs/lib2.jar')
    // ...
}

References

  1. https://stackoverflow.com/questions/16613722/gradle-configurations-not-working-as-expected-in-new-android-build-system
  2. http://stackoverflow.com/a/24157721
-->