React Native打包发布记录

开发环境

Mac

打包目标

安卓APK

资料

https://reactnative.cn/docs/signed-apk-android
https://www.cnblogs.com/librarookie/p/16364384.html

第1步,生成签名文件

随便找个文件夹(就是用来放签名文件的地方,不要放项目里),打开终端执行:
keytool -genkeypair -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
出现如下步骤:

输入密钥库口令:  //示例:123456
再次输入新口令:  
您的名字与姓氏是什么:  //示例:kay
您的组织单位名称是什么:  //示例:HG科技
您的组织名称是什么:
您所在的城市或区域名称是什么:
该单位的双字母国家/地区代码是什么:   //示例: CN

填写完后,这个文件夹下会生成一个my-release-key.keystore文件。

第2步,设置 gradle 变量
  1. 把刚生成的my-release-key.keystore文件放到你工程中的android/app文件夹下。
  2. 编辑~/.gradle/gradle.properties(全局配置,对所有项目有效)或是项目目录/android/gradle.properties(项目配置,只对所在项目有效)。如果没有gradle.properties文件你就自己创建一个,添加如下的代码(注意把其中的****替换为相应密码)
MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=*****    //示例:123456
MYAPP_RELEASE_KEY_PASSWORD=*****    //示例:123456
第3步,把签名配置加入到项目的 gradle 配置中

编辑android/app/build.gradle文件,找到signingConfigs配置的地方,添加如下代码:

...
android {
    ...
    defaultConfig { ... }
    signingConfigs {
        ...
        /** 新增部分 **/
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
        /** 新增部分 **/
    }
    buildTypes {
        release {
            ...
            /** 修改部分 **/
            signingConfig signingConfigs.release
            /** 修改部分 **/
        }
    }
}
...
第4步,生成发行APK包

工程项目文件打开终端,执行:

cd android
./gradlew assembleRelease

在 macOS、Linux 或是 windows 的 PowerShell 环境中表示执行当前目录下的名为 gradlew 的脚本文件,且其运行参数为 assembleRelease,注意这个./不可省略;而在 windows 的传统 CMD 命令行下则需要去掉./。

注意:请确保 gradle.properties 中没有包含org.gradle.configureondemand=true,否则会跳过 js 打包的步骤,导致最终生成的是一个无法运行的空壳。

生成的 APK 文件位于android/app/build/outputs/apk/release/app-release.apk,它已经可以用来发布了。

遇到的问题

执行第4步报错:

Task :react-native-async-storage_async-storage:generateReleaseRFile FAILED

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':react-native-async-storage_async-storage:generateReleaseRFile'.
> Could not resolve all files for configuration ':react-native-async-storage_async-storage:releaseCompileClasspath'.
   > Failed to transform react-native-0.71.0-rc.0-release.aar (com.facebook.react:react-native:0.71.0-rc.0) to match attributes {artifactType=android-symbol-with-package-name, com.android.build.api.attributes.BuildTypeAttr=release, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-api}.
      > Could not find react-native-0.71.0-rc.0-release.aar (com.facebook.react:react-native:0.71.0-rc.0).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/com/facebook/react/react-native/0.71.0-rc.0/react-native-0.71.0-rc.0-release.aar

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
java.lang.StackOverflowError (no error message)

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5.1/userguide/command_line_interface.html#sec:command_line_warnings

参考: https://www.jianshu.com/p/2932334e4114、https://blog.csdn.net/junhuahouse/article/details/126880806
找到android/app/build.gradle里的 implementation "com.facebook.react:react-native:+"改成implementation "com.facebook.react:react-native:0.70.2"
但还是会报错,尝试将依赖包里相应的build.gradle也改掉。但这样很麻烦,要改的依赖包太多了。
找到android/build.gradle,添加如下代码,就可以解决该问题。再次编译,OK!

/* 添加代码 */
def REACT_NATIVE_VERSION = new File(['node','--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())
/* 添加代码 */

buildscript {
  ...
}

allprojects {
    repositories {
        ...
    }

    /* 添加代码 */
    //解决编译出错: Failed to transform react-native-0.71.0-rc.0-release.aar
    configurations.all {
        resolutionStrategy {
            // Remove this override in 0.70.2, as a proper fix is included in react-native itself.
            force"com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }
    /* 添加代码 */
}

ps: Apk分享给测试人员,可以用蒲公英

你可能感兴趣的:(React Native打包发布记录)