我创建了一个 React Native 应用程序,它在除 Android 13 之外的所有版本上都能顺利运行。我已经尝试了在其他 StackOverflow 帖子上找到的解决方案,但没有取得任何结果。我的package.json文件如下:
{
"name": "aloha-live",
"version": "1.0.0",
"private": true,
"scripts": {
"android": "react-native run-android --variant=productiondebug --appId com.alohachat && npx react-native start && emulator -avd emulator-5554 && adb reverse tcp:8081 tcp:8081",
"ios": "react-native run-ios ",
"start": "react-native start",
"ios:beta": "react-native run-ios --scheme 'AlohaLiveStaging' --configuration 'StagingDebug'",
"test": "jest",
"lint": "eslint --ext src/*/.{js,ts,tsx}",
"clean": "cd android && ./gradlew clean && rm -rf ~/.gradle/caches/build-cache-* && ./gradlew --stop && cd .. && watchman watch-del-all && rm -rf node_modules/ && rm -rf $TMPDIR/react-native-packager-cache-* && rm -rf $TMPDIR/metro-bundler-cache-* && yarn cache clean && yarn install && cd ios && rm -rf Podfile.lock && rm -rf Pods/ && pod install && cd .. && yarn start --reset-cache",
"metro-clean": "watchman watch-del-all && rm -rf $TMPDIR/react-native-packager-cache-* && rm -rf $TMPDIR/metro-bundler-cache-* && yarn cache clean && yarn start --reset-cache",
"cache-clean": "yarn start --reset-cache",
"postinstall": "patch-package",
"prettier:write": "yarn prettier --write src/*/.{js,ts,tsx,json}",
"package-check": "npm-check -i @aloha/* -p",
"package-check:dev": "npm-check -i @aloha/* -d",
"build-android:apk": "cd android && ./gradlew clean && rm -rf ~/.gradle/caches/build-cache-* && ./gradlew --stop && watchman watch-del-all && ./gradlew assembleProductionRelease && ./gradlew --stop",
"build-android-market": "cd android && ./gradlew bundleProductionRelease"
},
"dependencies": {
"@eva-design/eva": "^2.1.1",
"@invertase/react-native-apple-authentication": "^2.2.2",
"@ptomasroos/react-native-multi-slider": "^2.2.2",
"@react-native-clipboard/clipboard": "1.11.2",
"@react-native-community/datetimepicker": "7.0.0",
"@react-native-firebase/analytics": "16.7.0",
"@react-native-firebase/app": "16.7.0",
"@react-native-firebase/auth": "16.7.0",
"@react-native-firebase/crashlytics": "16.7.0",
"@react-native-firebase/database": "16.7.0",
"@react-native-firebase/messaging": "16.7.0",
"@react-native-firebase/perf": "16.7.0",
"@react-native-google-signin/google-signin": "^10.0.1",
"@react-native-masked-view/masked-view": "0.2.9",
"@react-navigation/bottom-tabs": "6.5.7",
"@react-navigation/drawer": "6.6.2",
"@react-navigation/elements": "1.3.17",
"@react-navigation/native": "6.1.6",
"@react-navigation/stack": "6.3.16",
"@reduxjs/toolkit": "1.9.3",
"@rnhooks/keyboard": "^1.1.0",
"@sentry/react-native": "^5.15.2",
"@ui-kitten/components": "^5.1.2",
"@ui-kitten/eva-icons": "^5.1.2",
"axios": "1.3.4",
"deprecated-react-native-prop-types": "^4.0.0",
"expo": "^49.0.21",
"expo-linear-gradient": "^12.5.0",
"expo-modules-core": "^1.5.12",
"expo-screen-orientation": "5.1.1",
"false": "^0.0.4",
"i18next": "22.4.13",
"lodash": "^4.17.21",
"lottie-ios": "3.4.0",
"lottie-react-native": "5.1.4",
"moment": "2.29.4",
"moment-duration-format": "^2.3.2",
"prop-types": "^15.8.1",
"react": "18.1.0",
"react-i18next": "12.2.0",
"react-native": "0.70.0",
"react-native-adjust": "4.33.0",
"react-native-agora": "4.1.2",
"react-native-bootsplash": "4.5.3",
"react-native-code-push": "7.0.5",
"react-native-codegen": "^0.70.7",
"react-native-config": "1.5.0",
"react-native-device-info": "10.5.1",
"react-native-dialog": "^9.2.2",
"react-native-fast-image": "8.6.3",
"react-native-fbsdk-next": "^11.2.1",
"react-native-flash-message": "0.4.1",
"react-native-gesture-handler": "2.9.0",
"react-native-gifted-chat": "1.1.0",
"react-native-google-mobile-ads": "10.0.1",
"react-native-gradle-plugin": "^0.71.19",
"react-native-hyperlink": "^0.0.22",
"react-native-inappbrowser-reborn": "^3.7.0",
"react-native-keyboard-aware-scroll-view": "^0.9.5",
"react-native-localize": "2.2.6",
"react-native-mmkv": "2.5.1",
"react-native-modal": "^13.0.1",
"react-native-modal-datetime-picker": "14.0.1",
"react-native-pager-view": "6.2.0",
"react-native-permissions": "3.8.0",
"react-native-purchases": "5.13.1",
"react-native-reanimated": "2.14.4",
"react-native-restart": "^0.0.27",
"react-native-safe-area-context": "4.5.0",
"react-native-screens": "^3.29.0",
"react-native-snackbar": "^2.4.0",
"react-native-snap-carousel": "^3.9.1",
"react-native-store-review": "^0.3.0",
"react-native-svg": "^13.10.0",
"react-native-swipe-list-view": "^3.2.9",
"react-native-tab-view": "3.5.1",
"react-native-vector-icons": "9.2.0",
"react-native-walkthrough-tooltip": "^1.5.0",
"react-redux": "8.0.5",
"redux": "4.2.1",
"sharp": "^0.33.1",
"socket.io-client": "4.6.1"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/plugin-proposal-decorators": "7.20.0",
"@babel/plugin-syntax-typescript": "7.20.0",
"@babel/preset-typescript": "7.18.6",
"@babel/runtime": "^7.12.5",
"@react-native-community/eslint-config": "^2.0.0",
"@sentry/types": "^7.45.0",
"@types/eslint": "^8.4.6",
"@types/jest": "29.2.0",
"@types/lodash": "^4.14.191",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"@types/react-native": "^0.71.0",
"@types/react-native-snap-carousel": "^3.8.5",
"@types/react-redux": "7.1.24",
"@types/react-test-renderer": "^17.0.1",
"@typescript-eslint/eslint-plugin": "5.41.0",
"@typescript-eslint/parser": "5.41.0",
"@ui-kitten/metro-config": "^5.1.2",
"babel-jest": "^26.6.3",
"babel-plugin-module-resolver": "^4.1.0",
"babel-plugin-transform-inline-environment-variables": "0.4.4",
"babel-plugin-transform-remove-console": "^6.9.4",
"eslint": "8.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-config-universe": "^11.1.0",
"eslint-import-resolver-typescript": "^3.5.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.0.1",
"eslint-plugin-react": "^7.31.8",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-standard": "^5.0.0",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "0.72.3",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.6.2",
"pretty-quick": "^3.1.3",
"react-test-renderer": "18.1.0",
"ts-jest": "29.0.5",
"typescript": "4.8.4"
},
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
},
"expo": {
"autolinking": {
"exclude": [
"react-native-reanimated"
]
}
}
}
我的 android/app/build.gradle 文件如下:
project.ext.envConfigFiles = [
productiondebug: ".env.production",
productionrelease: ".env.production",
developmentrelease: ".env.development",
developmentdebug: ".env.development",
stagingrelease: ".env.staging",
stagingdebug: ".env.staging",
]
apply plugin: "com.android.application"
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.firebase.firebase-perf'
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
import com.android.build.OutputFile
import org.apache.tools.ant.taskdefs.condition.Os
/**
* The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
* and bundleReleaseJsAndAssets).
* These basically call react-native bundle with the correct arguments during the Android build
* cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
* bundle directly from the development server. Below you can see all the possible configurations
* and their defaults. If you decide to add a configuration block, make sure to add it before the
* apply from: "../../node_modules/react-native/react.gradle" line.
*
* project.ext.react = [
* // the name of the generated asset file containing your JS bundle
* bundleAssetName: "index.android.bundle",
*
* // the entry file for bundle generation. If none specified and
* // "index.android.js" exists, it will be used. Otherwise "index.js" is
* // default. Can be overridden with ENTRY_FILE environment variable.
* entryFile: "index.android.js",
*
* // https://reactnative.dev/docs/performance#enable-the-ram-format
* bundleCommand: "ram-bundle",
*
* // whether to bundle JS and assets in debug mode
* bundleInDebug: false,
*
* // whether to bundle JS and assets in release mode
* bundleInRelease: true,
*
* // whether to bundle JS and assets in another build variant (if configured).
* // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
* // The configuration property can be in the following formats
* // 'bundleIn${productFlavor}${buildType}'
* // 'bundleIn${buildType}'
* // bundleInFreeDebug: true,
* // bundleInPaidRelease: true,
* // bundleInBeta: true,
*
* // whether to disable dev mode in custom build variants (by default only disabled in release)
* // for example: to disable dev mode in the staging build type (if configured)
* devDisabledInStaging: true,
* // The configuration property can be in the following formats
* // 'devDisabledIn${productFlavor}${buildType}'
* // 'devDisabledIn${buildType}'
*
* // the root of your project, i.e. where "package.json" lives
* root: "../../",
*
* // where to put the JS bundle asset in debug mode
* jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
*
* // where to put the JS bundle asset in release mode
* jsBundleDirRelease: "$buildDir/intermediates/assets/release",
*
* // where to put drawable resources / React Native assets, e.g. the ones you use via
* // require('./image.png')), in debug mode
* resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
*
* // where to put drawable resources / React Native assets, e.g. the ones you use via
* // require('./image.png')), in release mode
* resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
*
* // by default the gradle tasks are skipped if none of the JS files or assets change; this means
* // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
* // date; if you have any other folders that you want to ignore for performance reasons (gradle
* // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
* // for example, you might want to remove it from here.
* inputExcludes: ["android/*", "ios/*"],
*
* // override which node gets called and with what additional arguments
* nodeExecutableAndArgs: ["node"],
*
* // supply additional arguments to the packager
* extraPackagerArgs: []
* ]
*/
project.ext.react = [
enableHermes: true, // clean and rebuild if changing
]
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/@sentry/react-native/sentry.gradle"
/**
* Set this to true to create two separate APKs instead of one:
* - An APK that only works on ARM devices
* - An APK that only works on x86 devices
* The advantage is the size of the APK is reduced by about 4MB.
* Upload all the APKs to the Play Store and people will download
* the correct one based on the CPU architecture of their device.
*/
def enableSeparateBuildPerCPUArchitecture = false
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = true
/**
* The preferred build flavor of JavaScriptCore.
*
* For example, to use the international variant, you can use:
* def jscFlavor = 'org.webkit:android-jsc-intl:+'
*
* The international variant includes ICU i18n library and necessary data
* allowing to use e.g. Date.toLocaleString and String.localeCompare that
* give correct results when using with locales other than en-US. Note that
* this variant is about 6MiB larger per architecture than default.
*/
def jscFlavor = 'org.webkit:android-jsc:+'
/**
* Whether to enable the Hermes VM.
*
* This should be set on project.ext.react and that value will be read here. If it is not set
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
* and the benefits of using Hermes will therefore be sharply reduced.
*/
def enableHermes = project.ext.react.get("enableHermes", false);
/**
* Architectures to build native code for.
*/
def reactNativeArchitectures() {
def value = project?.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}
android {
ndkVersion rootProject.ext.ndkVersion
// compileSdkVersion rootProject.ext.compileSdkVersion
compileSdkVersion 33
buildToolsVersion "33.0.0"
// aarMetadata {
// artifactId = "my-application"
// groupId = "com.example"
// version = "1.0"
// }
defaultConfig {
applicationId "com.alohachat"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 106
versionName "5.0.0"
multiDexEnabled true
resValue "string", "build_config_package", "com.alohachat"
resValue 'string', "CODE_PUSH_APK_BUILD_TIME", String.format("\"%d\"", System.currentTimeMillis())
configurations.all {
resolutionStrategy { force 'androidx.work:work-runtime:2.6.0' }
}
//react-native-image-crop-picker
vectorDrawables.useSupportLibrary = true
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
//react-native-iap
missingDimensionStrategy 'store', 'play'
if (isNewArchitectureEnabled()) {
// We configure the CMake build only if you decide to opt-in for the New Architecture.
externalNativeBuild {
cmake {
arguments "-DPROJECT_BUILD_DIR=$buildDir",
"-DREACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
"-DREACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
"-DNODE_MODULES_DIR=$rootDir/../node_modules",
"-DANDROID_STL=c++_shared"
}
}
if (!enableSeparateBuildPerCPUArchitecture) {
ndk {
abiFilters (*reactNativeArchitectures())
}
}
}
}
if (isNewArchitectureEnabled()) {
// We configure the NDK build only if you decide to opt-in for the New Architecture.
externalNativeBuild {
cmake {
path "$projectDir/src/main/jni/CMakeLists.txt"
}
}
def reactAndroidProjectDir = project(':ReactAndroid').projectDir
def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
into("$buildDir/react-ndk/exported")
}
def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
into("$buildDir/react-ndk/exported")
}
afterEvaluate {
// If you wish to add a custom TurboModule or component locally,
// you should uncomment this line.
// preBuild.dependsOn("generateCodegenArtifactsFromSchema")
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
// Due to a bug inside AGP, we have to explicitly set a dependency
// between configureCMakeDebug* tasks and the preBuild tasks.
// This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
configureCMakeRelWithDebInfo.dependsOn(preReleaseBuild)
configureCMakeDebug.dependsOn(preDebugBuild)
reactNativeArchitectures().each { architecture ->
tasks.findByName("configureCMakeDebug[${architecture}]")?.configure {
dependsOn("preDebugBuild")
}
tasks.findByName("configureCMakeRelWithDebInfo[${architecture}]")?.configure {
dependsOn("preReleaseBuild")
}
}
}
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include (*reactNativeArchitectures())
}
}
signingConfigs {
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
}
}
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
// minifyEnabled true
// proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
//minifyEnabled enableProguardInReleaseBuilds
// minifyEnabled true
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
/* Add the firebaseCrashlytics extension (by default,
* it's disabled to improve build speeds) and set
* nativeSymbolUploadEnabled to true along with a pointer to native libs. */
firebaseCrashlytics {
nativeSymbolUploadEnabled true
unstrippedNativeLibsDir 'build/intermediates/merged_native_libs/release/out/lib'
}
}
}
flavorDimensions "default"
productFlavors {
production {
resValue "string", "CodePushDeploymentKey", '""'
resValue 'string', 'deep_link_url', 'app.alohalive.online'
resValue 'string', 'deep_link_scheme', 'alohalive'
}
staging {
applicationIdSuffix ".staging"
resValue "string", "CodePushDeploymentKey", '"TzWaC1KOII946zujOXUsbV_9VQtbq19XVGads"'
resValue 'string', 'deep_link_url', 'app.alohalive.online'
resValue 'string', 'deep_link_scheme', 'alohalive'
}
}
// 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)
}
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
implementation 'androidx.multidex:multidex:2.0.1'
implementation "androidx.core:core-splashscreen:1.0.0"
//play store billing
implementation 'com.android.billingclient:billing:5.0.0'
//adjust
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
implementation 'com.android.installreferrer:installreferrer:2.2'
implementation project(':react-native-config')
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
exclude group:'com.facebook.fbjni'
}
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
exclude group:'com.facebook.flipper'
exclude group:'com.squareup.okhttp3', module:'okhttp'
}
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
exclude group:'com.facebook.flipper'
}
if (enableHermes) {
//noinspection GradleDynamicVersion
implementation("com.facebook.react:hermes-engine:+") { // From node_modules
exclude group:'com.facebook.fbjni'
}
} else {
implementation jscFlavor
}
}
if (isNewArchitectureEnabled()) {
// If new architecture is enabled, we let you build RN from source
// Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
// This will be applied to all the imported transtitive dependency.
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute(module("com.facebook.react:react-native"))
.using(project(":ReactAndroid"))
.because("On New Architecture we're building React Native from source")
substitute(module("com.facebook.react:hermes-engine"))
.using(project(":ReactAndroid:hermes-engine"))
.because("On New Architecture we're building Hermes from source")
}
}
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.implementation
into 'libs'
}
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
def isNewArchitectureEnabled() {
// To opt-in for the New Architecture, you can either:
// - Set newArchEnabled to true inside the gradle.properties file
// - Invoke gradle with -newArchEnabled=true
// - Set an environment variable ORG_GRADLE_PROJECT_newArchEnabled=true
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}
我测试了在 Android 13 上创建的 APK,但应用程序抛出错误。它向用户显示一条消息,指出应用程序存在问题
有谁知道可能导致此问题的原因以及如何解决它?
尝试将 android:exported="true" 添加到 AndroidManifest.xml 文件中。
以下是如何将 android:exported="true" 属性添加到 AndroidManifest.xml 文件中的活动的示例:
<activity
android:name=".YourActivity"
android:exported="true">
<!-- other activity attributes -->
</activity>