Gradle config for ContentProvider contract

323 Views Asked by At

I'm trying to make separate artifact in my project to build jar file with ContentProvider contract classes. To build this jar I use next task:

task contractsJar(type: Jar) {
  from android.applicationVariants.release.javaCompile.destinationDir
  includes "contract classes filter"
  into "$buildDir/libs"
}

contractsJar.dependsOn 'compileReleaseJava'

but this task build jar with base path in the system root (for Mac OS X it is /Users/myuser...). So what should I do to make "right" jar with base path in the project root?

1

There are 1 best solutions below

0
On

There were two problems:

  1. android.applicationVariants.release doesn't exist at the very beginning, so we should use DomainObjectCollection.all()
  2. into works in some weird way - if we use it then jar will have system root as base folder

So the result config:

task contractsJar(type: Jar, dependsOn: 'compileReleaseJava')

android.applicationVariants.all() { variant ->
  if (variant.name == "release") {
    contractsJar {
      from variant.javaCompile.destinationDir
      include "filter pattern (ANT style)"
      baseName="contracts"
    }
  }
}