参考链接
ReactNative中文网
1.安装VisualStudioCode
2.运行VisualStudioCode
2.1 安装yarn、ReactNative命令行工具(react-native-cli)
Yarn是Facebook提供的替代npm的工具,可以加速node模块的下载。React Native的命令行工具用于执行创建、初始化、更新项目、运行打包服务(packager)等任务。
npm install -g yarn react-native-cli
2.2 安装JDK1.8或者以上
1) 去JDK官网下载JDK
2) 运行安装
3) 设置系统环境 JAVA_HOME 变量(重要),新建系统变量,将JDK路径传进去
2.3 创建ReactNative 项目
react-native init RNTest
2.4 配置Android环境变量
SET ANDROID_HOME=D:\Android-sdk-windows
SET PATH=%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%PATH%
2.5 编译 Android 项目并安装到模拟器上
cd RNTest && react-native run-android
2.6 编译 Android Release apk项目并安装到模拟器上
build_android_release.bat
CD /D %~dp0
SET ROOT=%CD%
SET ANDROID_HOME=D:\DevelopmentEnvironment\Softwares\Android\AndroidIDEA\android-sdk-windows
SET PATH=%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%PATH%
START "http://localhost:8081/" /D %ROOT% yarn run start
CHOICE /t 30 /d y /n >NUL
MKDIR %ROOT%\android\app\src\main\assets
curl -k "http://localhost:8081/index.android.bundle" >%ROOT%\android\app\src\main\assets\index.android.bundle
DEL /S /Q %ROOT%\android\app\build\outputs\apk\release\*.apk
START "gradlew assembleRelease" /D %ROOT%\android gradlew assembleRelease
2.7 签名配置android\gradle.properties(xxx.jks放在android\app\目录下)
MYAPP_RELEASE_STORE_FILE=XXX-signed-key(XXX).jks
MYAPP_RELEASE_KEY_ALIAS=XXX-signed-key
MYAPP_RELEASE_STORE_PASSWORD=XXX_XXX
MYAPP_RELEASE_KEY_PASSWORD=XXX_XXX
2.8 签名配置android\app\build.gradle
defaultConfig {
applicationId "com.rntest"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
signingConfigs {
release {
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
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}