问题
I've recently tried to use StrictMode but got a weird violation, so I tried setting up a new 'Empty Activity' project (in Android Studio
) and added an Application class as follows (as described in here):
public class TestApplication extends Application {
@Override
public void onCreate() {
setStrictMode();
super.onCreate();
}
private void setStrictMode() {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
}
}
I have the same problem in this empty project:StrictMode policy violation; ~duration=165 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=65599 violation=2
Stack:
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440)
at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:241)
at java.io.File.isDirectory(File.java:837)
at dalvik.system.DexPathList$Element.toString(DexPathList.java:634)
at java.lang.String.valueOf(String.java:2827)
at java.util.Arrays.toString(Arrays.java:4511)
at dalvik.system.DexPathList.toString(DexPathList.java:194)
at java.lang.String.valueOf(String.java:2827)
at java.lang.StringBuilder.append(StringBuilder.java:132)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:126)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.DecorView.onResourcesLoaded(DecorView.java:1933)
at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2586)
at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2659)
at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2067)
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.test.testapp.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
The question(s):
Is this a configuration problem?
If not, can I make StrictMode ignore specific method calls (it happens on a layoutInflater.inflate
call as well on the original project)?
Edit (add configuration):
Android Studio version: 3.1.3
minSdkVersion 21
targetSdkVersion 27
appcompat-v7:27.1.1
Device: Nexus 6p
Edit2 (solution):
I realized that the solution by CommonsWare is somewhat hidden in his comment below, so: it was the instant run enabled that caused my problem.
回答1:
Is this a configuration problem?
Probably not, at least in terms of configuring StrictMode
.
I tried reproducing your case, using your TestApplication
class and a new minSdkVersion 19
project, and I could not reproduce the results, testing on a Google Pixel 2.
Possible sources of the crash include:
- Something different about the version of Android on your device (e.g., manufacturer tweaks that the Pixel 2 lacks)
- Something different about the version of
appcompat-v7
in your project (mine wound up with28.0.0-alpha3
) - Something else different about your project (the crash feels like a support multidex issue)
- UPDATE Something different about your development environment — in this case, Instant Run seems to be the culprit, based on the comments
can I make StrictMode ignore specific method calls
Unfortunately, no.
来源:https://stackoverflow.com/questions/51021362/strictmode-disk-read-violation-on-empty-activitys-setcontentview