To take a simple example String.isEmpty() was made available in API level 9.
If I wanted to use this method in my application that targets API levels less than 9, how would I go about this?
Is it as simple as locating the source code of this method and importing the class/method? – Perhaps renaming it with my own package name for reference?
Can I compile a separate jar of the new API classes that I wish to use?
Is this what the support library does for selected APIs?
Have I completely misunderstood some fundamental basics of the Android Framework? If I have, please be nice and perhaps offer me some links to reading material!
This post asks a similar question, but the answer doesn’t resolve all of the above for me.
I thank you in advance.
For something as trivial as this, it is as simple as copying and pasting the implementation into some static method of your own somewhere. You have no means of changing the implementation of
Stringon millions of devices, so you have no means of puttingisEmpty()onStringon API Level 8 or lower. But, you could create aStringCompatclass with a staticisEmpty()method that borrows from the implementation you see in the source code.That would depend upon what the “new API classes” are.
Sort of. The Android Support package, in some cases, has other modifications to allow the code to work on older devices. For example, in the native API Level 11 implementation of fragments, fragments work with any
Activity; in the Android Support package, fragments are tied toFragmentActivity.Other pieces of code in the Android Support package might be direct clones of their native counterparts, except for package and/or class name changes.