Forbid android.util.Log (flutter/engine#21696)

This commit is contained in:
Dan Field
2020-10-08 14:52:05 -07:00
committed by GitHub
parent 232410d279
commit 73b959d7cb
19 changed files with 104 additions and 27 deletions

View File

@@ -251,6 +251,23 @@ embedding_dependencies_jars =
],
"list lines")
action("check_imports") {
script = "//flutter/tools/android_illegal_imports.py"
sources = android_java_sources
stamp_file = "$root_out_dir/check_android_imports"
# File does not actually get created, but GN expects us to have an output here.
outputs = [ stamp_file ]
args = [
"--stamp",
rebase_path(stamp_file),
"--files",
] + rebase_path(android_java_sources)
}
action("flutter_shell_java") {
script = "//build/android/gyp/javac.py"
depfile = "$target_gen_dir/$target_name.d"
@@ -299,7 +316,10 @@ action("flutter_shell_java") {
args += rebase_path(sources, root_build_dir)
deps = [ ":gen_android_build_config_java" ]
deps = [
":check_imports",
":gen_android_build_config_java",
]
}
action("icudtl_object") {

View File

@@ -13,6 +13,13 @@ import androidx.annotation.NonNull;
public class Log {
private static int logLevel = android.util.Log.DEBUG;
public static int ASSERT = android.util.Log.ASSERT;
public static int DEBUG = android.util.Log.DEBUG;
public static int ERROR = android.util.Log.ERROR;
public static int INFO = android.util.Log.INFO;
public static int VERBOSE = android.util.Log.VERBOSE;
public static int WARN = android.util.Log.WARN;
/**
* Sets a log cutoff such that a log level of lower priority than {@code logLevel} is filtered
* out.
@@ -23,6 +30,12 @@ public class Log {
Log.logLevel = logLevel;
}
public static void println(@NonNull int level, @NonNull String tag, @NonNull String message) {
if (BuildConfig.DEBUG && logLevel <= level) {
android.util.Log.println(level, tag, message);
}
}
public static void v(@NonNull String tag, @NonNull String message) {
if (BuildConfig.DEBUG && logLevel <= android.util.Log.VERBOSE) {
android.util.Log.v(tag, message);

View File

@@ -19,12 +19,12 @@ import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import io.flutter.Log;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.platform.PlatformPlugin;
import io.flutter.util.Preconditions;

View File

@@ -4,12 +4,12 @@
package io.flutter.embedding.android;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.Log;
import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
import io.flutter.plugin.editing.TextInputPlugin;
import java.util.AbstractMap.SimpleImmutableEntry;

View File

@@ -10,12 +10,12 @@ import android.content.res.AssetManager;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.BuildConfig;
import io.flutter.FlutterInjector;
import io.flutter.Log;
import io.flutter.embedding.engine.FlutterJNI;
import io.flutter.util.PathUtils;
import io.flutter.view.VsyncWaiter;

View File

@@ -11,10 +11,10 @@ import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import io.flutter.BuildConfig;
import io.flutter.Log;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;

View File

@@ -4,11 +4,11 @@
package io.flutter.plugin.common;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import io.flutter.BuildConfig;
import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
import java.nio.ByteBuffer;

View File

@@ -4,9 +4,9 @@
package io.flutter.plugin.common;
import android.util.Log;
import androidx.annotation.Nullable;
import io.flutter.BuildConfig;
import io.flutter.Log;
/**
* An implementation of {@link MethodChannel.Result} that writes error results to the Android log.

View File

@@ -4,9 +4,9 @@
package io.flutter.plugin.common;
import android.util.Log;
import androidx.annotation.UiThread;
import io.flutter.BuildConfig;
import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
import java.nio.ByteBuffer;

View File

@@ -4,8 +4,8 @@
package io.flutter.plugin.common;
import android.util.Log;
import io.flutter.BuildConfig;
import io.flutter.Log;
/** Thrown to indicate that a Flutter method invocation failed on the Flutter side. */
public class FlutterException extends RuntimeException {

View File

@@ -4,11 +4,11 @@
package io.flutter.plugin.common;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import io.flutter.BuildConfig;
import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
import java.io.PrintWriter;

View File

@@ -4,8 +4,8 @@
package io.flutter.plugin.common;
import android.util.Log;
import io.flutter.BuildConfig;
import io.flutter.Log;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;

View File

@@ -11,7 +11,6 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
@@ -20,6 +19,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting;
import io.flutter.Log;
import io.flutter.embedding.android.AndroidTouchProcessor;
import io.flutter.embedding.android.FlutterImageView;
import io.flutter.embedding.android.FlutterView;

View File

@@ -17,7 +17,6 @@ import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
@@ -29,6 +28,7 @@ import android.widget.FrameLayout;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.Log;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

View File

@@ -15,7 +15,6 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowInsets;
@@ -28,6 +27,7 @@ import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import io.flutter.BuildConfig;
import io.flutter.Log;
import io.flutter.embedding.engine.systemchannels.AccessibilityChannel;
import io.flutter.plugin.platform.PlatformViewsAccessibilityDelegate;
import io.flutter.util.Predicate;

View File

@@ -9,7 +9,6 @@ import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
@@ -20,6 +19,7 @@ import android.view.accessibility.AccessibilityRecord;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.Log;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -502,9 +502,9 @@ class AccessibilityViewEmbedder {
try {
return (Long) getSourceNodeId.invoke(node);
} catch (IllegalAccessException e) {
Log.w(TAG, e);
Log.w(TAG, "Failed to access getSourceNodeId method.", e);
} catch (InvocationTargetException e) {
Log.w(TAG, e);
Log.w(TAG, "The getSourceNodeId method threw an exception when invoked.", e);
}
return null;
}
@@ -523,9 +523,9 @@ class AccessibilityViewEmbedder {
// type ReflectiveOperationException. As a workaround either create individual
// catch statements, or catch Exception. [NewApi]
} catch (IllegalAccessException e) {
Log.w(TAG, e);
Log.w(TAG, "Failed to access getChildId method.", e);
} catch (InvocationTargetException e) {
Log.w(TAG, e);
Log.w(TAG, "The getChildId method threw an exception when invoked.", e);
}
} else {
try {
@@ -536,9 +536,9 @@ class AccessibilityViewEmbedder {
// type ReflectiveOperationException. As a workaround either create individual
// catch statements, or catch Exception. [NewApi]
} catch (IllegalAccessException e) {
Log.w(TAG, e);
Log.w(TAG, "Failed to access longArrayGetIndex method or the childNodeId field.", e);
} catch (InvocationTargetException | ArrayIndexOutOfBoundsException e) {
Log.w(TAG, e);
Log.w(TAG, "The longArrayGetIndex method threw an exception when invoked.", e);
}
}
return null;
@@ -555,9 +555,9 @@ class AccessibilityViewEmbedder {
// type ReflectiveOperationException. As a workaround either create individual
// catch statements, or catch Exception. [NewApi]
} catch (IllegalAccessException e) {
Log.w(TAG, e);
Log.w(TAG, "Failed to access getParentNodeId method.", e);
} catch (InvocationTargetException e) {
Log.w(TAG, e);
Log.w(TAG, "The getParentNodeId method threw an exception when invoked.", e);
}
}
@@ -620,9 +620,9 @@ class AccessibilityViewEmbedder {
try {
return (Long) getRecordSourceNodeId.invoke(node);
} catch (IllegalAccessException e) {
Log.w(TAG, e);
Log.w(TAG, "Failed to access the getRecordSourceNodeId method.", e);
} catch (InvocationTargetException e) {
Log.w(TAG, e);
Log.w(TAG, "The getRecordSourceNodeId method threw an exception when invoked.", e);
}
return null;
}

View File

@@ -6,9 +6,9 @@ package io.flutter.view;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.UiThread;
import io.flutter.Log;
import io.flutter.app.FlutterPluginRegistry;
import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener;
import io.flutter.embedding.engine.FlutterJNI;

View File

@@ -19,7 +19,6 @@ import android.os.Build;
import android.os.Handler;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.DisplayCutout;
import android.view.KeyEvent;
@@ -41,6 +40,7 @@ import android.view.inputmethod.InputMethodManager;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.UiThread;
import io.flutter.Log;
import io.flutter.app.FlutterPluginRegistry;
import io.flutter.embedding.android.AndroidKeyProcessor;
import io.flutter.embedding.android.AndroidTouchProcessor;

View File

@@ -0,0 +1,44 @@
#!/usr/bin/env python
# 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.
import argparse
import os
import subprocess
import sys
ANDROID_LOG_CLASS = 'android.util.Log'
FLUTTER_LOG_CLASS = 'io.flutter.Log'
def main():
parser = argparse.ArgumentParser(description='Checks Flutter Android library for forbidden imports')
parser.add_argument('--stamp', type=str, required=True)
parser.add_argument('--files', type=str, required=True, nargs='+')
args = parser.parse_args()
open(args.stamp, 'wa').close()
bad_files = []
for file in args.files:
if file.endswith(os.path.join('io', 'flutter', 'Log.java')):
continue
with open(file) as f:
if ANDROID_LOG_CLASS in f.read():
bad_files.append(file)
if bad_files:
print('')
print('Illegal import %s detected in the following files:' % ANDROID_LOG_CLASS)
for bad_file in bad_files:
print(' - ' + bad_file)
print('Use %s instead.' % FLUTTER_LOG_CLASS)
print('')
return 1
return 0
if __name__ == '__main__':
sys.exit(main())