diff --git a/shell/platform/android/io/flutter/Log.java b/shell/platform/android/io/flutter/Log.java index fa31b8476a0f5..a44835bb0c60a 100644 --- a/shell/platform/android/io/flutter/Log.java +++ b/shell/platform/android/io/flutter/Log.java @@ -5,6 +5,7 @@ package io.flutter; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; /** * Port of {@link android.util.Log} that only logs in {@link io.flutter.BuildConfig#DEBUG} mode and @@ -95,4 +96,9 @@ public static void wtf(@NonNull String tag, @NonNull String message) { public static void wtf(@NonNull String tag, @NonNull String message, @NonNull Throwable tr) { android.util.Log.wtf(tag, message, tr); } + + @NonNull + public static String getStackTraceString(@Nullable Throwable tr) { + return android.util.Log.getStackTraceString(tr); + } } diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java index d338b7cfcbce1..e2d3764570d82 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java @@ -11,8 +11,6 @@ import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.StandardMethodCodec; -import java.io.PrintWriter; -import java.io.StringWriter; import java.nio.ByteBuffer; import java.util.HashMap; import java.util.List; @@ -38,10 +36,7 @@ public void invokeViewFocused(int viewId) { } private static String detailedExceptionString(Exception exception) { - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - exception.printStackTrace(printWriter); - return stringWriter.toString(); + return Log.getStackTraceString(exception); } private final MethodChannel.MethodCallHandler parsingHandler = diff --git a/shell/platform/android/io/flutter/plugin/common/MethodChannel.java b/shell/platform/android/io/flutter/plugin/common/MethodChannel.java index 9949167a7ce03..2d646a2514784 100644 --- a/shell/platform/android/io/flutter/plugin/common/MethodChannel.java +++ b/shell/platform/android/io/flutter/plugin/common/MethodChannel.java @@ -11,9 +11,6 @@ import io.flutter.Log; import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler; import io.flutter.plugin.common.BinaryMessenger.BinaryReply; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.io.Writer; import java.nio.ByteBuffer; /** @@ -280,14 +277,8 @@ public void notImplemented() { Log.e(TAG + name, "Failed to handle method call", e); reply.reply( codec.encodeErrorEnvelopeWithStacktrace( - "error", e.getMessage(), null, getStackTrace(e))); + "error", e.getMessage(), null, Log.getStackTraceString(e))); } } - - private String getStackTrace(Exception e) { - Writer result = new StringWriter(); - e.printStackTrace(new PrintWriter(result)); - return result.toString(); - } } } diff --git a/shell/platform/android/io/flutter/plugin/common/StandardMethodCodec.java b/shell/platform/android/io/flutter/plugin/common/StandardMethodCodec.java index a2e8b211c416f..5a8db8b25b677 100644 --- a/shell/platform/android/io/flutter/plugin/common/StandardMethodCodec.java +++ b/shell/platform/android/io/flutter/plugin/common/StandardMethodCodec.java @@ -5,10 +5,8 @@ package io.flutter.plugin.common; import androidx.annotation.NonNull; +import io.flutter.Log; import io.flutter.plugin.common.StandardMessageCodec.ExposedByteArrayOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.io.Writer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -75,7 +73,7 @@ public ByteBuffer encodeErrorEnvelope( messageCodec.writeValue(stream, errorCode); messageCodec.writeValue(stream, errorMessage); if (errorDetails instanceof Throwable) { - messageCodec.writeValue(stream, getStackTrace((Throwable) errorDetails)); + messageCodec.writeValue(stream, Log.getStackTraceString((Throwable) errorDetails)); } else { messageCodec.writeValue(stream, errorDetails); } @@ -96,7 +94,7 @@ public ByteBuffer encodeErrorEnvelopeWithStacktrace( messageCodec.writeValue(stream, errorCode); messageCodec.writeValue(stream, errorMessage); if (errorDetails instanceof Throwable) { - messageCodec.writeValue(stream, getStackTrace((Throwable) errorDetails)); + messageCodec.writeValue(stream, Log.getStackTraceString((Throwable) errorDetails)); } else { messageCodec.writeValue(stream, errorDetails); } @@ -134,11 +132,4 @@ public Object decodeEnvelope(@NonNull ByteBuffer envelope) { } throw new IllegalArgumentException("Envelope corrupted"); } - - @NonNull - private static String getStackTrace(@NonNull Throwable t) { - Writer result = new StringWriter(); - t.printStackTrace(new PrintWriter(result)); - return result.toString(); - } } diff --git a/shell/platform/android/test/io/flutter/LogTest.java b/shell/platform/android/test/io/flutter/LogTest.java new file mode 100644 index 0000000000000..e5fe1f3196d8a --- /dev/null +++ b/shell/platform/android/test/io/flutter/LogTest.java @@ -0,0 +1,32 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package io.flutter; + +import static org.junit.Assert.assertEquals; + +import java.io.PrintWriter; +import java.io.StringWriter; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@Config(manifest = Config.NONE) +@RunWith(RobolectricTestRunner.class) +public class LogTest { + + @Test + public void canGetStacktraceString() { + Exception exception = new Exception(); + String str = Log.getStackTraceString(exception); + + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + exception.printStackTrace(printWriter); + String expectStr = stringWriter.toString(); + + assertEquals(str, expectStr); + } +}