反应本地构建错误:包android.support.annotation不存在

时间:2019-06-19 12:05:45

标签: android react-native

我不得不完全重写问题。

我有一个React native android应用。当我使用apk构建./gradlew assembleRelease -x bundleReleaseJsAndAssets文件时,它运行良好,但此后它完全停止编译。甚至react-native run-android也无法正常工作。

到目前为止,我发现了什么: 首先,错误是这样

Task :app:processDebugResources FAILED
resource android:attr/fontVariationSettings not found.
resource android:attr/ttcIndex not found.

如果我将这些行添加到gradle.properties

android.useAndroidX=true
android.enableJetifier=true

错误发生变化。现在就是这个

Task :@JWWon_react-native-universal-pedometer:compileDebugJavaWithJavac FAILED

error: package android.support.annotation does not exist
import android.support.annotation.Nullable;
                                 ^
cannot find symbol
  private void sendPedometerUpdateEvent(@Nullable WritableMap params) {
                                         ^
  symbol:   class Nullable
  location: class BMDPedometerModule

问题不在于库。如果我从项目中删除它,它将开始抱怨另一个项目。要使其编译,我必须删除7个库。一个例子:

Task :@react-native-community_netinfo:compileDebugJavaWithJavac FAILED
error: package android.support.v4.net does not exist
import android.support.v4.net.ConnectivityManagerCompat;
error: cannot find symbol
    promise.resolve(ConnectivityManagerCompat.isActiveNetworkMetered(getConnectivityManager()));
                    ^
  symbol:   variable ConnectivityManagerCompat
  location: class ConnectivityReceiver
2 errors

然后如果我删除另一个,则会发生这种情况:

Task :react-native-camera-kit:compileDebugJavaWithJavac FAILED
package android.support.annotation does not exist
import android.support.annotation.ColorInt;
                                 ^
package android.support.annotation does not exist
import android.support.annotation.IntRange;
                                 ^
...
92 errors

因此,如果我从项目中删除7个库,它将进行编译。他们是:

react-native-camera-kit @react-native-community_netinfo react-native-push-notification react-native-sensors @JWWon_react-native-universal-pedometer react-native-keep-awake react-native-toast-native

没有它们,它可以完美地编译。因此,有一个更大的问题使它无法正常工作。 2天前,所有这些库均正常运行,没有问题。但是现在有些东西压垮了它。

7 个答案:

答案 0 :(得分:6)

我实际上发生了非常相似的事情,并且运行了它

npx jetify

当我通过CI管道运行它时,它不起作用,并最终不得不添加

"scripts": {
 ...
    "postinstall": "jetify"
}

npm在管道中运行安装后,然后运行jetify转换为androidx并涵盖所有需要转换的库。

答案 1 :(得分:5)

尝试使用喷射器

npm install --save-dev jetifier (or use yarn, but install it locally in your project, not globally) npx jetify or npx jetify -w=1 (to specify the number of parallel workers) npx react-native run-android

答案 2 :(得分:3)

尝试使用androidx

// build.gradle 
implementation "androidx.annotation:annotation:1.1.0"

// where use it
import androidx.annotation.Nullable;

更新:

如果其他库出错,也许您可​​以尝试jetifier, 我知道这个有用的issue

下面是完整的参考,希望对您有帮助:)

// android/build.gradle
ext {
    buildToolsVersion = "28.0.3"
    minSdkVersion = 24
    compileSdkVersion = 28
    targetSdkVersion = 28
    supportLibVersion = "1.0.0-beta01"
}

// app/build.gradle
dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "androidx.core:core:1.0.2"
    implementation "androidx.annotation:annotation:1.1.0"
    implementation "androidx.appcompat:appcompat:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:0.59.9"  // From node_modules
}

顺便说一句,我遇到了这个问题(AndroidX)了几天,最后通过更新react-native@0.59.9来解决,使用最新的android设置和神奇的jetifier

答案 3 :(得分:2)

allprojects {
  repositories {
      bla bla bla...
  }
  subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "26.+"
            }
            if (details.requested.group == 'com.google.android.gms'
            && !details.requested.name.contains('multidex') && !details.requested.name.contains('play-services-stats')) {
                details.useVersion "12.+"
            }
            if (details.requested.group == 'com.google.android.gms'
            && !details.requested.name.contains('multidex') && details.requested.name.contains('play-services-stats')) {
                details.useVersion "+"
            }
        }
    }

}

在build.gradle(android)中添加子项目

dependencies {
...bla bla bla

implementation "com.google.android.gms:play-services-gcm:12.+"

}

在build.gradle(android / app)中添加实现“ com.google.android.gms:play-services-gcm:12。+”

,因此您无需迁移到Androidx

答案 4 :(得分:0)

添加

android.useAndroidX=true
android.enableJetifier=true

在gradle.properties中

答案 5 :(得分:0)

// build.gradle

实现“androidx.annotation:annotation:1.1.0”

// 用在什么地方

导入 androidx.annotation.Nullable;

在出现错误的所有地方从 androidx 导入丢失的类。

答案 6 :(得分:-1)

好的。过去四天的屁股很痛,但我现在可以正常工作了。

我做了什么:

已添加

android.useAndroidX=true android.enableJetifier=true 到gradle.properties

将SDK更改为28

compileSdkVersion 28 buildToolsVersion '28.0.3'

并删除了一个库react-native-camera-kit。现在至少可以全部编译。我猜Jetifier不能与此库一起使用。现在,我必须将其关闭。