我的应用在发布模式下启动后立即崩溃,但在调试模式下使用时正常工作。以下是构建apk时所做的事情:
- 创建一个密钥库文件并将其添加到
build.gradle
文件中 cd android && ./gradlew clean && ./gradlew assembleRelease
build.gradle
:
def enableProguardInReleaseBuilds = true // proguard is enabled
.
.
.
android {
ndkVersion rootProject.ext.ndkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.wallet"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release {
if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
storeFile file(MYAPP_UPLOAD_STORE_FILE)
storePassword MYAPP_UPLOAD_STORE_PASSWORD
keyAlias MYAPP_UPLOAD_KEY_ALIAS
keyPassword MYAPP_UPLOAD_KEY_PASSWORD
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
if (nativeArchitectures) {
ndk {
abiFilters nativeArchitectures.split(',')
}
}
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// https://developer.android.com/studio/build/configure-apk-splits.html
// Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
defaultConfig.versionCode * 1000 + versionCodes.get(abi)
}
}
}
}
package.json
:
{
"name": "wallet",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.15.14",
"@react-native-clipboard/clipboard": "^1.8.5",
"@react-native-community/blur": "^3.6.0",
"@react-native-community/masked-view": "^0.1.11",
"@react-native-community/slider": "^4.1.12",
"@react-navigation/bottom-tabs": "^6.0.9",
"@react-navigation/native": "^6.0.6",
"@react-navigation/stack": "^6.0.11",
"axios": "^0.24.0",
"moment": "^2.29.1",
"moment-jalaali": "^0.9.4",
"moment-timezone": "^0.5.34",
"react": "17.0.2",
"react-native": "0.66.4",
"react-native-animatable": "^1.3.3",
"react-native-device-info": "^8.4.8",
"react-native-elements": "^3.4.2",
"react-native-flags": "^1.0.0",
"react-native-gesture-handler": "^2.1.0",
"react-native-keyboard-aware-scroll-view": "^0.9.5",
"react-native-linear-gradient": "^2.5.6",
"react-native-modal": "^13.0.0",
"react-native-network-info": "^5.2.1",
"react-native-reanimated": "^2.3.1",
"react-native-restart": "^0.0.23",
"react-native-safe-area-context": "^3.3.2",
"react-native-screens": "^3.10.1",
"react-native-splash-screen": "^3.3.0",
"react-native-svg": "^12.1.1",
"react-native-toast-message": "^2.1.1",
"react-native-touch-id": "^4.4.1",
"react-native-vector-icons": "^9.0.0",
"react-redux": "^7.2.6",
"redux": "^4.1.2",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.4.1",
"reselect": "^4.1.5"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/runtime": "^7.12.5",
"@react-native-community/eslint-config": "^2.0.0",
"babel-jest": "^26.6.3",
"eslint": "7.14.0",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "^0.66.2",
"react-test-renderer": "17.0.2"
},
"jest": {
"preset": "react-native"
}
}
proguard-rules.pro
:
-keep class com.swmansion.reanimated.** { *; } // Added these two lines because of what they said in reanimated 2 documentation
-keep class com.facebook.react.turbomodule.** { *; }
我运行了adb logcat -d > logs.txt
命令来获取日志列表:
--------- beginning of crash
01-11 15:34:02.883 4969 4969 E AndroidRuntime: FATAL EXCEPTION: main
01-11 15:34:02.883 4969 4969 E AndroidRuntime: Process: com.wallet, PID: 4969
01-11 15:34:02.883 4969 4969 E AndroidRuntime: java.lang.IllegalStateException: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.swmansion.reanimated.layoutReanimation.NativeMethodsHolder.isLayoutAnimationEnabled()' on a null object reference
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.bridge.ReactContext.handleException(Unknown Source:74)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.bridge.ReactContext$ExceptionHandlerWrapper.handleException(Unknown Source:2)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.bridge.GuardedRunnable.run(Unknown Source:7)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:883)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:100)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at android.os.Looper.loop(Looper.java:224)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7562)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.swmansion.reanimated.layoutReanimation.NativeMethodsHolder.isLayoutAnimationEnabled()' on a null object reference
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.swmansion.reanimated.layoutReanimation.AnimationsManager.isLayoutAnimationEnabled(Unknown Source:2)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.swmansion.reanimated.layoutReanimation.ReaLayoutAnimator.isLayoutAnimationEnabled(Unknown Source:5)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.swmansion.reanimated.layoutReanimation.ReanimatedNativeHierarchyManager.manageChildren(Unknown Source:11)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.uimanager.z0$m.b(Unknown Source:14)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.uimanager.z0$a.run(Unknown Source:135)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.uimanager.z0.U(Unknown Source:53)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.uimanager.z0.s(Unknown Source:0)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.uimanager.z0$b.runGuarded(Unknown Source:2)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: at com.facebook.react.bridge.GuardedRunnable.run(Unknown Source:0)
01-11 15:34:02.883 4969 4969 E AndroidRuntime: ... 7 more
显然,Reanimated
是引起问题的原因。但是我该如何解决呢?非常感谢。
proguard
,问题就解决了。但是应用程序在开始时崩溃可能有很多原因。您应该尝试使用adb logcat
并找到与您的应用程序相关的日志。有一次我没有使用proguard
就遇到了这个问题,后来我将reanimated 2
更新到最新版本后问题就解决了。但是您应该先执行adb logcat
并查看是什么导致了问题。 - Ardalan