For my application, I have a custom implementation of SharedPreferences.
In API level 9, the apply method was added to the SharedPreferences.Editor interface.
However, Android 1.6 throws a VerifyError if I add that method to my implementation and try to use it.
What is the best way to make this work in a backwards compatible way?
Edit:
I should clarify my conundrum. Before I switched to targeting Gingerbread, my implementation of SharedPreferences.Editor did not include the apply method. Upon switching the target, it ceased to compile due to that method being missing from my implementation.
Adding an implementation of the method fixed the compile problem, but added a new problem: Android 1.6 will not even load the class, throwing a VerifyError. Even if I catch the error, my custom implementation will never load on 1.6.
So far, my best idea has been to make my own interfaces identical to SharedPreferences and SharedPreferences.Editor, and implement those instead. Of course, that’s going to require sweeping changes throughout my code, so I’m trying to avoid it if possible.
If you were willing to drop Android 1.x support, you could just put this in an
if ()test, akin to:If you really need to support back to Android 1.6, though, you will need to isolate the new-API code in a class that only gets loaded on new-API devices, the so-called “conditional class loading” technique.
Here is a sample project demonstrating using this technique for your very problem.