Search code examples
androidandroid-compatibility

Android Compatibility Build Problems


BACKGROUND

I am developing an android application that needs to work on API levels from 7 to 16.

THE PROBLEM

Whenever I go to build my project this is the process I have to go through.

  1. Clean project
  2. Run Project
  3. "Errors in project" > Click OK > Run Project Again
  4. Runs fine on any API

I think the problem is due to the fact I am including code (such as the ActionBar) that API < 3.0 can't use but I am checking for it and running something else if thats the case.

THE QUESTION

Does anyone know a way round this because it is very time consuming considering I have to do this every time I want to run it.


Solution

  • I suppose you are using Eclipse for your development. You can annotate the offending methods as follows:

    private final int VERSION = android.os.Build.VERSION.SDK_INT;
    private File myDir;
    // some stuff here
    @SuppressLint("NewApi")
    private void doSomething() {
        if (VERSION < 8) {
            // Uses a method available since API 1
            myDir = Environment.getExternalStorageDirectory();
         else {
            // Uses a method available since API 8
            myDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        }
        // Do more stuff
    }