Java 在非 SDK 接口上访问隐藏方法 logcat 警告和限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52690188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:34:18  来源:igfitidea点击:

Accessing hidden method logcat warnings and Restrictions on non-SDK interfaces

javaandroidandroid-9.0-pie

提问by Kostas Trakos

I have read in Android docs about "Restrictions on non-SDK interfaces":

我在 Android 文档中阅读了关于“非 SDK 接口的限制”:

These restrictions are applied whenever an app references a non-SDK interface or attempts to obtain its handle using reflection or JNI... Handling of non-SDK interfaces is an implementation detail that the API abstracts away; it is subject to change without notice... Greylisted non-SDK interfaces encompass methods and fields which continue to function in Android 9, but to which we don't guarantee access in future versions of the platform... You can use adb logcat to access these log messages, which appear under the PID of the running app...

每当应用程序引用非 SDK 接口或尝试使用反射或 JNI 获取其句柄时,就会应用这些限制。非 SDK 接口的处理是 API 抽象掉的实现细节;如有更改,恕不另行通知……列入灰名单的非 SDK 接口包含在 Android 9 中继续运行的方法和字段,但我们不保证在未来版本的平台中可以访问这些方法和字段……您可以使用 adb logcat访问这些日志消息,它们出现在正在运行的应用程序的 PID 下...

Here are the relevant parts of my code running on an API 28 Emulator:

以下是我在 API 28 模拟器上运行的代码的相关部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

           ................       

    MobileAds.initialize(this,  "ca-app-pub-3940256099942544~3347511713");

           ................

    adView = new AdView(this);

    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    adView.setAdSize(AdSize.BANNER);
    adView.setBackgroundColor(Color.TRANSPARENT);
    adView.setVisibility(View.GONE);            
    adView.loadAd(adBuilder());

           ................

    interstitial = new InterstitialAd(GLGame.this);            
    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    interstitial.loadAd(adBuilder());        

    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    mRewardedVideoAd.setRewardedVideoAdListener(this);        
    loadRewardedVideoAd();        
}

AdRequest adBuilder() {
    return new AdRequest.Builder().build();
}
public void loadRewardedVideoAd() {        
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());        
}

I found that with

我发现与

MobileAds.initialize(this,  "ca-app-pub-3940256099942544~3347511713");
adView.loadAd(adBuilder());
interstitial.loadAd(adBuilder());

my Logcat output is:

我的 Logcat 输出是:

W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker;-><init>(Landroid/content/Context;I)V (light greylist, reflection)
W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker;->logEvent(Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;)V (light greylist, reflection)
W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionStarted(I)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionModified(II)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionModified(IILandroid/view/textclassifier/TextClassification;)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionModified(IILandroid/view/textclassifier/TextSelection;)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionAction(III)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
W: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionAction(IIILandroid/view/textclassifier/TextClassification;)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)

if i disable by commenting

如果我通过评论禁用

//MobileAds.initialize(this,  "ca-app-pub-3940256099942544~3347511713");
//adView.loadAd(adBuilder());
//interstitial.loadAd(adBuilder());

the Accessing hidden logcat goes away.

访问隐藏的 logcat 消失了。

Same as before i got Logcat output:

和我得到 Logcat 输出之前一样:

W: Accessing hidden method Landroid/media/AudioTrack;->getLatency()I (light greylist, reflection)

if i disable by commenting

如果我通过评论禁用

//loadRewardedVideoAd();

the Accessing hidden logcat goes away.

访问隐藏的 logcat 消失了。

As you can see the code:

如您所见,代码如下:

MobileAds.initialize(this,  "ca-app-pub-3940256099942544~3347511713");
adView.loadAd(adBuilder());
interstitial.loadAd(adBuilder());
loadRewardedVideoAd();

caused a lot of Accessing hidden logcat.

造成了大量的Accessing 隐藏logcat。

My questions are:

我的问题是:

  1. These are a problem of the emulator?
  2. Is it possible that i make usage of NON-SDK interfaces (see Accessing hidden methods, light greylist, reflection) that will break my app in future versions of the platform?
  3. How can this be fixed?
  1. 这些是模拟器的问题吗?
  2. 我是否可能使用非 SDK 接口(请参阅访问隐藏方法、浅灰名单、反射),这会在平台的未来版本中破坏我的应用程序?
  3. 如何解决这个问题?

回答by Ramesh Yankati

Greylisted non-SDK interfaces means methods and fields which continue to function in Android 9, but to which google doesn't guarantee access in future versions of the platform. If there is a reason that you cannot implement an alternative strategy to a greylisted API, you may file a bug to request reconsideration of the restriction.

列入灰名单的非 SDK 接口意味着在 Android 9 中继续运行的方法和字段,但谷歌不保证在该平台的未来版本中可以访问。如果出于某种原因,您无法实施灰名单 API 的替代策略,您可以提交错误请求重新考虑限制。

https://issuetracker.google.com/issues/new?component=328403&template=1027267

https://issuetracker.google.com/issues/new?component=328403&template=1027267