Tencent / Shadow

零反射全动态Android插件框架
BSD 3-Clause "New" or "Revised" License
7.45k stars 1.3k forks source link

插件调用getOnBackInvokedDispatcher()报错:No virtual method getOnBackInvokedDispatcher()Landroid/window/OnBackInvokedDispatcher #1342

Closed hehe2015 closed 2 months ago

hehe2015 commented 2 months ago
// 这是插件
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.toSec).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new android.content.Intent(MainActivity.this, SecActivity.class));
            }
        });

        if (Build.VERSION.SDK_INT >= 33) {
            System.out.println("### registerOnBackInvokedCallback");
            getOnBackInvokedDispatcher()
                    .registerOnBackInvokedCallback(
                            OnBackInvokedDispatcher.PRIORITY_DEFAULT, new OnBackInvokedCallback() {
                                @Override
                                public void onBackInvoked() {
                                    System.out.println("### onBackInvoked");
                                }
                            });
        }

    }
}
FATAL EXCEPTION: main
Process: com.tencent.shadow.sample.host:plugin, PID: 16467
java.lang.NoSuchMethodError: No virtual method getOnBackInvokedDispatcher()Landroid/window/OnBackInvokedDispatcher; in class Lcom/tencent/shadow/sample/plugin/MainActivity; or its super classes (declaration of 'com.tencent.shadow.sample.plugin.MainActivity' appears in /data/user/0/com.tencent.shadow.sample.host/files/ShadowPluginManager/UnpackedPlugin/test-dynamic-manager/4ba04d63c25f940791d15979fd45c500/my-plugin-debug.zip/my-plugin-plugin-debug.apk)
    at com.tencent.shadow.sample.plugin.MainActivity.onCreate(MainActivity.java:24)
    at com.tencent.shadow.core.loader.delegates.ShadowActivityDelegate.onCreate(ShadowActivityDelegate.kt:156)
    at com.tencent.shadow.core.runtime.container.PluginContainerActivity.onCreate(PluginContainerActivity.java:84)
    at android.app.Activity.performCreate(Activity.java:8891)
    at android.app.Activity.performCreate(Activity.java:8856)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1468)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3973)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4144)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:99)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:224)
    at android.os.Looper.loop(Looper.java:318)
    at android.app.ActivityThread.main(ActivityThread.java:8759)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)

调整compileSdkVersion、targetSdkVersion 为33还是报这个错误!!@shifujun 请问这个是哪里的问题?

hehe2015 commented 2 months ago

在com.tencent.shadow.core.runtime.container.PluginContainerActivity 和 com.tencent.shadow.core.runtime.ShadowActivity添加

@Override
    public OnBackInvokedDispatcher getOnBackInvokedDispatcher() {
        return super.getOnBackInvokedDispatcher();
    }

解决