简体   繁体   中英

Build version check doesn't work on Android 2.2 Froyo

I am using a simple conditional check on Build.Version.SDK_INT in the onCreate method of my application (code below) to prevent strict mode being enabled on any Android OS earlier than 2.3. Up until recently this had been working fine, but after a re-jig of my project, I receive the following error:

Could not find class 'android.os.StrictMode$ThreadPolicy$Builder', referenced from method com.myPackage.MyApp.onCreate

I have heard that the way class dependencies were evaluated and loaded changed from a static analysis of the class to a 'lazy loading' system in Android 2.0, but since I am using 2.2, I don't think this is at play. I suspect there is something elsewhere in my project structure that is causing this error, but I am at a loss as to what that might be.

Has anyone here had a similar experience and could maybe shed some light on this? Any help would be gratefully received.

Thanks in advance for your help!

Please see my code below for reference:

public class MyApp extends Application {

    @Override
    public void onCreate() {
        // Set up strict mode
        int buildInt = Build.VERSION.SDK_INT;
        Log.d(LogTags.TRIGGER_CODE, String.format("Build is %d (%s)", buildInt, Build.VERSION.CODENAME));
        if (buildInt >= 9) {
            StrictMode.setThreadPolicy(new ThreadPolicy.Builder()
                    .detectCustomSlowCalls()
                    .detectNetwork()
                    .build());
            StrictMode.setVmPolicy((new VmPolicy.Builder()
                    .detectAll()
                    .build()));
        }
        super.onCreate();
     }
 }

This turned out to be just a symptom of another problem elsewhere in code, far too specific to the project to be worth going into here...

Thanks for the thoughts on the root cause here, and sorry for the 'doh' moment :)

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