1、生成签名密钥
执行命令生成密钥文件my-release-key.keystore
keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
将它复制到项目的android\app文件夹下(需要记住你设置的“口令”这一项)
2、build.gradle配置
在app文件夹下的build.gradle文件里找到signingConfigs,在debug下面添加
debug {...}release {storeFile file('my-release-key.keystore')storePassword '你的口令'keyAlias 'my-key-alias'keyPassword '你的口令'}
(如果打包失败提示找不到key 'my-key-alias' 那么就是keyAlias不对,cmd到my-release-key.keystore所在路径执行
keytool -list -v -keystore my-release-key.keystore -storepass 对应的密码,查看对应'别名')
再找到buildTypes,在release里面添加
signingConfig signingConfigs.release
找到enableProguardInReleaseBuilds将它设置为true(减小包体积,但打包时间会很长,去除不引用包)
enableProguardInReleaseBuilds = true
3、package.json添加打包
"bundle-android": "cd ./android && ./gradlew assembleRelease","bundle-win-android": "cd android && gradlew assembleRelease","bundle-ios": "node node_modules/react-native/local-cli/cli.js bundle --entry-file index.js --platform ios --dev false --bundle-output ./ios/main.jsbundle --bundle-encoding utf8 --assets-dest ./ios",//如果不想配置的话官网有个办法可以直接打包cd androidgradlew assembleRelease