首页 > 解决方案 > 导入 android.app.NotificationChannel 错误

问题描述

尝试编译我的 android React-native 应用程序时出现以下错误。

    :react-native-push-notification:compileReleaseJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
/tmp/workspace/my-develop/my_app/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java:7: error: cannot find symbol
import android.app.NotificationChannel;
                  ^
  symbol:   class NotificationChannel
  location: package android.app
/tmp/workspace/my-develop/my_app/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java:480: error: cannot find symbol
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
                                                       ^
  symbol:   variable O
  location: class VERSION_CODES
/tmp/workspace/my-develop/my_app/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java:488: error: cannot find symbol
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
                                            ^
  symbol:   variable IMPORTANCE_DEFAULT
  location: class NotificationManager
/tmp/workspace/my-develop/my_app/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java:489: error: cannot find symbol
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
        ^
  symbol:   class NotificationChannel
  location: class RNPushNotificationHelper
/tmp/workspace/my-develop/my_app/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java:489: error: cannot find symbol
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
                                          ^
  symbol:   class NotificationChannel
  location: class RNPushNotificationHelper
Note: /tmp/workspace/my-develop/my_app/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/helpers/ApplicationBadgeHelper.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
5 errors
:react-native-push-notification:compileReleaseJavaWithJavac FAILED

我读过各种帖子说这个问题可以通过设置来解决compileSdkVersion 26。但是我已经把这个设置为26. 我还没有看到任何其他可能导致此问题的建议。

标签: androidreact-native

解决方案


安卓/build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
}

安卓/应用程序/build.gradle:

compileSdkVersion 26
buildToolsVersion "26.0.2"
   ...
defaultConfig {
    ...
    minSdkVersion 16
    targetSdkVersion 22
    ...
}

在 react-native-push-notification/android/build.gradle 里面

 dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'  //-----> Changed this from 2.1.3

}

def DEFAULT_COMPILE_SDK_VERSION = 27

def DEFAULT_BUILD_TOOLS_VERSION = "27.0.3"

定义 DEFAULT_TARGET_SDK_VERSION = 26

def DEFAULT_SUPPORT_LIB_VERSION = "26.+"

def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "+"

def DEFAULT_FIREBASE_MESSAGING_VERSION = "+"


推荐阅读