Repackaging the .jar file
Asked Answered
E

3

2

I need to add some jars from JRE7 library to my Android project. But for example rt.jar is in conflict with android.jar from Adroid 2.2 SDK, so I get this error:

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.

I know there have been several threads about it and things like JarJar, OneJar or FatJar might be good for me. But I don't know how to make any of them work and documentation doesn't really make it clear (for me). I guess they use Ant commands, but I have always used Eclipse built-in builder and now I have no idea how to use neither Ant nor any of mentioned above.

So my question is: how can I repack this rt.jar so I could compile it in my Android project?

Thank you!

EDIT:

Ok, so what I want to achieve is to create a .jar, which can be used during developing Android application (simplifies some functionalities, doesn't really matter). But I would also like to be able to add the very same .jar to standard Java project in order to use some functions there as well. It would look like this: Whoever writes an application adds this .jar to his Java project -> it enables him to generate certain files (internet is needed to do it) -> these generated files are then added to Android project -> later on, when somebody uses this Android app, these files provide certain functionalities without using internet (off-line).

East answered 25/4, 2012 at 12:10 Comment(2)
You really should not include rt.jar from the JRE into an android project. What functionality are your after that you cannot achieve with core Android SDK?Steeple
related question: https://mcmap.net/q/622907/-android-core-library-errorHumdrum
E
3

It would be ill-advised to do this in any project at all, even if it were possible. You would be opening yourself to a wealth of class incompatibility and loading problems. But in any case it doesn't even matter because the core Java libraries are loaded way before your archives are even touched, making any such attempt at overriding them moot.

Not to even talk about the fact that Android is using its own JVM implementation which is not fully compatible with JDK 6 (forget JDK 7). Also note that it may be a copyright violation to package the core Java libraries with your code and could change your licensing options (IANAL).

You need to find another way to resolve whatever issue you are having (which you failed to mention in your question).

Essonite answered 25/4, 2012 at 12:25 Comment(4)
It also will be a copyright violation, unless you are using an open source JDK (e.g., OpenJDK) and are willing to license your code under its terms (e.g., the GPL).Johann
@Johann - another good point, Im going to add to my answer if you dont mind.Essonite
I updated the info you asked for. I hope it's clearer now. And about the copyrights, I am a student and this application will never be licensed or anything like this - I'm doing it just to learn something.East
@Johann and Perception: if you have any idea on how to achieve such a thing, please let me know.East
J
0

There are many JARs that work nicely on both Android and on classic Java. None involve having Android developers pirate rt.jar. Stick to java.* and javax.* classes that exist in both the Android SDK and in whatever level of Java you are supporting, and your JAR will work fine in both environments.

Johann answered 26/4, 2012 at 14:53 Comment(0)
H
0

You should ideally refrain from using such .jar files, but if you must, you can add them to build path. But this, at times results in a conflict, like the one that you are facing right now. What you need to do to resolve this kind of a conflict, is:

  1. add the jar in the build path.
  2. Check "referenced libraries". The jar file should appear under the same.
  3. once it features under referenced libraries, check the "android dependencies" virtual directory. If you get to see that you have an instance of the same jar file there as well, you should delete the "android dependencies" folder altogether. (Trust me, this does not affect your project in any way).
  4. having done that, you should be able to compile your code without any further conflicts.

Happy coding.. :)

Helbona answered 18/1, 2013 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.