2012-04-25 87 views
1

我需要從JRE7庫中添加一些JAR到我的Android項目。但例如rt.jarandroid.jar從的Adroid 2.2 SDK衝突,所以我得到這個錯誤:重新包裝.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.

我知道已經有關於它的幾個線程之類的東西JarJarOneJarFatJar可能是爲我好。但我不知道如何使它們中的任何一個都起作用,並且文檔沒有真正地說清楚(對我而言)。我猜他們使用Ant命令,但我一直使用Eclipse內置的構建器,現在我不知道如何使用Ant和上面提到的任何一種。

所以我的問題是:如何重新包裝這個rt.jar,以便我可以在我的Android項目中進行編譯?

謝謝!

編輯:

好了,我想達到的目標是創建一個.jar,它可以開發Android應用程序中使用(簡化一些功能,其實並不重要)。但我也希望能夠將標準Java項目添加到同一個.jar文件中,以便在其中使用一些函數。它看起來像這樣: 編寫應用程序的人將這個.jar添加到他的Java項目 - >它使他能夠生成某些文件(需要互聯網來完成) - >這些生成的文件然後被添加到Android項目 - >稍後在有人使用此Android應用程序時,這些文件提供某些功能而不使用互聯網(脫機)。

+2

你真的不應該包括來自JRE'rt.jar'到Android項目。你之後使用核心Android SDK無法實現的功能是什麼? – 2012-04-25 12:23:14

+0

相關問題:http://stackoverflow.com/questions/839435/android-core-library-error – 2013-03-10 09:11:07

回答

2

即使有可能,在任何項目中都這樣做是不明智的。你會打開自己的課堂不兼容和加載問題的財富。但是無論如何,這甚至都不重要,因爲核心Java庫在你的檔案被觸及之前就被加載了,所以任何這樣的嘗試都無可救藥。

甚至不會談論Android使用與JDK 6不兼容的自己的JVM實現(忘記JDK 7)的事實。另請注意,將核心Java庫與您的代碼打包在一起可能會導致版權侵犯,並可能會更改許可選項(IANAL)。

您需要找到另一種方法來解決您遇到的任何問題(您在問題中未提及)。

+2

這也將是版權侵犯,除非您使用的是開源JDK(例如'OpenJDK')並且願意根據其條款授權您的代碼(例如,GPL)。 – CommonsWare 2012-04-25 12:27:43

+0

@CommonsWare - 另一個好點,如果你不介意,我會添加到我的答案。 – Perception 2012-04-25 12:31:06

+0

我更新了您要求的信息。我希望現在更清楚。而關於版權,我是一名學生,這個應用程序永遠不會被許可或者像這樣的任何東西 - 我只是爲了學習而做的。 – alex 2012-04-25 14:52:26

0

有許多JAR在Android和傳統Java上都能很好地工作。沒有涉及Android開發者盜版rt.jar。堅持使用java.*javax.*這兩個類都存在於Android SDK和您所支持的任何級別的Java中,並且您的JAR在兩種環境中都能正常工作。

0

您應該理想地避免使用這樣的.jar文件,但是如果您必須,您可以將它們添加到構建路徑。但是,這有時會導致衝突,就像您現在正面臨的衝突一樣。你需要做什麼來解決這種衝突,是:

  1. 在構建路徑中添加jar。
  2. 檢查「引用庫」。該jar文件應該出現在相同的位置。
  3. 一旦它在被引用的庫下引用,請檢查「android依賴關係」虛擬目錄。如果你看到你有一個同樣的jar文件的實例,你應該刪除「android依賴項」文件夾。(相信我,這不會以任何方式影響您的項目)。
  4. 已經這樣做了,你應該能夠編譯你的代碼而不會有任何進一步的衝突。

編碼愉快.. :)