Search code examples
androidcompatibilityandroid-support-library

Android Api level 4 compatibility error


I'm using supportv4 compatibility library, the target Api level of my app is 17, and i want to make it work even in Api level 4. When i run my app i have this error only in api level 4 :

03-11 16:16:01.412: E/dalvikvm(302): Could not find method android.view.Display.getSize, referenced from method com.meher.tools.menulauncher.common.MyApplicationContext.setScreenDimensions
03-11 16:16:01.412: W/dalvikvm(302): VFY: unable to resolve virtual method 3065: Landroid/view/Display;.getSize (Landroid/graphics/Point;)V
03-11 16:16:01.412: W/dalvikvm(302): VFY:  rejecting opcode 0x6e at 0x0018
03-11 16:16:01.412: W/dalvikvm(302): VFY:  rejected Lcom/meher/tools/menulauncher/common/MyApplicationContext;.setScreenDimensions (Landroid/view/Display;)V
03-11 16:16:01.412: W/dalvikvm(302): Verifier rejected class Lcom/meher/tools/menulauncher/common/MyApplicationContext;
03-11 16:16:01.422: D/AndroidRuntime(302): Shutting down VM
03-11 16:16:01.422: W/dalvikvm(302): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
03-11 16:16:01.422: E/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
03-11 16:16:01.422: E/AndroidRuntime(302): java.lang.VerifyError: com.meher.tools.menulauncher.common.MyApplicationContext
03-11 16:16:01.422: E/AndroidRuntime(302):  at com.meher.tools.menulauncher.MainActivity.onCreate(MainActivity.java:79)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.app.ActivityThread.access$2100(ActivityThread.java:116)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.os.Looper.loop(Looper.java:123)
03-11 16:16:01.422: E/AndroidRuntime(302):  at android.app.ActivityThread.main(ActivityThread.java:4203)
03-11 16:16:01.422: E/AndroidRuntime(302):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:16:01.422: E/AndroidRuntime(302):  at java.lang.reflect.Method.invoke(Method.java:521)
03-11 16:16:01.422: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-11 16:16:01.422: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-11 16:16:01.422: E/AndroidRuntime(302):  at dalvik.system.NativeStart.main(Native Method)
03-11 16:16:01.422: I/dalvikvm(302): threadid=7: reacting to signal 3
03-11 16:16:01.422: E/dalvikvm(302): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

This is the code raising the error :

MainActivity.java :

// Init application context
        myApplicationContext = new MyApplicationContext(this,
                getWindowManager().getDefaultDisplay());

MyApplicationContext.java :

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    @SuppressWarnings("deprecation")
    public void setScreenDimensions(Display display) {
        Point size = new Point();
        if (android.os.Build.VERSION.SDK_INT < 13) {
            this.screenWidth = display.getWidth();
            this.screenHeight = display.getHeight();
        } else {
            try {
                display.getSize(size);
                this.screenWidth = size.x;
                this.screenHeight = size.y;
            } catch (java.lang.NoSuchMethodError ignore) { // Older device
                this.screenWidth = display.getWidth();
                this.screenHeight = display.getHeight();
            }
        }
    }

AndroidManifest.mf :

<uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="17" />

Solution

  • API Level 4 does a full check of every method used in the class to make sure it exists, as noted in this answer. Therefore you can either:

    • encapsulate all new API calls in separate classes (which, because they aren't loaded in your v4 runtime due to conditional logic, are never greedily checked)
    • move your minimum SDK to v7 (Android 2.1) and lose the 0.2% of the Android population still using v4.