简体   繁体   中英

IntelliJ/Eclipse not respecting minimum SDK version for Android app

I have an Android project that is targeted to Android 15, but has a minimum version of Android 8. This defined in the manifest file:

<uses-sdk android:minSdkVersion="8"
    android:targetSdkVersion="15"/>

This means I can only use functionality up to Android 8.

When I use methods like the String class' isEmpty() method (that was added in API 9), IntelliJ and Eclipse both happily compile it, but when I run it on older devices, I get crashes because API 8 does not have isEmpty() implemented.

Does anyone know why neither IDE can catch stuff like this?

My Java compiler is version 1.6.0_33 - would forcing the IDE to use an older version help here?

Thanks! If there is any info I missed, feel free to leave a comment and I'll dig the info up!

It is supposed to compile. This allows you to use newer methods in more recent SDKs via wrapper classes. If your target SDK was 8, then the IDE would catch you and complain.

More info here:

http://developer.android.com/guide/practices/compatibility.html

The wrapper class technique is very useful. There is a guide in this blog post about it:

http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html

Edit:

It should be noted the latest ADT (r20, perhaps earlier) for Eclipse with Lint in it's default settings will look for usage of API methods above the minSDK. However it will only check files after they have been modified, or if you run Lint.

More on Lint: http://tools.android.com/tips/lint

For IntelliJ IDEA:

  1. Project Structure > Modules
  2. Choose a module
  3. On the Dependencies tab, select the Module SDK that correlates to the API level you want to limit yourself to. Android 2.3.3 Google APIs for instance.

IDEA is also working on improving their Android Lint support so that it can help you check for adherence to API levels more easily even when compiling with the latest SDK.

http://youtrack.jetbrains.com/issue/IDEA-85200

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM