Hide the Detected problems with API compatibility warning popup

If you run the debug app on Android 9.0, that is, API-28 or above, the following warning pop-up window appears:
Detected problems with API compatibility (visit g.co/dev/appcompat for more info)
indicates that the code has passed Reflect the logic of calling methods or attributes with @hide annotations, that is, calling the logic of the system's hidden API. Starting from Android 9.0, the system will restrict this calling method, and a warning pop-up window will pop up.
To hide this warning popup, there are 3 solutions:

1. Modify the build.gradle file and modify the targetSdkVersion to 28 or above.

2. Use the reflection method to close the pop-up window, and execute the following method in Application.

/**
 * Android 9开始限制开发者调用非官方API方法和接口(即用反射直接调用源码)
 * 弹框提示 Detected problems with API compatibility(visit g.co/dev/appcompat for more info)
 * <p>
 * 隐藏警告弹框
 */
private void closeDetectedProblemApiDialog() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
        return;
    }
    try {
        @SuppressLint("PrivateApi") Class clsPkgParser = Class.forName("android.content.pm.PackageParser$Package");
        Constructor constructor = clsPkgParser.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);

        @SuppressLint("PrivateApi") Class clsActivityThread = Class.forName("android.app.ActivityThread");
        Method method = clsActivityThread.getDeclaredMethod("currentActivityThread");
        method.setAccessible(true);
        Object activityThread = method.invoke(null);
        Field hiddenApiWarning = clsActivityThread.getDeclaredField("mHiddenApiWarningShown");
        hiddenApiWarning.setAccessible(true);
        hiddenApiWarning.setBoolean(activityThread, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

3. Check which system hidden APIs are called in the code, and see if these logics can be implemented in other ways.
When the system hidden API is called, there will be printing similar to Accessing hidden field or Accessing hidden method. For example:
W/com.test.hm: Accessing hidden field Landroid/os/Message;->next:Landroid/os/Message; (light greylist, reflection)
W/com.test.hm: Accessing hidden method Ldalvik /system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, reflection)
W/com.test.hm: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;) V (light greylist, reflection)
W/com.test.hm: Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (light greylist, reflection)
W/com.test.hm: Accessing hidden method Lcom/android /org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([B)V (light greylist, reflection)
W/com.test.hm: Accessing hidden field Landroid/content/ContentResolver;->mTargetSdkVersion:I (dark greylist, reflection)
Of course the above method is called, it may not be easy to find other implementation methods, if you really can’t find it, There is no way.
 

Guess you like

Origin blog.csdn.net/chenzhengfeng/article/details/111652106