0

I need make changes in the android source code(android.jar).

I know I can make changes and compile a new android.jar file. I found some links on SO too regarding this.

But what I want to do is, for getting my project running on eclipse, rather than creating an android.jar, I want to add framework.jar, common.jar, core.jar, etc. individually, and remove the android.jar library from the build path in eclipse.

Is it even possible.

I tried it, but it is not working. I added the jar files as system user libraries. I even set the jar files as exported in build path configuration. My code gets compiled, but it gives a launching error for the emulator.

Can anyone give me any inputs regarding this error.

superuser
  • 625
  • 3
  • 7
  • 19

1 Answers1

3

Afaik the android.jar library is just a skeleton, a mock-up of the android.jar library present on a running Android system.

If you decompile it, I think that most methods in classes are implemented with just a throw UnsupportedOperationException and a return null if needed.

This has been done so that compiling/deploying your application is faster and the resulting .apk uses less space.

Thus changing source code in that .jar would be useless, as it would not get deployed to the device/emulator.

If you are interested in changing Android source code, you should really check the documentation for the Android source code and build the entire OS from source.

EDIT:

Re-read the part of your question about adding the .jar as a user library.

If you deploy android.jar as a user library (i.e. not shared), then your application will crash at runtime when trying to load classes multiple times (from different sources), as the classes are already present in the android.jar present on the device.

I.e. you wouldn't be able to load your own implementation of Application, because it would conflict with the Application classes that are initialized by Android when starting your application. Your code will not be run until the Application class is loaded when starting your application and, thus, you wouldn't be able to "override" that.

You can check this question for a bit more info (esp. the answer that has been awarded a bounty).

There's also some info on this question, to which I'd like to add here that, afaik, you can't unload a class while you're using it: if you'd like to use a custom class loader to load your system classes, you'd have to stop all threads of the application that are using instances of the class that you're trying to use, retain information about the existing instances of the class and the class' static data, unload the Android system class, load you custom one, re-create all the instances you retained information about (also restore static data) and then resume all the threads that you have stopped. Also, expect things to break in other places, too, as all the other classes/libraries that you will not override might rely on code that you're changing.

Community
  • 1
  • 1
lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
  • @ lucian.pantelimon What I understand from your answer is that I cannot add individual jar files or android.apk as a whole in my application, as that would create duplicacy with android os present on the emuolator (that is why the launching error). Unfortunately I do not know of any alternative. What is it that I can do to get my custom framework code running on emulator (as my application is dependent on them)? Afaik, there no support by google or otherwise to add custom android.jar (or framework.jar/common.jar/core.jar, etc) on emulator. Can somebody enlighten me if I am correct. – superuser Dec 02 '13 at 08:58
  • @girlDevelopIt There is no support to add a custom `android.jar`. If you want to change that library, you'd have to redistribute the whole OS (like phone manufacturers or custom ROM developers do). Is it really necessary for what you are developing to change components in `android.jar`? Maybe you can get away with just extending some of the Android core classes or using wrappers around them ... – lucian.pantelimon Dec 02 '13 at 09:12
  • @girlDevelopIt By extending core classes I'm also referring to copy-in g the classes you want to have modified under a different package. As an example, if you want a custom `LinearLayout` that requires you change a `final` or `private` method of `LinearLayout`, you can implement your own `LinearLayout` class by copying the code from the Android source code and changing the implementation to your need. This would not require redeployment of the OS to the user device. – lucian.pantelimon Dec 02 '13 at 09:14
  • @ lucian.pantelimon "you'd have to redistribute the whole OS (like phone manufacturers or custom ROM developers do)" - Actually I am working in production environment itself. So, I WILL need to be working on custom framework code. My app's code heavily interacts with the custom framework code. That is one of the reason my app's code is giving problems with google emulator. – superuser Dec 02 '13 at 09:31
  • @girlDevelopIt what do you mean by: `Actually I am working in production environment itself.`? Are you developing/working on a fork of Android? If that's so, I might have looked at your question from an entirely wrong POV... – lucian.pantelimon Dec 02 '13 at 09:37
  • @ lucian.pantelimon "There is no support to add a custom android.jar. If you want to change that library, you'd have to redistribute the whole OS" - So I actually have to work on a custom OS (which would be distributed my by employer). What then can I do to port my application to eclipse/emulator & make development simpler task (currently I only do a device build). Is there any method I am missing - maybe flash the entire custom ROM on the emulator. Does google/emulator support that. What I understand from your answer is that it does not. Please correct me if I am wrong. – superuser Dec 02 '13 at 09:52
  • @ lucian.pantelimon - it is not completely a fork of android. it is android, customized somewhat by my employer. – superuser Dec 02 '13 at 09:57
  • @girlDevelopIt I think that you can use a custom ROM in the emulator. I've found this (link at the end) and I hope if will be of use. Unfortunately I don't have the knowledge (and barely the experience as I've only developed applications for Android) to help with this kind of task. http://forum.xda-developers.com/showthread.php?t=1599005 – lucian.pantelimon Dec 02 '13 at 10:09