简体   繁体   English

在Android .aar库中重新打包.jar-s

[英]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. 我正在使用.jar库在该库中编写Android库(.aar)。

在此输入图像描述

In order to avoid dependency duplication I'm using ShadowJar plugin for repackaging, as shown below: 为了避免依赖重复,我使用ShadowJar插件进行重新打包,如下所示:

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. 在使用Android库(.aar)的应用程序中,我具有下面显示的库的依赖性,仅用于测试我没有依赖性重复问题。

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 但是当我尝试在Android上运行应用程序时,我遇到了以下错误

在此输入图像描述

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. 错误:警告:忽略匿名内部类的InnerClasses属性错误:(com.bea.xml.stream.util.CircularQueue $ 1)没有出现Error:关联的EnclosingMethod属性。 This class was probably produced by a Error:compiler that did not target the modern .class file format. 这个类可能是由一个没有以现代.class文件格式为目标的Error:编译器生成的。 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. 推荐的错误:解决方案是使用最新的编译器错误从源代码重新编译类,而不指定任何“-target”类型选项。 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. 错误:处理“javax / xml / XMLConstants.class”时出错:错误:错误建议或错误使用核心类(java。*或javax。*)错误:未构建核心库时。 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). 错误:这通常是由于在使用IDE(例如Error:Eclipse)时无意中在应用程序的项目中包含了一个核心库文件Error :. 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:core class,那么这就是错误的最可能的解释:继续。 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. 错误:但是,您实际上可能正在尝试在核心Error:namespace中定义一个类,您可能已经采用了该类,例如,Error:来自非Android虚拟机项目。 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. 错误:如果您真的打算构建一个核心库 - 这只是错误:适合作为创建完整虚拟机的一部分错误:分发,而不是编译应用程序 - 然后使用错误:“ - core-library “选项可以禁止显示此错误消息。 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. 错误:如果您继续使用“--core-library”但实际上是错误:构建应用程序,那么请预先警告您的应用程序错误:在某些时候仍然无法构建或运行。 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. 错误:如果您合法地使用恰好位于Error:core包中的某些代码,那么最简单的安全替代方法是Error:重新打包该代码。 That is, move the classes in question into Error:your own package namespace. 也就是说,将有问题的类移动到Error:您自己的包命名空间。 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. JarJar是一个可以帮助错误的工具:你在这方面的努力。 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; 错误:1错误; aborting :app:transformClassesWithDexForDebug FAILED Error:Execution failed for task ':app:transformClassesWithDexForDebug'. 中止:app:transformClassesWithDexForDebug FAILED错误:任务':app:transformClassesWithDexForDebug'的执行失败。

com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException 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. 当您在lib或app中包含Jars时 - 您可能包含已使用不同的java版本或使用与您的配置不同的配置编译的文件,这可能会导致您遇到的问题。

I guess the dependency you used refers to a library that does cause the problem that the jar creates. 我猜你使用的依赖项是指一个导致jar创建的问题的库。

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. 如果您不使用依赖项至关重要 - 请考虑下载您正在使用的lib的代码(假设它们是开源的并且没有法律限制)并使用您的应用程序将它们编译为项目中的附加模块,而不是添加jar 。 If you do that - you should definitely consider what jmols wrote about the lib size and method count. 如果你这样做 - 你应该考虑jmols写的关于lib大小和方法计数的内容。

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 来源链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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