Avoid packaging jar dependencies into aar

时间秒杀一切 提交于 2019-12-23 06:12:47

问题


I'm developing an android library for work and we're using some jars that are not available in maven repositories. However, we are not legally allowed to package these jars into our library, the consumer must get those jars themselves in addition to our library.

My problem is that I can't seem to require the consumer of our library to provide these jars (I'm using a test app that includes the aar). I tried the solutions for this similar question to no avail.

I have tried setting them to provided instead of compile in my gradle file:

// Neither of these seem to fix the problem
compile files('libs/externalDep.jar')
provided files('libs/externalDep.jar')

I also tried excluding them in packaging options:

packagingOptions { exclude 'libs/externalDep.jar }`

I can exclude them by adding this to the android tag in my build.gradle:

android.libraryVariants.all { variant ->
variant.outputs.each { output ->
    def packageLib = output.getPackageLibrary()
    packageLib.exclude('libs/externalDep.jar')
}

They're not added to the aar but building the test app gives me:

com.android.build.api.transformTransformException:
  com.android.builder.packaging.DuplicateFileException:
    Duplicate files copied in APK VERSION.txt
File1: path/to/jar/in/test/app/project/externalDep.jar
File2: path/to/build/intermediates/exploded-aar/.../jars/classes.jar

To clarify, I'm not concerned at all with maven repo dependencies, those are working fine. I just want whoever uses the library to have to get and add those jars we're using too.
Surely there's a less convoluted way to specify this?


回答1:


The jar file must be moved up one level, from mymodule/libs/ to mymodule/ as per this answer.

Then in the build.gradle file, change:
compile file('libs/externalDep.jar')
to
provided file('externalDep.jar')

That's it.
It won't be copied into the aar and clients using the aar must provide the jar file themselves.
App projects using the aar can specify compile file('lib/externalDep') as normal.



来源:https://stackoverflow.com/questions/41696117/avoid-packaging-jar-dependencies-into-aar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!