16

I've custom annotation processor generating factory classes and META-INF/services/factory.interface.class resource.

Annotation processor is used in library project and all generated files are packaged correctly into AAR.

When I use annotation processor in application project with library added as a dependency only classes from libraries META-INF/services/factory.interface.class exist in APK/META-INF/services/factory.interface.class

After some investigation I realized that MergeJavaResourcesTransform in android-gradle-plugin-1.5.0 (and 2.0.0-alpha3) looks for resources for merging in all exploded-aars, jars, and intermediates/sourceFolderJavaResources

Is there any way to merge META-INF from intermediates/classes (it's where resource files from annotation processor get created) or make annotation processor create file in sourceFolderJavaResources?

Only workaround I've found so far is to add CopyTask in application's buildscript

android.applicationVariants.all { variant ->
  def variantName = variant.name
  def variantNameCapitalized = variantName.capitalize()
  def copyMetaInf = tasks.create "copyMetaInf$variantNameCapitalized", Copy
  copyMetaInf.from project.fileTree(javaCompile.destinationDir)
  copyMetaInf.include "META-INF/**"
  copyMetaInf.into "build/intermediates/sourceFolderJavaResources/$variantName"
  tasks.findByName("transformResourcesWithMergeJavaResFor$variantNameCapitalized").dependsOn copyMetaInf
}

But I do not wat to force compiler an library users do do anything more than adding dependencies.

pixel
  • 24,905
  • 36
  • 149
  • 251
skyman
  • 2,422
  • 17
  • 16

1 Answers1

1

It seems that the issue is related to META-INF folder. Here is the bug reported for Service Loader and META-INF

By the way, there is a confirmed solution:

  • right click on /src/main (where you have /java and /res folders),
  • select New > Folder > Java Resources Folder,
  • click Finish (do not change Folder Location),
  • right click on new /resources folder,
  • select New > Directory
  • enter "META-INF" (without quotes),
  • right click on /resources/META-INF folder,
  • select New > Directory
  • enter "services" (without quotes)
  • copy any file you need into /resources/META-INF/services

Also, you may want to check the following questions on SO:


Initial Answer:

You might want to try to configure your library source set's resources attribute (with your location of META-INF/services) which is responsible for:

The Java resources which are to be copied into the javaResources output directory.

Here is an example:

android {
    ...
    sourceSets {
        main {
            resources.srcDirs = ['/src/META_INF_generated']
        }
    }
    ...
}
Community
  • 1
  • 1
GregoryK
  • 3,011
  • 1
  • 27
  • 26
  • 1
    Library META-INF is correctly packaged to aar, there is a problem with MET-INF files in application. adding `resources.srcDirs 'intermediates/classes/debug/META-INF'` doesn't help – skyman Dec 30 '15 at 08:24
  • @skyman I updated with extra details. There are some workarounds for the issue. Hope it helps. – GregoryK Dec 30 '15 at 09:25
  • 1
    It does not work with build system. META-INF is generated in compile time both in library and application so coping apter compile but before packageing is not a solution – skyman Dec 30 '15 at 12:08
  • did you try to look at http://stackoverflow.com/questions/13254620/meta-inf-services-in-jar-with-gradle ? – GregoryK Dec 30 '15 at 14:21
  • 1
    META-INF in correctly generated in annotation procesor and placed in jar (and then aar) when building a library project. It is also correctly created (and merged with INE from library) in application project but not taken into account when creatin result jar. – skyman Dec 31 '15 at 05:59