Best practice for loading native library async

40 Views Asked by At

I know everyone just loads native library when class is loaded like static {System.loadLibrary("libXXX")}. Recently I found that the StrictMode shows DiskReadViolation at System.loadLibrary, should I move it to non-UI thread? It seems not easy to implement. Is there any official documentation that explains what to do?

1

There are 1 best solutions below

2
Botje On

The intent behind StrictMode is to help you catch potentially blocking operations while the app is being used, however in this case the System.loadLibrary call happens long before the screen is changed as it is in a class initializer.

From the documentation on StrictMode:

If you find violations that you feel are problematic, there are a variety of tools to help solve them: threads, Handler, AsyncTask, IntentService, etc. But don't feel compelled to fix everything that StrictMode finds. In particular, many cases of disk access are often necessary during the normal activity lifecycle. Use StrictMode to find things you did by accident. Network requests on the UI thread are almost always a problem, though.