Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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") {
Expand Down
13 changes: 13 additions & 0 deletions shell/platform/android/io/flutter/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
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.
Expand All @@ -23,6 +30,12 @@ public static void setLogLevel(int logLevel) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand All @@ -20,6 +19,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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;
Expand All @@ -29,6 +28,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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;
Expand All @@ -28,6 +27,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
Expand All @@ -20,6 +19,7 @@
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;
Expand Down Expand Up @@ -502,9 +502,9 @@ private Long getSourceNodeId(@NonNull AccessibilityNodeInfo node) {
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;
}
Expand All @@ -523,9 +523,9 @@ private Long getChildId(@NonNull AccessibilityNodeInfo node, int child) {
// 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 {
Expand All @@ -536,9 +536,9 @@ private Long getChildId(@NonNull AccessibilityNodeInfo node, int child) {
// 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;
Expand All @@ -555,9 +555,9 @@ private Long getParentNodeId(@NonNull AccessibilityNodeInfo node) {
// 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);
}
}

Expand Down Expand Up @@ -620,9 +620,9 @@ private Long getRecordSourceNodeId(@NonNull AccessibilityRecord node) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

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;
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand All @@ -41,6 +40,7 @@
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;
Expand Down
44 changes: 44 additions & 0 deletions tools/android_illegal_imports.py
Original file line number Diff line number Diff line change
@@ -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())