React Native - 是否有构建和部署调试 APK 的简单方法?

React Native - Is There a Simple Way to Build and Deploy Debug APK?

所有文档似乎都在考虑一个处于开发模式并寻求为开发人员设备切换端口或正在进行生产构建!我只想创建一个 appDebug.apk,任何人都可以使用它来 运行 应用程序,而不会看到有关桥、事件发射器或 AppRegistry 等的错误。我不能告诉其他想看 React Native 应用程序的人切换端口等,我不想每次共享应用程序时都进行完整发布。有什么建议吗?

更新:我不想调试应用程序。我只想发布一个可在任何人的设备上运行的测试版本,以便我可以共享该版本进行测试。

 UPDATE: HERE IS MY PROJECT STRUCTURE:under
   main-project
   -> index.android.js
   ->gridlew
   -> build.properties
   ->build.gradle
   ->package.json
   -> my-app  (App project folder)
       -> build->output->apk->release.apk 
       ->src->main->assets
       ->src->main->res 
       ->src->main->java

也许使用 Expo or create-react-native-app

在 iOS 或 Android phone 上安装 Expo 应用程序。

运行你的项目,你会得到一个link或QRCode。 然后发送此 link 或二维码以将您的应用分享给任何人。

为了在设备上测试应用程序,我使用了另一个问题的评论

您需要为调试版本手动创建包。

捆绑调试构建:

react-native bundle --dev false --platform android --entry-file index.android.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug

创建调试版本:

cd android
./gradlew assembleDebug

.apk 将位于:

"APP"/android/app/build/outputs/apk

P.S。另一种方法可能是修改 gradle 脚本。

桥梁问题:

react-native run-android
react-native start --reset-cache

或:

cd myproject  
react-native start > /dev/null 2>&1 &  
curl "http://localhost:8081/index.android.bundle?platform=android" -o
> "android/app/src/main/assets/index.android.bundle

或:

adb reverse tcp:8081 tcp:8081

在我的项目中,我通过创建一个将生成的 RN 代码捆绑在 APK 中的构建变体来实现这一点。

正在生成 bundle.js

使用 wget 我从 Node.JS 本地服务器获取 RN 代码并将其保存为 bundle.js:

wget "http://127.0.0.1:8081/index.android.bundle?platform=android&dev=false" -O bundle.js

正在将 bundle.js 添加到项目中

我将 bundle.js 文件添加到 assets/ 目录。

创建将 RN 指向文件的构建变体

我不想在本地 (bundle.js) 和实时版本之间切换时手动更改我的代码。所以我为这种情况创建了一个构建变体。 有一个关于构建变体的广泛教程 here,所以我将只讨论关键的细节。

在我的 build.gradle 中,在 android 节点下,我添加了:

productFlavors {
      bundled {
          buildConfigField 'boolean', 'BUNDLED', 'true'
          buildConfigField 'String', 'DEV_HOST', "null"
      }
}

这会自动生成 BuildConfig.java(更多关于此 here):

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "....";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "bundled";
  public static final int VERSION_CODE = ...;
  public static final String VERSION_NAME = ...;
  // Fields from product flavor: bundled
  public static final boolean BUNDLED = true;
}

将 RN 指向 bundle.js

所以现在我根据我的构建变体启动 RN:

boolean bundled = BuildConfig.BUNDLED;

mReactInstanceManager = ReactInstanceManager.builder()
        .setApplication(getApplication())
        .setBundleAssetName("bundle.js")
        .setJSMainModuleName("index.android")
        .setJSBundleFile(bundled ? "assets://bundle.js" : null)
        .addPackage(new MainReactPackage(false))
        .addPackage(mInnerItemReactPackage)
        .setUseDeveloperSupport(bundled ? false : ConfigSupplier.isDebuggable())
        .setInitialLifecycleState(LifecycleState.RESUMED)
        .build();

正在生成 APK

我从构建变体屏幕中选择了正确的构建变体:

然后照常点击 Build -> Build APK.


稍后我可能会添加更详细的博客post。

以下是完成这项工作所需的步骤,这些步骤取自 build.grade 创建反应本机应用程序所生成的文件:

这是您的应用构建文件:

import com.android.build.OutputFile
// These properties must be declared above the apply below!
project.ext.react = [
    // whether to bundle JS and assets in debug mode
    bundleInDebug: true,

    // whether to bundle JS and assets in release mode
    bundleInRelease: true,

    bundleIn$releaseDebug: true  // For custom release type

  ]


 apply from: "../node_modules/react-native/react.gradle"  // adjust depending on where your node_modules directory is located.

还要检查您是否在 build.gradle 升级中使用应用程序包名称等定义了 ext 属性。

这有效,并且为所有构建类型成功创建了包。