Pergunta

I know that the appcompat libraries provide additional functionality for apps running under earlier versions of Android than the app's target level. My question is whether I would need to include the appcompat library if I never call any of its methods if I detect an earlier version of Android on the device. In other words, do the appcompat libraries have stubs of later classes to prevent the Dalvik dynamic classloader from crashing, or are those not needed if the classes are not dynamically referenced?

// Will Android/Dalvik barf if this class is loaded on a pre-Honeycomb device?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {        
    ActionBar actionBar = getActionBar();
}
Foi útil?

Solução

My question is whether I would need to include the appcompat library if I never call any of its methods if I detect an earlier version of Android on the device.

No, then you wouldn't need it. The appcompat library is a bit more than just a back port of the ActionBar. It also includes a few little fixes for platforms newer than Gingerbread. Here is an example. As you can see there are implementations of the ActionBar for HC, ICS and GB for the Appcompat ActionBar, that all derive from a base implementation. But in general you don't need it, if you aren't using the ActionBar in earlier versions. The ActionBar is stable enough and the fixes made with the appcompat library for later versions are not very significant.

If you wrap something in Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB then it is guaranteed to not being called in a version earlier than HoneyComb, so no, you can keep doing that for API's, that were introduced later without crashing earlier versions.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top