找不到方法compile()错误反应本机版本.68+

Cen*_*kit 3 gradle react-native

我使用React Native Upgrade Helper将我的项目从 0.67.4 升级到 0.68.0 并且我看到 Gradle 版本也升级了。

当我尝试./gradlew clean在 ./android 文件中运行命令时,它给出了编译错误。

A problem occurred evaluating project ':react-native-i18n'.
> Could not find method compile() for arguments [com.facebook.react:react-native:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Run Code Online (Sandbox Code Playgroud)

为了检查它,我创建了 0.68.2 版本的新 React Native 项目。第一次使用 npm run android 命令运行时,应用程序按预期工作。但是在我安装 react-native-i18n包之后;

npm install react-native-i18n --save

再次运行./gradlew clean命令,它再次给了我同样的错误。

我将编译替换为 ./node_modules 文件夹上的实现,它修复了错误。但是如果我有很多包,我应该手动替换所有包的 build.gradle 文件吗?

根构建.gradle:

...
dependencies {
            classpath("com.android.tools.build:gradle:7.0.4")
            classpath("com.facebook.react:react-native-gradle-plugin")
            classpath("de.undercouch:gradle-download-task:4.1.2")
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
...
Run Code Online (Sandbox Code Playgroud)

gradle/wrapper/gradle-wrapper.properties 文件:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Run Code Online (Sandbox Code Playgroud)

包.json:

...
"dependencies": {
    "react": "17.0.2",
    "react-native": "0.68.2",
    "react-native-i18n": "^2.0.15"
  }
...
Run Code Online (Sandbox Code Playgroud)

谢谢。

小智 11

这个问题可以通过编辑react-native-i18n包的节点模块来解决。

  1. 转到node_modules/react-native-i18n/android/build.gradle
  2. 编译“com.facebook.react:react-native:+” 替换为 实现“com.facebook.react:react-native:+”
  3. 保存文件并运行react-native run-android

您可以使用 patch-package 对节点模块进行并保留更改。

这可以通过首先对 node_modules 内的包进行更改,然后运行以下命令来完成,其中命令是您刚刚更改的包的名称。

然后npx patch-package patch-package 将创建一个补丁文件夹,其中包含一个文件,代表您的更改。然后可以将该文件提交到 git,并且稍后可以通过运行 npx patch-package (不带任何参数)来恢复补丁。

可选步骤:

在 package.json 的脚本部分添加以下内容,以便在执行“npm install”时自动修补依赖项。

"postinstall": "npx 补丁包"