Is there any kind of conditional compiling for Android? I am trying to use android.webkit.WebView.onPause(), while still also supporting Android API 8.
I tried the solutions suggested by Fiddler and Andrey Voitenkov at Conditional compiling in Android? :
public void pause()
{
int version = android.os.Build.VERSION.SDK_INT;
if(version >= 11)
{
webViewOnPause(_webView);
}
super.onPause();
}
@TargetApi(11)
void webViewOnPause(final android.webkit.WebView webview)
{
new Runnable() {
public void run() {
new PauseStuffAvailableOnHC(webview);
}
}.run();
}
class PauseStuffAvailableOnHC
{
@TargetApi(11)
public PauseStuffAvailableOnHC(android.webkit.WebView webview)
{
webview.onPause();
}
}
...but I am still prevented from compiling/running the app:
"The method onPause() is undefined for the type WebView"
and
"Your project contains error(s), please fix them before running your application."
Am I doing something wrong, or is it simply not possible to do this?
Your code looks fine, it checks for the minimal SDK version before using a feature that is only available from that version on. So if you run your app on an Android device with a version lower than API 11, that code will not be executed. But if it is run on an Android device with API 11 or higher, it will be executed.
So this means, that your app should be compiled against at least API 11, because it contains a feature that is introduced in API 11. Depending on the version it is run on, that feature will be enabled or not.
To do this, adjust your AndroidManifest.xml :
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />
This will build your Android app against API 11, but still allows it be used on an Android device running API 8 or later.