Android P (API 28) - StrictMode 策略违规 "SmartSelectionEventTracker$SelectionEvent;->selectionAction" 是什么意思?

Android P (API 28) - What does the StrictMode policy violation "SmartSelectionEventTracker$SelectionEvent;->selectionAction" mean?

我正在使用 StrictMode 以查找非 SDK 用法:

if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectNonSdkApiUsage()
            .penaltyLog()
            .build());
}

现在我收到了政策违规:

D/StrictMode: StrictMode policy violation: android.os.strictmode.NonSdkApiUsedViolation: Landroid/view/textclassifier/logging/    SmartSelectionEventTracker$SelectionEvent;->selectionAction(IIILandroid/view/textclassifier/TextClassification;)Landroid/view/textclassifier/logging/    SmartSelectionEventTracker$SelectionEvent;
        at android.os.StrictMode.lambda$static(StrictMode.java:428)
        at android.os.-$$Lambda$StrictMode$lu9ekkHJ2HMz0jd3F8K8MnhenxQ.accept(Unknown Source:2)
        at java.lang.Class.getDeclaredMethodInternal(Native Method)
        at java.lang.Class.getPublicMethodRecursive(Class.java:2075)
        at java.lang.Class.getMethod(Class.java:2063)
        at java.lang.Class.getMethod(Class.java:1690)
        at bzi.a(SourceFile:11)
        at bzq.a(SourceFile:12)
        at org.chromium.content.browser.selection.SmartSelectionClient.<init>(SourceFile:5)
        at bzZ.a(Unknown Source:7)
        at org.chromium.android_webview.AwContents.e(SourceFile:193)
        at org.chromium.android_webview.AwContents.d(SourceFile:153)
        at org.chromium.android_webview.AwContents.<init>(SourceFile:81)
        at uY.run(SourceFile:15)
        at ahv.a(SourceFile:13)
        at ahw.run(SourceFile:2)
        at org.chromium.base.ThreadUtils.b(SourceFile:31)
        at ahv.a(SourceFile:7)
        at com.android.webview.chromium.WebViewChromiumFactoryProvider.b(SourceFile:6)
        at com.android.webview.chromium.WebViewChromium.init(SourceFile:111)
        at android.webkit.WebView.<init>(WebView.java:678)
        at android.webkit.WebView.<init>(WebView.java:604)
        at android.webkit.WebView.<init>(WebView.java:587)
        at android.webkit.WebView.<init>(WebView.java:574)
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        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 com.mine.ui.events.EventScreen.onCreateView(EventScreen.java:70)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:781)
        (... shortened ...)

重要的一行是:

at com.mine.ui.events.EventScreen.onCreateView(EventScreen.java:70)

检查提到的行:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// This is the important line:
ViewGroup content = (ViewGroup) inflater.inflate(R.layout.mine_event, container, false);
WebView webView = (WebView) content.findViewById(R.id.container);
webView.loadUrl(event.getWebViewUrl());
webView.getSettings().setJavaScriptEnabled(true);
(...)

所以我在 inflation 发生时遇到了违规,我不太明白。

如您所见,在上述行之后不久,WebView 开始发挥作用。我查看了 SmartSelectionEventTracker here 的源代码,它似乎是 Widgets like TextViews, WebViews, ... 的通用 class。 WebViews 似乎与 MockViews 有关,而 MockViewsTextViews.

有关

但除了那个发现之外,我不明白违规行为是如何/为什么发生的,以及我可以采取什么措施来应对。

有人能给我解释一下吗?

在AndroidP中,对私有API(即不属于publicSDK且通过反射访问的API)添加了限制。见 Restrictions on non-SDK interfaces:

Android 9 (API level 28) introduces new restrictions on the use of non-SDK interfaces, whether directly, via reflection, or via JNI. These restrictions are applied whenever an app references a non-SDK interface or attempts to obtain its handle using reflection or JNI. For more information about this decision, see Improving Stability by Reducing Usage of non-SDK Interfaces.

这就是您所看到的——这是一个警告,表明某些东西正在访问这些私有 API 之一,这些 API 将来可能会被删除。但是,正如您提到的,堆栈跟踪显示这是来自 WebView。特别是在 SmartSelectionClient. That initializes a native library that implicitly is accessing SmartSelectionEventTracker 的初始化过程中,它在其 Javadoc 语句中标有 @hide,表明它不是 public SDK 的一部分。

简而言之,对于这个特定的警告,您可能无能为力,所以我不担心。它发生在设备的 Chromium WebView 实现中,因此它不在您的应用程序的控制范围内。由于 Android 和 Chromium 均由 Google 发布,如果在未来的版本中存在合法问题,将在 WebView 实现成为问题之前发布更新。