【问题标题】:minSdk compatibility error setting up react-native-notifications@2.1.3 with react-native 0.60.0minSdk 兼容性错误设置 react-native-notifications@2.1.3 与 react-native 0.60.0
【发布时间】:2019-12-10 22:59:19
【问题描述】:

我正在尝试向 react-native 项目添加推送通知系统,并且尝试使用 react-native-notifications 2.1.3 版。

我的 package.json 文件如下所示:

"dependencies": {
    "@react-native-community/async-storage": "^1.6.3",
    "jetifier": "^1.6.4",
    "react": "16.8.6",
    "react-native": "0.60.0",
    "react-native-camera": "^3.11.1",
    "react-native-elements": "^1.2.7",
    "react-native-gesture-handler": "^1.5.1",
    "react-native-notifications": "^2.1.3",
    "react-native-permissions": "^2.0.3",
    "react-native-qrcode-scanner": "^1.3.1",
    "react-native-vector-icons": "^6.6.0",
    "react-navigation": "^1.0.0-beta.27",
    "react-redux": "^5.1.2",
    "redux": "^4.0.4",
    "redux-thunk": "^2.3.0",
    "rn-fetch-blob": "^0.10.16"
  },
  "devDependencies": {
    "@babel/core": "7.5.0",
    "@babel/runtime": "7.5.0",
    "@react-native-community/eslint-config": "0.0.3",
    "babel-jest": "24.8.0",
    "eslint": "6.0.1",
    "jest": "24.8.0",
    "metro-react-native-babel-preset": "0.54.1",
    "react-test-renderer": "16.8.6"
  },
  "jest": {
    "preset": "react-native"
  }
}

我的项目级 build.gradle(android/build.gradle) 看起来像

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.4.1")

        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()
    }
}
subprojects { subproject ->
    afterEvaluate {
        if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                variantFilter { variant ->
                    def names = variant.flavors*.name
                    if (names.contains("reactNative59")) {
                        setIgnore(true)
                    }
                }
            }
        }
    }
}

react-native-notifications 版本 2.1.3 android/build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:4.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

react-native-notifications android/app/build.gradle

apply plugin: 'com.android.library'
apply from: '../prepare-robolectric.gradle'

android {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
    testOptions {
        unitTests.all { t ->
            reports {
                html.enabled true
            }
            testLogging {
                events "PASSED", "SKIPPED", "FAILED", "standardOut", "standardError"
            }
            afterSuite { desc, result ->
                if (!desc.parent) { // will match the outermost suite
                    def output = "      ${result.resultType} (${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)     "
                    def repeatLength = output.length()
                    println '\n\n' + ('-' * repeatLength) + '\n' + output + '\n' + ('-' * repeatLength) + '\n'

                    println "see report at file://${t.reports.html.destination}/index.html"
                }
            }
        }
    }

    flavorDimensions "RNNotifications.reactNativeVersion"
    productFlavors {
        reactNative59 {
            dimension "RNNotifications.reactNativeVersion"
        }
        reactNative60 {
            dimension "RNNotifications.reactNativeVersion"
        }
    }
}

dependencies {
    implementation "com.google.firebase:firebase-messaging:17.3.0"
    implementation 'com.facebook.react:react-native:+'

    // tests
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:3.5.1'
    testImplementation 'org.assertj:assertj-core:3.8.0'
    testImplementation 'com.squareup.assertj:assertj-android:1.1.1'
    testImplementation 'org.mockito:mockito-core:2.13.0'
}

MainApplication.java 片段:

protected List<ReactPackage> getPackages() {
      @SuppressWarnings("UnnecessaryLocalVariable")
      List<ReactPackage> packages = new PackageList(this).getPackages();
      // Packages that cannot be autolinked yet can be added manually here, for example:
      // packages.add(new MyReactNativePackage());
      return packages;
    }

取自 PackageList.java(MainApplication getPackages() 返回的内容)

public ArrayList<ReactPackage> getPackages() {
    return new ArrayList<>(Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      new AsyncStoragePackage(),
      new RNCameraPackage(),
      new RNGestureHandlerPackage(),
      new RNPermissionsPackage(),
      new VectorIconsPackage(),
      new RNFetchBlobPackage()
    ));
  }

我知道上面的大部分代码都不需要,但我提供了以防我错过了什么。

我的问题是我的项目的 minSDK 版本为 16,但 react-native-notifications 的 minSDK 版本为 19。尝试构建时,我得到以下输出:

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:react-native-notifications] Project-Root\node_modules\react-native-notifications\an
droid\app\build\intermediates\library_manifest\reactNative60Debug\AndroidManifest.xml as the library might be using APIs not available in 16
        Suggestion: use a compatible library with a minSdk of at most 16,
                or increase this project's minSdk version to at least 19,
                or use tools:overrideLibrary="com.wix.reactnativenotifications" to force usage (may lead to runtime failures)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s

    at checkExecSyncError (child_process.js:621:11)
    at execFileSync (child_process.js:639:15)
    at runOnAllDevices (Project-Root\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:75:39)
    at buildAndRun (Project-Root\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\index.js:137:41)
    at Project-Root\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\index.js:103:12
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Command.handleAction (Project-Root\node_modules\react-native\node_modules\@react-native-community\cli\build\cliEntry.js:160:7)

我在网上查找了解决方案,但我尝试将 react-native-notifications minSDK 版本下移至 16,但未成功。

当我尝试将项目的 minSDK 更改为 19 时,我得到了

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more de
tails.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
C:\Users\nvashakidze\dev\BRAND-NEW\Micromeritics-Final\android\app\build\generated\rncli\src\main\java\com\facebook\react\PackageList.java:66: error: constructor RNNotificationsPackage in class RNNotificationsPackage cannot be applied
 to given types;
      new RNNotificationsPackage(),
      ^
  required: Application
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

    标签: android reactjs react-native gradle


    【解决方案1】:

    您必须传递应用程序上下文。作为参考检查这个https://github.com/wix/react-native-notifications/blob/master/docs/installation.md#-android

    【讨论】:

    • 这真的没有帮助。我对使用 react 还很陌生,但是该文档中有关 android 的说明看起来像手动链接过程,我认为 react 0.60.0 已将其丢弃。
    • 忽略我之前的评论。我忘记包含传递上下文的 MainApplication.java 文件代码。它抛出了这个错误,因为我需要手动链接库。
    【解决方案2】:

    我找到了解决办法。

    我将项目的 minSDKVersion 更改为 19,手动链接库

    react-native link react-native-notifications
    

    然后我在项目的根目录中创建了一个新文件 (react-native.config.js),其中仅包含

    module.exports = {
      dependencies: {
        'react-native-notifications': {
          platforms: {
            android: null
          },
        },
     },
    }
    

    感谢 GitHub 用户 msantang78 https://github.com/wix/react-native-notifications/issues/396

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2021-01-07
      相关资源
      最近更新 更多