3

I created a chat project using android studio 1.0.1
this is the gradle build properties
apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "chat.mchattrav.chattrav"
        minSdkVersion 5
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

when I run the application I get this error message Immediately

unfortunately app has stopped

when I debug the application I get this exception information

java.lang.NoSuchMethodError: android.os.StatFs.getBlockSizeLong

It seems like I used lower api level than "18" which required by this method
can I solve this problem without the need to increase the api level "minSdkVersion"?
can I use the support library instead ?

Rickless
  • 1,377
  • 3
  • 17
  • 36

1 Answers1

10

If you need to support SDK lower then 18, then you need to handle that.

Exists 2 methods:

public int getBlockSize () Added in API1, was deprecated at API18

and

public long getBlockSizeLong () Added in API18

Your project use 2nd one, you need to find all usages and care about running android version, for example

StatFs staFs = new StatFs("path");
long size = 0;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
    size = staFs.getBlockSizeLong();
}else {
    size = staFs.getBlockSize();
}
// use size ...
gio
  • 4,950
  • 3
  • 32
  • 46
  • thank you gio, can you please tell me why using support library ? and when to use it ? for example can I use the API 18 methods "public long getBlockSizeLong ()" in lower api level platform by using the support library or am i misunderstood something ? – Rickless Jan 11 '15 at 18:32
  • 1
    @MadMass support libraries do not cover all methods of Android SDK, each library was designed for some special cases ([link](http://developer.android.com/tools/support-library/index.html)). Your target SDK is 21 and min SDK is 5. That means that during compile will be used API 21, and you allow to install your application at devices with API 5. You need to be ensure about backward compatibility of it at APIs less then 21 (more details at [question](http://stackoverflow.com/q/4568267/940720)). – gio Jan 11 '15 at 18:40