访问隐藏方法日志警告和非 SDK 接口上的限制

2022-09-02 04:50:25

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

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

以下是在 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());        
}

我发现

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

我的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)

如果我通过评论禁用

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

访问隐藏的日志消失了。

与我获得Logcat输出之前相同:

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

如果我通过评论禁用

//loadRewardedVideoAd();

访问隐藏的日志消失了。

如您所见,代码:

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

导致大量访问隐藏的日志。

我的问题是:

  1. 这些是模拟器的问题吗?
  2. 我是否有可能使用非SDK接口(请参阅访问隐藏方法,浅灰名单,反射)将在平台的未来版本中破坏我的应用程序?
  3. 如何解决这个问题?

答案 1

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

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


答案 2

推荐