0

For a project, I have this type of structure :

--- app => my Android application | \- lib => my Android library

The application use methods from the lib, but I want a build without embedding classes from the lib (since they are also available in a custom AOSP build), which could be done with provided/compileOnly directive for dependencies. The problem is that this directive doesn't work with aar librairies generated by the Android plugin, only with jars.

I found a way to build also jar files by using the custom makeJar task found here : Build library jar with Gradle

Then in the build.gradle for the application, I can add a dependency like this :

dependencies { compileOnly fileTree(dir: '../lib/build/outputs/jar', include: ['*.jar']) ... }

This works fine, but only if the jar has already been generated by a previous build, which means I have to launch ./gradlew build two time to make it work.

I suppose the problem is that unlike regular dependencies (ie compileOnly project(':lib')), the fileTree dependency is evaluated without requesting a build of the sub project and on the first build, no jar file is added to the classpath.

I tried things like preBuild.dependsOn(':lib:makeJar') but without success because it's executed after evaluation.

I can't switch lib to Java plugin because it's Android specific code and I need to build aar for other projects, so is there a better way to use the compileOnly directive with this module ? Or maybe a way to force the lib to be built before the app build.gradle is evaluated ?

L.C
  • 21
  • 1
  • 3

1 Answers1

-3

add this on your build.gradle(app)

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

or directly compile your jar file as following

compile files('libs/your_filename_in_libs_folder.jar')
  • The libs folder must be in app folder. If it doesn't exist their create it – Leonce BALI Dec 22 '17 at 17:47
  • Not really what I want, I keep the lib as a module to recompile it everytime. This way I have to copy the jar each time it changes. – L.C Dec 22 '17 at 21:46
  • if you keep it as a module, do this compile project(path: ':lib') – Leonce BALI Dec 24 '17 at 11:00
  • 1
    Using "compile" (or "implement" since Android Gradle Plugin 3) will embed classes from the lib into the apk, which is what I'm trying to avoid. – L.C Dec 25 '17 at 14:19
  • instead of **compile**, you can use **implementation** – Leonce BALI Dec 26 '17 at 10:27
  • 1
    **implementation** is just the new syntax since Android Gradle Plugin 3, it does exactly the same as **compile** – L.C Dec 26 '17 at 13:20