pka
pka

Reputation: 351

Android gradle java and android sub-projects

I have a multi-project build, where some modules are "pure java" modules, the jar files output from those java modules are released. These java modules depend on jar files generated by android libraries.

Is there a way to specify the android module as the dependency of the java module?

The project structure is as follows:

Root
  |
  JavaModule
      |
     build.gradle
  |
  AndroidModule
      |
     build.gradle

The JavaModule's build.gradle is:

apply plugin:'java'
dependencies{
    compile project(:AndroidModule)
}

The AndroidModule's build.gradle is:

apply plugin:'android-library'

Adding the dependency as above, doesn't work, the java module is not able to find the symbols in the AndroidModule.

I guess the issues is the AndroidModule generates an aar file, which the java plugin doesn't understand.

As part of the build, the AndroidModule generates classes.jar. Is there a way to specify a dependency on this classes.jar file?

Upvotes: 2

Views: 498

Answers (1)

Jeffrey Mixon
Jeffrey Mixon

Reputation: 13666

You've correctly identified you need to reference the jar output from the Android library projects. There are two(ish) ways to do this.

manual approach

Simply copy the jar files into the parent project and reference them like you would from an Android project:

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

i.e., create a libs folder in the project and simply drop the jar in there. Depending on the volatility of these libraries, this may be the easiest as well as the simplest approach.

maven approach

If your libraries are constantly changing, it will quickly be a pain to manually copy the jars around. Of course you could use some scripts to automate it, but the better solution imo is to deploy your jars to a local Maven repository. This can be accomplished with the standard maven-publish Gradle plugin. You would then reference the dependencies like you would from Maven central, i.e.:

dependencies {
    compile 'com.your.module:1.0.+@jar'
}

In this setup, you build each library and publish the jar to a local maven repo on your own machine (for example), and then any dependent projects are set up to download the binary from there.

Upvotes: 1

Related Questions