【问题标题】:react native build error: package android.support.annotation does not exist反应原生构建错误:包android.support.annotation不存在
【发布时间】:2019-06-19 12:05:45
【问题描述】:

我不得不完全重写问题。

我有一个 react 原生的 android 应用。当我用./gradlew assembleRelease -x bundleReleaseJsAndAssets 构建apk 文件时,它运行良好,但之后它就完全停止编译了。甚至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 天前,所有这些库都运行良好,没有任何问题。但现在有什么东西把它压碎了。

【问题讨论】:

    标签: android react-native


    【解决方案1】:

    试试jetifier

    npm install --save-dev jetifier
    或者使用yarn,但是在你的项目中本地安装,而不是全局安装

    npx jetify

    npx jetify -w=1 - 指定并行工作人员的数量

    npx react-native run-android

    【讨论】:

    • 这些步骤有效。我使用 yarn add jetifier 然后 yarn jetify 和 react-native run-android
    【解决方案2】:

    我实际上发生了一些非常相似的事情并且运行它有效

    npx jetify
    

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

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

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

    【讨论】:

    • 我的救命稻草
    • 只是 npx jetify 对我来说就足够了,谢谢
    【解决方案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

    【讨论】:

    • 我试过这个。我认为它有效,但我的项目仍然无法编译,另一个库出现另一个错误。我将此添加到问题中
    • 据我了解,它修复了错误。但是因为现在我使用的是 Android X,所以另一个库不再工作了
    • 我升级到 react-native 0.59.9,然后尝试了其他东西。所以我通过从项目中取消链接 5 个库来让它工作。我更新了问题。如果我尝试将其链接回来,它就不再起作用了。你能查一下吗?
    • 我认为您可以选择更改库或分叉它。至少,@react-native-community_netinfo react-native-push-notification react-native-keep-awake,这三个库在jetifier 进程之后可以完美运行:)
    • 太棒了,我的旧代码import android.support.annotation.Nullable;,更改为import androidx.annotation.Nullable;。现在可以使用了
    【解决方案4】:
    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

    编辑1:代码格式

    编辑 2:缺少括号

    【讨论】:

    • 感谢您的回复。我正在寻找没有 android x 的解决方案。我试过你的建议。它从现在开始找到资源,但是现在库 react-native-push-notification 存在问题。我猜它需要另一个版本的 gcm 并崩溃。你能告诉我如何解决这个问题吗?
    • 试试这个pastebin.com/MdKfhPYG resolutionStrategy.eachDependency 块的变化
    • 这成功了!非常感谢你,伙计!我很感激。上帝保佑你!
    【解决方案5】:

    // build.gradle

    实现“androidx.annotation:annotation:1.1.0”

    //在哪里使用

    导入 androidx.annotation.Nullable;

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

    【讨论】:

      【解决方案6】:

      添加

      android.useAndroidX=true
      android.enableJetifier=true
      

      在 gradle.properties 中

      【讨论】:

        【解决方案7】:

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

        我做了什么:

        添加

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

        将 sdk 更改为 28

        compileSdkVersion 28 buildToolsVersion '28.0.3'

        并删除了一个库react-native-camera-kit。至少现在一切都编译好了。我猜 Jetifier 不适用于这个库。现在我必须把它关掉。

        【讨论】:

          猜你喜欢
          • 2020-03-13
          • 1970-01-01
          • 2016-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-05
          • 1970-01-01
          相关资源
          最近更新 更多