Android's StrictMode penaltyListener gives NoClassDefFoundError
Asked Answered
M

1

7

I'm adding StrictMode to my app and it works great until I add a custom ViolationListener.

It seems it cannot find the callback I'm providing and detects that as an error, too.

Could you give me a hand with that?

The following code works just nice:

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
                    .build());

The following code does not work:

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()
                .penaltyListener(Executors.newSingleThreadExecutor(), new StrictMode.OnVmViolationListener() {
                    @Override
                    public void onVmViolation(Violation v) {
                        //DO MY CUSTOM STUFF LIKE LOG IT TO CRASHLYTICS
                        Crashlytics.logException(v);
                    }
                })
                .penaltyLog()
                .build());

This is the logcat reference (Sorry for the Xs. These are covering the package name...)

02-20 19:02:43.682 28793-28793/? W/System: ClassLoader referenced unknown path: /data/app/and.XXXXX.XXXXX.ib.qua-1/lib/arm64
02-20 19:02:43.696 28793-28793/? I/art: Rejecting re-init on previously-failed class java.lang.Class<and.XXXXX.XXXXX.ib.app.MyApp$CrashlyticsViolationListener>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/os/StrictMode$OnVmViolationListener;

Thanks in advance!

BTW: Already did a clean/build/rebuild/disabled instant run....

Mirk answered 20/2, 2019 at 18:9 Comment(0)
D
11

StrictMode.OnVmViolationListener was added in Android API 28, if your device or emulator is of a lower version then this class does not exist, causing the error you're seeing.

Dwanadwane answered 20/2, 2019 at 18:13 Comment(1)
Woah great! Thanks I did not even check for that as Android studio was letting me use it without api restrictions. I’ll try and come back with results.Mirk

© 2022 - 2024 — McMap. All rights reserved.