启用S​​trictMode(全部检测)的Crashlytics提供“检测到未标记的套接字”

时间:2018-11-07 17:54:58

标签: android crashlytics-android android-strictmode

我尝试将Crashlytics添加到我的应用中,该应用已通过StrictMode启用了detectAll()。结果为Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage

完整堆栈:

E/StrictMode: null
java.lang.Throwable: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage
    at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2012)
    at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:78)
    at libcore.io.BlockGuardOs.tagSocket(BlockGuardOs.java:47)
    at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:310)
    at libcore.io.IoBridge.socket(IoBridge.java:667)
    at java.net.PlainSocketImpl.socketCreate(PlainSocketImpl.java:116)
    at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:98)
    at java.net.Socket.createImpl(Socket.java:484)
    at java.net.Socket.getImpl(Socket.java:547)
    at java.net.Socket.setSoTimeout(Socket.java:1175)
    at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:139)
    at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(Unknown Source:0)
    at io.fabric.sdk.android.services.network.HttpRequest.code(HttpRequest.java:1357)
    at io.fabric.sdk.android.services.settings.DefaultSettingsSpiCall.handleResponse(DefaultSettingsSpiCall.java:104)
    at io.fabric.sdk.android.services.settings.DefaultSettingsSpiCall.invoke(DefaultSettingsSpiCall.java:88)
    at io.fabric.sdk.android.services.settings.DefaultSettingsController.loadSettingsData(DefaultSettingsController.java:88)
    at io.fabric.sdk.android.services.settings.DefaultSettingsController.loadSettingsData(DefaultSettingsController.java:65)
    at io.fabric.sdk.android.services.settings.Settings.loadSettingsData(Settings.java:153)
    at io.fabric.sdk.android.Onboarding.retrieveSettingsData(Onboarding.java:126)
    at io.fabric.sdk.android.Onboarding.doInBackground(Onboarding.java:99)
    at io.fabric.sdk.android.Onboarding.doInBackground(Onboarding.java:45)
    at io.fabric.sdk.android.InitializationTask.doInBackground(InitializationTask.java:63)
    at io.fabric.sdk.android.InitializationTask.doInBackground(InitializationTask.java:28)
    at io.fabric.sdk.android.services.concurrency.AsyncTask$2.call(AsyncTask.java:311)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)
W/System.err: StrictMode VmPolicy violation with POLICY_DEATH; shutting down.

我试图创建一个新项目,它与测试项目中MainActivity中的代码相同:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    setCrashlytics();
    setStrictMode();
}

private void setCrashlytics() {
    Fabric.with(this, new Crashlytics());
}

private void setStrictMode() {
    if (BuildConfig.DEBUG) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .penaltyDeath()
                .build());
    }
}

我确实看到了Okhttp has this issue(以及其他库),但是我不知道这是否有意义。
我将添加一个变通方法作为答案(基本上不使用detectAll()),但是我想知道是否有合适的解决方案,也许有一种将标记添加到套接字的方法?

2 个答案:

答案 0 :(得分:1)

一种解决方法是不使用detectAll(),我复制了detectAll()的代码(来自Crashlytics)并进行了修改;但是,由于有些类无法访问,因此我不得不删除部分代码。
我不是这种解决方法的忠实拥护者,但不确定是否有更好的解决方案。

private void setStrictMode() {
    if (BuildConfig.DEBUG) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll()   // detectDiskReads, detectDiskWrites, detectNetwork
                .penaltyLog()
                .build());

        StrictMode.setVmPolicy(getStrictModeBuilder().build());
    }
}

private StrictMode.VmPolicy.Builder getStrictModeBuilder() {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();

    builder.detectLeakedSqlLiteObjects();

    final int sdkInt = Build.VERSION.SDK_INT;
    if (sdkInt >= Build.VERSION_CODES.HONEYCOMB) {
        builder.detectActivityLeaks();
        builder.detectLeakedClosableObjects();
    }

    if (sdkInt >= Build.VERSION_CODES.JELLY_BEAN) {
        builder.detectLeakedRegistrationObjects();
    }

    if (sdkInt >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        builder.detectFileUriExposure();
    }

    if (sdkInt >= Build.VERSION_CODES.O) {
        builder.detectContentUriWithoutPermission();
    }

    builder.penaltyLog().penaltyDeath();
    return builder;
}

选项二-从penaltyDeath()中删除VmPolicy-这是我目前正在做的事情:

private void setStrictMode() {
if (BuildConfig.DEBUG) {
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()   // detectDiskReads, detectDiskWrites, detectNetwork
            .penaltyLog()
            .build());

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

答案 1 :(得分:0)

Api 28引入了:

public StrictMode.VmPolicy.Builder penaltyListener (Executor executor,
                StrictMode.OnVmViolationListener listener)

因此,至少您可以侦听特定的违规行为(例如UntaggedSocketViolation)并选择忽略它们。当然,如果Crashlytics适当地标记其套接字会更好。