首页 文章

在没有开发服务器的情况下在设备上构建和安装unsigned apk?

提问于
浏览
85

因为我是反应原生的新人,所以如果步骤中有任何错误,请告诉我 .

我根据文档使用命令构建了一个react native android应用程序

react-native android

在设备上运行时,使用了以下命令

react-native run-android

这给了我在 projectfolder/android/app/build/outputs/apk 中的2个apk文件的输出

enter image description here

现在,当我在安装后使用安装这个apk时,它要求开发服务器连接到捆绑JS . 但我的要求是,用户不必为开发服务器而烦恼,只需要安装apk即可完成所有操作 .

已经经历了一些stackoverflow Questions但没有帮助构建不需要开发服务器的unsigned apk .

你能帮助我找到如何在反应原生中 Build 和未签名的apk的方法吗?

7 回答

  • 28

    您需要手动为调试版本创建捆绑包 .

    捆绑调试版本:

    #React-Native 0.49.0+
    react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
    
    #React-Native 0-0.49.0
    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
    

    然后在捆绑后构建APK:

    $ cd android
    #Create debug build:
    $ ./gradlew assembleDebug
    #Create release build:
    $ ./gradlew assembleRelease #Generated `apk` will be located at `android/app/build/outputs/apk`
    

    附:另一种方法可能是修改gradle脚本 .

  • 168

    请按照这些步骤操作 .

    捆绑你的js:

    如果项目根目录中有index.android.js则运行

    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
    

    如果您在项目根目录中有index.js则运行

    react-native bundle --platform android --dev false --entry-file index.js --bundle-output android / app / src / main / assets / index.android.bundle --assets-dest android / app / src /主/ RES

    创建调试apk:

    cd android/
    ./gradlew assembleDebug
    

    然后你可以在这里找到你的apk:

    cd app/build/outputs/apk/
    
  • 3

    在我看来,在项目目录中运行以下命令 .

    对于本机旧版本(您将在root中看到index.android.js):

    mkdir -p android/app/src/main/assets && rm -rf android/app/build && react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && cd android && ./gradlew clean assembleRelease && cd ../
    

    对于本机新版本的反应(你只看到root中的index.js):

    mkdir -p android/app/src/main/assets && rm -rf android/app/build && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && cd android && ./gradlew clean assembleRelease && cd ../
    

    apk文件将在以下位置生成:

    • Gradle <3.0: android/app/build/outputs/apk/

    • Gradle 3.0: android/app/build/outputs/apk/release/

  • -1

    对于Windows用户,如果所有步骤都正确遵循:https://facebook.github.io/react-native/docs/signed-apk-android.html

    You need to only run: gradlew assembleRelease

    你的文件将是:

    • app-release.apk

    • app-release-unaligned.apk

    Location: E:\YourProjectName\android\app\build\outputs\apk

  • 3

    按照first response后,您可以使用运行您的应用程序

    react-native run-android --variant=debug
    

    而且您的应用程序无需打包即可运行

  • 2

    我正在反应原生0.55.4,基本上我必须手动捆绑:

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

    然后通过usb连接你的设备,启用usb调试 . 使用 adb devices 验证连接的设备 .

    最后运行 react-native run-android ,它将在您的手机上安装调试apk,您可以使用开发服务器运行它

    注意:

    • 从0.49.0开始,入口点是单个 index.js

    • gradlew assembleRelease 仅生成无法安装的release-unsigned apks

  • 18

    切换到android文件夹并运行 ./gradlew assembleRelease 来构建可以发送到Android设备的发布文件 . 发布apk不需要运行dev服务器 .

相关问题