其他分享
首页 > 其他分享> > Android P(API 28)-违反StrictMode策略“ SmartSelectionEventTracker $SelectionEvent;-> selectionAction”是

Android P(API 28)-违反StrictMode策略“ SmartSelectionEventTracker $SelectionEvent;-> selectionAction”是

作者:互联网

我正在使用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$1(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);
(...)

所以我在通货膨胀发生时得到了违规,我不太了解.

如您所见,在提到的那行之后不久,WebView开始起作用.我看了SmartSelectionEventTracker here的源代码,它看起来像是TextViews,WebViews等控件的通用类.
WebViews似乎与MockViews有关,后者与TextViews有关.

但是除了这一发现之外,我还不知道违规的发生原因/原因以及我可以采取什么措施.

有人可以向我解释吗?

解决方法:

在Android P中,限制已添加到私有API(即,不属于公共SDK且可以通过反射访问的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.但是,正如您提到的,堆栈跟踪显示这是来自WebView.特别是在初始化SmartSelectionClient期间.它将初始化隐式访问SmartSelectionEventTracker的本机库,该库在其Javadoc语句中用@hide标记,这表明它不属于公共SDK.

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

标签:android,reflection,android-strictmode
来源: https://codeday.me/bug/20191010/1886851.html