React Native 找不到开发服务器,正在集成现有 android 应用程序

React Native cannot find development server, integrating existing android app

我正在尝试将现有的 android 应用与 React Native 集成,继 guide 之后。它编译并 运行s,但是当我启动 React activity 时,它崩溃了:

Caused by: java.lang.RuntimeException: Could not connect to development server.

我在模拟器 (genymotion) 和 USB 连接设备上都试过了。我已经 运行 adb reverse tcp:8081 tcp:8081 并使用 adb reverse --list 进行了验证,并且进一步验证了我可以从设备上的浏览器访问 http://localhost:8081/index.android.bundle 除了从我的笔记本电脑(所以包服务器 运行 正常,通过 npm start).

值得注意的是,我可以 运行 教程示例很好,所以它似乎与我现有 Android 应用程序中的 运行ning RN 有关。

我正在使用 react-native 0.27.2。

想法?

更新: 我添加了一些代码以直接从我的一项活动中获取捆绑包:

public void pingReactServer() {
    Request request = new Request.Builder()
            .url("http://10.0.3.2:8081/index.android.bundle")
            .build();
    OkHttpClient client = OkHttpClientProvider.getOkHttpClient();
    client.newCall(request).enqueue(
            new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.v(TAG, "React ping failed: " + call);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.v(TAG, "React ping response: " + response.body().string());
                }
            }
    );
}

这很好用,我取回了捆绑内容。所以 React 的 http 客户端可以看到打包服务器,并且在以正常方式(DevServerHelper)获取捆绑包时一定是其他原因导致了错误。

想通了,是一连串的不幸事件:

tutorial 中的示例代码显示:

mReactInstanceManager = ReactInstanceManager.builder()
    ...
    .setUseDeveloperSupport(BuildConfig.DEBUG)

BuildConfig.DEBUG 实际上定义为 false,因此这会禁用开发人员支持(相反,它希望找到 js 包作为资产)。

在我解决这个问题之后,我 运行 进入了另一个涉及 Proguard 的问题;它已经删除了所有的 devsupport 类,所以我需要在我的 proguard 配置中将它们列入白名单,使用:

# Keep developer support classes.
-keep class com.facebook.react.devsupport.** { *; }

现在我已经了解了 ReactInstanceManager 从开发服务器加载和获取包,但又遇到了一个速度障碍:

Error: Requiring unknown module "react".If you are sure the module is there, try restarting the packager or running "npm install".

不确定为什么 npm install 没有自动拉入它,但是 运行 这手动修复了它:

npm install react@15.1.0

(使用node_modules/react-native/package.json中列出的具体版本。)