Home > OS >  Could not find method compile() error react native version .68
Could not find method compile() error react native version .68

Time:06-03

I upgraded my project with using React Native Upgrade Helper from 0.67.4 to 0.68.0 And I saw that Gradle versions upgraded, too.

When I try to run ./gradlew clean command in ./android file, it gives me that compile error.

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.

To check it, I created new react native project with 0.68.2 version. On first run with npm run android command, application worked as expected. But after I install react-native-i18n package;

npm install react-native-i18n --save

and run ./gradlew clean command again, it gave me same error again.

I replaced compile with implementation on ./node_modules folder and it fixed the error. But if I have lots of packages, should I replace all of packages' build.gradle files manually?

Root build.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
        }
...

gradle/wrapper/gradle-wrapper.properties file:

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

Package.json:

...
"dependencies": {
    "react": "17.0.2",
    "react-native": "0.68.2",
    "react-native-i18n": "^2.0.15"
  }
...

Thanks.

CodePudding user response:

This issue can be fixed by editing the node modules of react-native-i18n package.

  1. Go to node_modules/react-native-i18n/android/build.gradle
  2. Replace compile "com.facebook.react:react-native: " into implementation "com.facebook.react:react-native: "
  3. Save the file and run react-native run-android

You can use patch-package to make and persist changes to node modules.

This can be done by first making changes to the package inside node_modules and then running the following command, with being the name of the package you just made changes to.

npx patch-package patch-package will then create a patches folder with a file inside, representing your changes. This file can then be commited to git, and patches can be restored later by running npx patch-package (without any arguments).

Optional step:

Add the following in the script section of your package.json to automatically patch the dependency when you execute "npm install".

"postinstall": "npx patch-package"

CodePudding user response:

Change the code in that file error.

api Replace compile

  • Related