关于 RN 0.60.4 打包集成到 第三方应用报错 Activity needs to be set if initial lifecycle state is resumed



  • 最近写了一个 rn(版本是0.60.4) 工程打包集成到 第三方原生android的项目,在原生android工程 MainActivity 中写的内容是这样:

    public class MainActivity extends AppCompatActivity {

    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReactRootView = new ReactRootView(this);
        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModulePath("index")
                .addPackage(new MainReactPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        mReactRootView.startReactApplication(mReactInstanceManager, "AwesomeProject", null);
    
        setContentView(mReactRootView);
    }
    

    }

    然后如下错误:
    Process: com.example.myapplication, PID: 23440
    java.lang.AssertionError: Activity needs to be set if initial lifecycle state is resumed
    at com.facebook.infer.annotation.Assertions.assertNotNull(Assertions.java:35)
    at com.facebook.react.ReactInstanceManagerBuilder.build(ReactInstanceManagerBuilder.java:252)
    at com.example.myapplication.MainActivity.onCreate(MainActivity.java:30)
    at android.app.Activity.performCreate(Activity.java:7326)
    at android.app.Activity.performCreate(Activity.java:7317)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3066)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6981)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

    通过跟断点进入rn 核心包中到 ReactInstanceManagerBuilder.class的
    public ReactInstanceManager build();方法中的
    if (this.mInitialLifecycleState == LifecycleState.RESUMED) {
    Assertions.assertNotNull(this.mCurrentActivity, "Activity needs to be set if initial lifecycle state is resumed");
    }

    请问一下这是什么情况?请大牛们解答一下



  • 显然少了

    .setCurrentActivity(this)
    


  • @晴明 谢谢,现在断点过去了,但是又报这个错了

    Process: com.example.demo, PID: 9112
    java.lang.RuntimeException: SoLoader.init() not yet called
    at com.facebook.soloader.SoLoader.assertInitialized(SoLoader.java:781)
    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:505)
    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:484)
    at com.facebook.react.ReactInstanceManagerBuilder.getDefaultJSExecutorFactory(ReactInstanceManagerBuilder.java:304)
    at com.facebook.react.ReactInstanceManagerBuilder.build(ReactInstanceManagerBuilder.java:279)
    at com.example.demo.change.SecondFragment.getReactRootView(SecondFragment.java:100)
    at com.example.demo.change.SecondFragment.onActivityCreated(SecondFragment.java:47)
    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2460)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1483)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
    at android.os.Handler.handleCallback(Handler.java:873)



  • 在MainApplication#onCreate里加上SoLoader.init(this, false);

    
    import com.facebook.soloader.SoLoader;
    
    
    @Override
    public void onCreate() {
         super.onCreate();
         SoLoader.init(this, false);
    }
    


  • @晴明 非常感谢您提供的方法,现在已经跳过显示页面的方法了,但是却报了这个错误

    07-29 11:06:57.826 3264-3264/com.example.demo E/art: dlopen("/data/app/com.example.demo-1/lib/x86/libjscexecutor.so", RTLD_LAZY) failed: dlopen failed: library "libjsc.so" not found
    07-29 11:06:57.826 3264-3264/com.example.demo E/SoLoader: couldn't find DSO to load: libjscexecutor.so caused by: dlopen failed: library "libjsc.so" not found
    07-29 11:06:57.912 3264-3299/com.example.demo E/SoLoader: couldn't find DSO to load: libhermes.so
    07-29 11:06:57.913 3264-3299/com.example.demo E/AndroidRuntime: FATAL EXCEPTION: create_react_context
    Process: com.example.demo, PID: 3264
    java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libhermes.so
    at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:738)
    at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:591)
    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:529)
    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:484)
    at com.facebook.hermes.reactexecutor.HermesExecutor.<clinit>(HermesExecutor.java:20)
    at com.facebook.hermes.reactexecutor.HermesExecutorFactory.create(HermesExecutorFactory.java:27)
    at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:949)
    at java.lang.Thread.run(Thread.java:818)

    是现在最新版的RN 不能加载动态链接库么?



  • 0.59之后有第三方的jsc,0.60有新增的hermes,这些文档都还没有更新
    参考 https://github.com/facebook/react-native/blob/master/template/android/app/build.gradle
    把hermes和jsc的配置加上(二选一也可以)



  • @晴明 感谢您解决,我暂时先换59版本了。

    再麻烦问一下 RN 打包成SDK 集成到 第三方原生 app 中只能以 bundle的形式么?不能以aar的形式么?


Log in to reply