更新 Target Sdk 版本 31 后我的 React Native 应用程序崩溃

ham*_*aca 9 android android-studio react-native react-native-reanimated

我的React Native应用程序在更新目标Sdk版本和compileSdkVersion 31后崩溃。它是工作版本30。Google Play强迫我们进行此更新。该应用程序在 Android 12 版本设备上崩溃。它适用于 Android 10 或 11。

我的 package.json 文件:

{
  "name": "app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "build:ios": "react-native bundle --entry-file='index.js' --bundle-output='./ios/main.jsbundle' --dev=false --platform='ios'",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@notifee/react-native": "^0.12.2",
    "@react-native-community/async-storage": "^1.9.0",
    "@react-native-community/checkbox": "^0.5.7",
    "@react-native-community/datetimepicker": "^3.0.3",
    "@react-native-community/masked-view": "^0.1.9",
    "@react-native-community/netinfo": "^9.3.6",
    "@react-native-community/picker": "^1.5.1",
    "@react-native-community/progress-bar-android": "^1.0.3",
    "@react-native-community/progress-view": "^1.2.1",
    "@react-native-community/push-notification-ios": "^1.4.1",
    "@react-native-firebase/app": "^8.4.1",
    "@react-native-firebase/messaging": "7.8.4",
    "axios": "^0.21.1",
    "date-fns": "^2.28.0",
    "moment": "^2.24.0",
    "react": "16.13.1",
    "react-native": "^0.64.4",
    "react-native-animated-pagination-dots": "^0.1.72",
    "react-native-autoheight-webview": "^1.6.1",
    "react-native-calendars": "^1.1263.0",
    "react-native-countdown-circle-timer": "^2.3.7",
    "react-native-directory-picker": "^0.0.2",
    "react-native-document-picker": "^5.0.0",
    "react-native-elements": "^2.1.0",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-gifted-chat": "^0.16.3",
    "react-native-image-picker": "3.2.1",
    "react-native-immersive-bars": "^1.0.1",
    "react-native-keyboard-aware-scroll-view": "^0.9.1",
    "react-native-month-year-picker": "^1.3.4",
    "react-native-paper": "^4.9.2",
    "react-native-pdf": "^6.2.2",
    "react-native-push-notification": "^5.1.0",
    "react-native-reanimated": "2.1.0",
    "react-native-redash": "^14.2.3",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-screens": "^2.5.0",
    "react-native-splash-screen": "^3.2.0",
    "react-native-svg": "^12.1.0",
    "react-native-svg-transformer": "^0.14.3",
    "react-native-swipe-list-view": "^3.2.3",
    "react-native-vector-icons": "^9.0.0",
    "react-native-video": "^4.4.5",
    "react-native-webview": "^11.23.1",
    "react-navigation": "^4.1.0",
    "react-navigation-drawer": "^2.3.4",
    "react-navigation-stack": "^2.0.16",
    "react-navigation-tabs": "^2.5.6",
    "react-redux": "^7.1.3",
    "redux": "^4.0.4",
    "redux-thunk": "^2.3.0",
    "rn-fetch-blob": "^0.12.0"
  },
  "devDependencies": {
    "@babel/core": "^7.11.1",
    "@babel/runtime": "^7.11.2",
    "@react-native-community/eslint-config": "^2.0.0",
    "babel-jest": "^26.2.2",
    "eslint": "^7.6.0",
    "jest": "^26.2.2",
    "metro-react-native-babel-preset": "^0.61.0",
    "react-test-renderer": "16.13.1"
  },
  "jest": {
    "preset": "react-native"
  }
}
Run Code Online (Sandbox Code Playgroud)

构建.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "30.0.2"
        minSdkVersion = 21
        compileSdkVersion = 31
        targetSdkVersion = 31
        ndkVersion = "23.1.7779620"
        androidXAnnotation = "1.1.0"
        androidXBrowser = "1.0.0"
        androidXCore = "1.0.2"
        firebaseMessagingVersion = "21.1.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.5.4")
        classpath 'com.google.gms:google-services:4.3.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}
Run Code Online (Sandbox Code Playgroud)

Moh*_*ast 7

两天前我也遇到了同样的问题。您必须进行以下更改:

文件:android/build.gradle

像这样更改这些版本:

buildscript {
ext {
    buildToolsVersion = "31.0.0"
    minSdkVersion = 21
    compileSdkVersion = 31
    targetSdkVersion = 31
}
Run Code Online (Sandbox Code Playgroud)

文件:android/app/build.gradle

添加implementation 'androidx.work:work-runtime-ktx:2.7.0'依赖。

dependencies {
...
    implementation 'androidx.work:work-runtime-ktx:2.7.0'
...
}
Run Code Online (Sandbox Code Playgroud)

编辑:截至 2022 年末,版本 2.7.1 有效,其他一些答案也建议使用 2.6.0,如果它们与您的系统兼容,您可以尝试。

`implementation 'androidx.work:work-runtime-ktx:2.7.1'`
Run Code Online (Sandbox Code Playgroud)

文件:android/app/src/main/AndroidManifest.xml

添加android:exported="true"到主要活动。

<activity
  android:name=".MainActivity"
  android:exported="true"
  ...
 >
Run Code Online (Sandbox Code Playgroud)

此外,您还需要添加android:exported="false"具有intent-filter类似服务和其他服务的每个 XML 标记。

例如,这是我的通知服务,intent-filter其子项为:

<receiver android:exported="false" android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

编辑:除此之外,请确保您使用的是 JDK 11

编辑:如果问题仍然存在,请删除 android 文件夹内的 .gradle 文件夹。

  • &gt; 安装的构建工具版本 31.0.0 已损坏。使用 SDK 管理器删除并重新安装。 (2认同)

小智 5

尝试在 build.gradle 的依赖项中添加此行

dependencies {
  // ...
  implementation 'androidx.work:work-runtime:2.7.1'
}
Run Code Online (Sandbox Code Playgroud)

也许问题就在这里: 目标 S+(版本 31 及更高版本)要求指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一