songzhw
songzhw

Reputation: 272

What will happen if i run a apk with high api compiled in a low api device?

now i have a project, which is compiled with API 14. By "is compiled", i mean the project.properties file has a line "target=android-14". But, i defined the android:minSDK=8 in the AndroidManifest.xml.

In this project, i used the high api features, like android.animation.ValueAnimator, android.app.ActionBar. The project, of course, will run perfectly in high api device.

Since the minSDK is 8, so we can install the apk in 2.x devices, but what will happen then? the ValueAnimator and ActionBar will be erased? or we actually cannot install the apk in 2.x devices?

Upvotes: 1

Views: 502

Answers (2)

FD_
FD_

Reputation: 12919

If you are not using the corresponding classes from the support library, but the native ones that are only included in later versions of Android, your app will crash with a ClassNotFoundException or a NoSuchMethodError.

A workaround would be to either switch to the support libraries (if there are any depending on what features you use), or disable certain functionality of the app depending of the API version the app runs on.

Upvotes: 1

k3b
k3b

Reputation: 14755

if your app is loaded into memory the logcat will get warnings if classes/methods cannot be resolved. Example:

I/dalvikvm﹕ Could not find method java.lang.String.isEmpty, referenced from method eu.lp0.slf4j.android.LoggerFactory.getConfig
W/dalvikvm﹕ VFY: unable to resolve virtual method 524: Ljava/lang/String;.isEmpty ()Z
D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0010
D/dalvikvm﹕ VFY: dead code 0x0013-0044 in Leu/lp0/slf4j/android/LoggerFactory;.getConfig (Ljava/lang/String;)Leu/lp0/slf4j/android/LoggerConfig;

If your app is trying to execute code that is not supported by your device you get an exception NoSuchMethodError or ClassNotFoundException

W/dalvikvm﹕ Exception Ljava/lang/NoSuchMethodError; thrown during Lde/k3b/android/cellinfo/demo/CellInfoDemoActivity;.

Here is a complete example logcat: https://github.com/lp0/slf4j-android/issues/2

To avoid calling methods/classes that your device does not support you can use code like this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  // call method/class that is only available in HONEYCOMB and later
}

For more details see https://developer.android.com/guide/practices/compatibility.html

Upvotes: 2

Related Questions