简体   繁体   中英

Repackaging .jar-s in Android .aar library

Source Code

Library

Project which uses Library

Problem Description

I'm writing Android Library (.aar) in that Library I'm using .jar libraries.

在此输入图像描述

In order to avoid dependency duplication I'm using ShadowJar plugin for repackaging, as shown below:

task shadowJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
    System.out.println("Relocating packages...")
    relocate 'retrofit'               , 'com.codecraft.retrofit'
    relocate 'org.simpleframework.xml', 'com.codecraft.org.simpleframework.xml'
    relocate 'com.squareup.okhttp'    , 'com.codecraft.com.squareup.okhttp'
}

In the application which is using Android Library (.aar) I have dependency of shown below libraries, just for testing that I have no dependency duplication issues.

dependencies {
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
    compile 'com.squareup.retrofit:converter-simplexml:2.0.0-beta1'
}

But when I try to run application on Android I got following error

在此输入图像描述

Error:warning: Ignoring InnerClasses attribute for an anonymous inner class Error:(com.bea.xml.stream.util.CircularQueue$1) that doesn't come with an Error:associated EnclosingMethod attribute. This class was probably produced by a Error:compiler that did not target the modern .class file format. The recommended Error:solution is to recompile the class from source, using an up-to-date compiler Error:and without specifying any "-target" type options. The consequence of ignoring Error:this warning is that reflective operations on this class will incorrectly Error:indicate that it is not an inner class. Error:trouble processing "javax/xml/XMLConstants.class": Error:Ill-advised or mistaken usage of a core class (java.* or javax.*) Error:when not building a core library. Error:This is often due to inadvertently including a core library file Error:in your application's project, when using an IDE (such as Error:Eclipse). If you are sure you're not intentionally defining a Error:core class, then this is the most likely explanation of what's Error:going on. Error:However, you might actually be trying to define a class in a core Error:namespace, the source of which you may have taken, for example, Error:from a non-Android virtual machine project. This will most Error:assuredly not work. At a minimum, it jeopardizes the Error:compatibility of your app with future versions of the platform. Error:It is also often of questionable legality. Error:If you really intend to build a core library -- which is only Error:appropriate as part of creating a full virtual machine Error:distribution, as opposed to compiling an application -- then use Error:the "--core-library" option to suppress this error message. Error:If you go ahead and use "--core-library" but are in fact Error:building an application, then be forewarned that your application Error:will still fail to build or run, at some point. Please be Error:prepared for angry customers who find, for example, that your Error:application ceases to function once they upgrade their operating Error:system. You will be to blame for this problem. Error:If you are legitimately using some code that happens to be in a Error:core package, then the easiest safe alternative you have is to Error:repackage that code. That is, move the classes in question into Error:your own package namespace. This means that they will never be in Error:conflict with core system classes. JarJar is a tool that may help Error:you in this endeavor. If you find that you cannot do this, then Error:that is an indication that the path you are on will ultimately Error:lead to pain, suffering, grief, and lamentation. Error:1 error; aborting :app:transformClassesWithDexForDebug FAILED Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException

PLEASE NOTE that this issue didn't appear in the case if I add only retrofit dependency

dependencies {
        compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
    } 

What is the reason and how I can solve this error?

When you are including Jars in your lib or app - you may including files that have already been compiled using a different java version or with a different configuration than yours and this may cause problems like the one you are experiencing.

I guess the dependency you used refers to a library that does cause the problem that the jar creates.

If it's critical for you not to use dependencies - consider downloading the code for the lib you are using (assuming they are open source and there are no legal limitations) and compiling them with your app as additional modules in your project instead of adding the jar. If you do that - you should definitely consider what jmols wrote about the lib size and method count.

Good luck!

Exclude of some modules helped me with same error:

compile ('com.squareup.retrofit2:converter-simplexml:2.1.0'){
    exclude group: 'xpp3', module: 'xpp3'
    exclude group: 'stax', module: 'stax-api'
    exclude group: 'stax', module: 'stax'
}

source link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM