15

I am posting the question after having a look at all similar questions and answers.

Here are the questions I studied.

Could not find method compile() for arguments Gradle

Gradle Could not find method compile() for arguments

Maybe, you might wonder that it's a duplicate question but in my case, it's not. Let's see how it is. First, here is the code snippet that the error comes from:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile "com.facebook.react:react-native:+"
}

This code is from the file: node_modules/react-native-geocoder/android/build.gradle

Now let me show you what error shows up.

FAILURE: Build failed with an exception.

* Where:
Build file '/Project-root/node_modules/react-native-geocoder/android/build.gradle' line: 19

* What went wrong:
A problem occurred evaluating project ':react-native-geocoder'.
> Could not find method compile() for arguments [directory 'libs'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 26s

All answers say that you need to replace compile() method with implementation() method because compile() method is deprecated from gradle 7.0 and currently I am using gradle 7.4. But editing files inside node_modules folder is not a good idea as everybody knows. And it's react-native project and the package in issue is react-native-geocoder. I browsed react-native-geocoder repo but it's achieved by its owner and read-only now. So I cannot submit PR to the repo.

https://github.com/devfd/react-native-geocoder

I would like to discuss about any wiser answer. What is a fundamental answer to fix this issue? Thank you!

VecopWall
  • 549
  • 1
  • 7
  • 23
  • You should configure /android/app/build.gradle' and NOT the /android/build.gradle'. Also compile method has been deprecated from gradle 4.0 and completely removed from gradle 7.0 so use implementation instead of compile. – Ritesh Kumar Mar 24 '23 at 02:05

3 Answers3

36

react-native-geocoder compile issue:

You can fix this issue by replacing compile with implementation in node_modules/react-native-geocoder/android/build.gradle.

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.facebook.react:react-native:+"
}

changed to

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "com.facebook.react:react-native:+"
}

Compile method has been deprecated from gradle 4.0 and completely removed from gradle 7.0

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Muhammad Haidar
  • 1,541
  • 16
  • 17
  • That's what I already found. But editing node_modules will raise the issue once I or someone else reinstall the node_modules. Right? So I am seeking for another solution. – VecopWall May 24 '22 at 14:37
  • 1
    @VecopWall see `patch-package` to customize packages locally; it is ideal for situations like this. – dylan oliver Aug 07 '22 at 16:59
6

This can be resolved / bypassed by adding:

subprojects { subproject ->
    if(project['name'] == 'react-native-geocoder'){
        project.configurations { compile { } }
    }
}

to your project's build.gradle file.

Reference: https://github.com/software-mansion/react-native-reanimated/issues/3242#issuecomment-1145423942

instanceof
  • 1,404
  • 23
  • 30
2

Muhammad solution is correct, there's just a problem, when you run yarn install and/or delete node_modules the library is going to go back to its previous state and you will have to change it again, to prevent this you need to use patch-package:

After doing the modification you need to run this on your terminal:

npx patch-pacakge name-of-the-library-you-modified

Example:

npx patch-pacakge react-native-geocoder

This is going to create a file inside the patch folder, you need to commit this file.

The next time you delete node_modules or run yarn install, the library is going to return to its previous state so you need to apply the patches, you do this with just:

npx patch-package

Notice I didn't add the library name this time, this is because you could have several patches for different libraries and this is going to apply all of them.

Going beyond: If you don't want to have to remember to do npx patch-package each time you run yarn install, you can add this command in the scripts section of your package.json, like this:

 **package.json file**

 {
  "name": "MyApp",
  "version": "1.0.0",
  ...
  "scripts": {
      "postinstall": "patch-package",
     ...
  }
  ...
 }