由于 SDK_INT 和 VERSION_CODES.P 无法构建 Android APK - 那是什么?

and*_*ini 1 java android gradle

这两天我试图发布我的 Android 版 React Native 应用程序,但遇到了以下错误:

> Task :react-native-device-info:compileReleaseJavaWithJavac FAILED
/Users/myuser/project/node_modules/react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java:368: error: cannot find symbol
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                                                      ^
  symbol:   variable P
  location: class VERSION_CODES
/Users/myuser/project/node_modules/react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java:370: error: cannot find symbol
        locationEnabled = mLocationManager.isLocationEnabled();
                                          ^
  symbol:   method isLocationEnabled()
  location: variable mLocationManager of type LocationManager
Note: /Users/myuser/project/node_modules/react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors

FAILURE: Build failed with an exception.
Run Code Online (Sandbox Code Playgroud)

我的project/android/build.gradle文件是:

buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 23
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
        playServicesVersion = "16.0.0"
    }
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath 'com.google.gms:google-services:4.0.1'
    }
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url "https://maven.google.com" }
        mavenLocal()
        google()
        maven {
            url "$rootDir/../node_modules/react-native/android"
        }
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        jcenter()
    }
}

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "27.1.1"
            }
        }
    }
    afterEvaluate {
        android {
            compileSdkVersion 27
            buildToolsVersion "27.0.3"

            defaultConfig {
                targetSdkVersion 27
            }
        }
    }
}

ext {
    buildToolsVersion = "27.0.3"
    minSdkVersion = 16
    compileSdkVersion = 27
    targetSdkVersion = 27
    supportLibVersion = "27.1.0"
}
Run Code Online (Sandbox Code Playgroud)

另外,我的 Android Studio 下载了以下组件:

  • API 27
  • API 28
  • Android SDK 构建工具 29
  • 安卓模拟器
  • Android SDK平台工具
  • Android SDK工具
  • 支持存储库

我尝试了几次清理gradle缓存,但没有成功。我是java新手,不知道如何处理这个问题。

任何想法?


将 CompiledSdkVersion 更改为 28 后,现在出现以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all files for configuration ':app:releaseCompileClasspath'.
> Could not resolve android.arch.lifecycle:viewmodel:1.1.0.
  Required by:
      project :app
   > Cannot find a version of 'android.arch.lifecycle:viewmodel' that satisfies the version constraints:
        Dependency path 'MobileApp:app:unspecified' --> 'com.android.support:appcompat-v7:28.0.0' --> 'com.android.support:support-fragment:28.0.0' --> 'android.arch.lifecycle:viewmodel:1.1.1'
        Constraint path 'MobileApp:app:unspecified' --> 'android.arch.lifecycle:viewmodel' strictly '1.1.0' because of the following reason: releaseRuntimeClasspath uses version 1.1.0
        Constraint path 'MobileApp:app:unspecified' --> 'android.arch.lifecycle:viewmodel' strictly '1.1.0' because of the following reason: releaseRuntimeClasspath uses version 1.1.0
        Constraint path 'MobileApp:app:unspecified' --> 'android.arch.lifecycle:viewmodel' strictly '1.1.0' because of the following reason: releaseRuntimeClasspath uses version 1.1.0

> Could not find com.android.support:collections:27.1.1.
  Required by:
      project :app > com.android.support:appcompat-v7:28.0.0
      project :app > com.android.support:support-compat:28.0.0
> Could not find com.android.support:cursoradapter:27.1.1.
  Required by:
      project :app > com.android.support:appcompat-v7:28.0.0
> Could not find com.android.support:loader:27.1.1.
  Required by:
      project :app > com.android.support:support-fragment:28.0.0
      project :app > com.android.support:support-core-utils:28.0.0
> Could not resolve android.arch.lifecycle:viewmodel:1.1.1.
  Required by:
      project :app > com.android.support:support-fragment:28.0.0
   > Cannot find a version of 'android.arch.lifecycle:viewmodel' that satisfies the version constraints:
        Dependency path 'MobileApp:app:unspecified' --> 'com.android.support:appcompat-v7:28.0.0' --> 'com.android.support:support-fragment:28.0.0' --> 'android.arch.lifecycle:viewmodel:1.1.1'
        Constraint path 'MobileApp:app:unspecified' --> 'android.arch.lifecycle:viewmodel' strictly '1.1.0' because of the following reason: releaseRuntimeClasspath uses version 1.1.0
        Constraint path 'MobileApp:app:unspecified' --> 'android.arch.lifecycle:viewmodel' strictly '1.1.0' because of the following reason: releaseRuntimeClasspath uses version 1.1.0
        Constraint path 'MobileApp:app:unspecified' --> 'android.arch.lifecycle:viewmodel' strictly '1.1.0' because of the following reason: releaseRuntimeClasspath uses version 1.1.0

> Could not find com.android.support:versionedparcelable:27.1.1.
  Required by:
      project :app > com.android.support:support-compat:28.0.0
> Could not find com.android.support:documentfile:27.1.1.
  Required by:
      project :app > com.android.support:support-core-utils:28.0.0
> Could not find com.android.support:localbroadcastmanager:27.1.1.
  Required by:
      project :app > com.android.support:support-core-utils:28.0.0
> Could not find com.android.support:print:27.1.1.
  Required by:
      project :app > com.android.support:support-core-utils:28.0.0
Run Code Online (Sandbox Code Playgroud)

更新3

更改为后,details.useVersion28.0.0现在收到此错误:

> Task :app:bundleReleaseJsAndAssets FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Failed to capture fingerprint of input files for task ':app:bundleReleaseJsAndAssets' property '$1' during up-to-date check.
> Could not list contents of '/Users/myuser/myproject/node_modules/metro/node_modules/jest-haste-map/node_modules/.bin/sane'. Couldn't follow symbolic link.
Run Code Online (Sandbox Code Playgroud)

ian*_*ake 6

您的compileSdkVersion值为 27,但 Android P 的 API 为 28。compileSdkVersion如果您想使用 Android P 特定的 API(例如Build.VERSION_CODES.P仅在 Android P 中引入的常量),则需要将您的值设置为 28。

根据这篇博文,您应该始终寻求使用最新的 SDK 进行编译(无论您是否使用该 API 级别的任何功能)。

在迁移到 AndroidX之前,如果您使用的支持库版本与您的compileSdkVersion. 您可以忽略它,也可以升级到28.0.0,这是迁移到 AndroidX 之前的最后一个版本。