0

Can we override native methods in android? Apart from extending a class/interface and implementing its methods.
Can we use .so file to override native methods?
For eg. If there is methodA() in native code, shall we override that in .so file. so that everytime when we call methodA() from android application, it will call methodA() in our .so file?

LincyTonita
  • 327
  • 4
  • 14

2 Answers2

0

Can you come with an example of what you think it native methods? But afaik the answer is no. What would be the purpose of custom roms if you could customize android without updating the rom...

Edit: For the specific case you are mentioning. I'd extend SharedPreferences and override all the methodes that are needed.

Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • overriding method in custom rom applies for all applications in the device. I need that to be applied only for my application. Eg. Whenever I save a value in sharedpreference, I ll override the default method so that values are "encrypted" and saved.. If I could do this with a .so file, I ll use it for all the applications I develop – LincyTonita Oct 05 '12 at 11:17
  • @LincyTonita Simply override the class and methodes then. That is not native code. – Warpzit Oct 05 '12 at 11:33
  • @WarpzitThanks.. I mentioned sharedpreference just for example. We can do that by extending that class. But I want other way of doing it. (A external lib.. either native code by a .so file or a jar) – LincyTonita Oct 05 '12 at 13:33
0

No. Dalvik VM is not compatible enough for JNI DefineClass() call. Therefore, any override you want to load must be available in dex format to begin with.

Also, there is no way to "inject' native lib into an app without the app's knowledge.

But, there are lots of sophisticated things you can do through reflection - either from Java or from C. If this code gets a handle to your Activity, it could find the field of class SharedPreferences and replace it with a new field of class ExtendedSharedPreferences.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307