java - 重新打包 .jar 文件

标签 java android ant jar jarjar

我需要将 JRE7 库中的一些 jar 添加到我的 Android 项目中。但例如 rt.jar 与 Adroid 2.2 SDK 中的 android.jar 冲突,因此我收到此错误:

Ill-advised or mistaken usage of a core class (java.* or javax.*) when not building a core library.

This is often due to inadvertently including a core library file in your application's project, when using an IDE (such as Eclipse). If you are sure you're not intentionally defining a core class, then this is the most likely explanation of what's going on.

However, you might actually be trying to define a class in a core namespace, the source of which you may have taken, for example, from a non-Android virtual machine project. This will most assuredly not work. At a minimum, it jeopardizes the compatibility of your app with future versions of the platform. It is also often of questionable legality.

If you really intend to build a core library -- which is only appropriate as part of creating a full virtual machine distribution, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact building an application, then be forewarned that your application will still fail to build or run, at some point. Please be prepared for angry customers who find, for example, that your application ceases to function once they upgrade their operating system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code. That is, move the classes in question into your own package namespace. This means that they will never be in conflict with core system classes. JarJar is a tool that may help you in this endeavor. If you find that you cannot do this, then that is an indication that the path you are on will ultimately lead to pain, suffering, grief, and lamentation.

我知道有几个关于它的帖子,比如JarJar , OneJarFatJar可能对我有好处。但我不知道如何使它们中的任何一个工作并且文档并没有真正说明(对我来说)。我猜他们使用 Ant 命令,但我一直使用 Eclipse 内置构建器,现在我不知道如何使用 Ant 或上面提到的任何命令。

所以我的问题是:如何重新打包这个 rt.jar 以便我可以在我的 Android 项目中编译它?

谢谢!

编辑:

好吧,所以我想要实现的是创建一个.jar,它可以在开发Android应用程序时使用(简化一些功能,并不重要)。但我还希望能够将相同的 .jar 添加到标准 Java 项目中,以便也使用其中的一些功能。它看起来像这样: 编写应用程序的人将这个 .jar 添加到他的 Java 项目中 -> 它使他能够生成某些文件(需要互联网来完成) -> 然后将这些生成的文件添加到 Android 项目中 -> 稍后,当有人使用这个 Android 时应用程序中,这些文件无需使用互联网(离线)即可提供某些功能。

最佳答案

即使有可能,在任何项目中这样做都是不明智的。您将使自己面临大量的类不兼容和加载问题。但无论如何,这都不重要,因为核心 Java 库在您的文件被触及之前就已加载,使得任何此类覆盖它们的尝试都毫无意义。

更不用说 Android 使用自己的 JVM 实现,该实现与 JDK 6 不完全兼容(忘记 JDK 7)。另请注意,将核心 Java 库与代码打包在一起可能会侵犯版权,并且可能会更改您的许可选项 (IANAL)。

您需要找到另一种方法来解决您遇到的任何问题(您在问题中没有提及)。

关于java - 重新打包 .jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10315306/

相关文章:

java - 在android应用程序中使用dll库

ant - 将 ant 脚本转换为 gradle 脚本

java - Ant 将类提取到 Jar 中

java - Visual Studio Code - 打字时滞后

android - 将文件保存到android中的内部存储器?

Android Drawable 动画和缩放

java - Ant 构建结果在 Eclipse 代码上为 "NullPointerException"

java - antlr4/java : pretty print parse tree to stdout

java - 在Linux中,如何用外部jar文件执行Java jar文件?

java - Hibernate : Failed to create sessionFactory object. org.hibernate.InvalidMappingException