tags:

views:

64

answers:

1

I was testing android minSDKVersion property and find a strange behavior-

I put minSDKVersion=3 (1.5) and targetSDKVersion=4 (1.6) in androidManifest.xml file.

For testing, I put following lines in onCreate method of launching activity -

 android.telephony.SmsManager sm = android.telephony.SmsManager.getDefault();
 ArrayList<String> stringArray = sm.divideMessage("this is message");
 Toast.makeText(this, stringArray.get(0), Toast.LENGTH_LONG).show(); 

android.telephony.SmsManager class is being introduced in android 1.6.

After adding these lines, on 1.6 emulator it was showing toast, but on 1.5 emulator it did not show toast and did not crashed either.

I was expecting that the application will crash on 1.5 emulator but this did not happen. Can anyone explain this?

Thanks

A: 

Attempting to call methods of classes that aren't available will result in an exception.

It isn't clear to me why you did not observe a crash. Perhaps you have other code that is saving you.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.versions"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MinSdk"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest> 

Activity Code:

package com.example.versions;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MinSdk extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SmsManager sm = SmsManager.getDefault();
        ArrayList<String> stringArray = sm.divideMessage("this is message");
        Toast.makeText(this, stringArray.get(0), Toast.LENGTH_LONG).show(); 
    }
}

Dump of Logcat when run in Android 1.5 emulator:

E/dalvikvm(  779): Could not find method android.telephony.SmsManager.getDefault, referenced from method com.example.versions.MinSdk.onCreate
W/dalvikvm(  779): VFY: unable to resolve static method 3: Landroid/telephony/SmsManager;.getDefault ()Landroid/telephony/SmsManager;
W/dalvikvm(  779): VFY:  rejecting opcode 0x71 at 0x0008
W/dalvikvm(  779): VFY:  rejected Lcom/example/versions/MinSdk;.onCreate (Landroid/os/Bundle;)V
W/dalvikvm(  779): Verifier rejected class Lcom/example/versions/MinSdk;
W/dalvikvm(  779): Class init failed in newInstance call (Lcom/example/versions/MinSdk;)
D/AndroidRuntime(  779): Shutting down VM
W/dalvikvm(  779): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
E/AndroidRuntime(  779): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  779): java.lang.VerifyError: com.example.versions.MinSdk
E/AndroidRuntime(  779):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(  779):    at java.lang.Class.newInstance(Class.java:1472)
E/AndroidRuntime(  779):    at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
E/AndroidRuntime(  779):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
E/AndroidRuntime(  779):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
E/AndroidRuntime(  779):    at android.app.ActivityThread.access$1800(ActivityThread.java:112)
E/AndroidRuntime(  779):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
E/AndroidRuntime(  779):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  779):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  779):    at android.app.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime(  779):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  779):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  779):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime(  779):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
E/AndroidRuntime(  779):    at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(  541): GC freed 2 objects / 48 bytes in 194ms
I/Process (  575): Sending signal. PID: 779 SIG: 3
I/dalvikvm(  779): threadid=7: reacting to signal 3
I/dalvikvm(  779): Wrote stack trace to '/data/anr/traces.txt'
Tim Kryger