Android solving compatibility with SDK_INT hack; is this ok?
Asked Answered
E

2

0

Running the following (note: target > 3.0)

ActionBar actionBar = getActionBar();

on Android with version < 3.0 (SDK 11) results in a NoSuchMethodError.

There are several ways to get around this, including reflection and class lazy loading. However, the following seems to work across all the devices I've tested (2.3.6, 3.0, 3.1, 4.0):

boolean hasActionBar = android.os.Build.VERSION.SDK_INT >= 11;

if (hasActionBar) {
    ActionBar actionBar = getActionBar();
} else {
    // create custom actionbar
}

Note the SDK_INT parameter is static final, which appears to be why this works.

Is this a valid way to deal with compatibility?

Evaevacuant answered 7/2, 2012 at 20:35 Comment(2)
according to google: developer.android.com/resources/dashboard/… the majority of users are not above 2.3. Therefore, you may want to rethink your strategy. Implement a solution that works on <3.0 devices first.Dapsang
@Paul, this is really for curiosity's sake.Evaevacuant
E
1

It looks like this works due to the JIT compiler. This code fails on SDK < 2.1, which supports this theory. Regardless, this probably isn't a reliable way to avoid reflection.

Evaevacuant answered 8/2, 2012 at 17:42 Comment(0)
A
2

I believe so, as long as everything is setup correctly.

From Reto Meier's blog: http://blog.radioactiveyak.com/2011/02/strategies-for-honeycomb-and-backwards.html

Aara answered 7/2, 2012 at 21:13 Comment(0)
E
1

It looks like this works due to the JIT compiler. This code fails on SDK < 2.1, which supports this theory. Regardless, this probably isn't a reliable way to avoid reflection.

Evaevacuant answered 8/2, 2012 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.