如何从根本上修复错误"Could not find method compile() for arguments [directory 'libs']"?



我在看了所有类似的问题和答案后发布了这个问题。

这是我学习的问题。

找不到compile()方法的参数Gradle

Gradle找不到compile()方法

也许,你可能会怀疑这是一个重复的问题,但在我的情况下,它不是。让我们看看是怎么回事。首先,下面是产生错误的代码片段:

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:+"
}

这段代码来自文件:node_modules/react-native-geocoder/android/build.gradle

现在让我给你看看出现了什么错误。

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

所有的答案都说你需要用implementation()方法代替compile()方法,因为compile()方法从gradle 7.0弃用,目前我正在使用gradle 7.4。但编辑文件在node_modules文件夹不是一个好主意,大家都知道。它是react-native项目,问题的包是react-native-geocoder。我浏览了react-native-geocoder repo,但它是由其所有者实现的,现在是只读的。所以我不能提交PR到repo。

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

我愿意讨论更明智的答案。解决这个问题的根本答案是什么?谢谢你!

react-native-geocoder编译问题:

你可以通过在node_modules/react-native-geocoder/android/build.gradle中将compile替换为implementation来解决这个问题。

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

更改为

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

编译方法已从gradle 4.0弃用,并从gradle 7.0完全删除

这可以通过添加:

来解决/绕过
subprojects { subproject ->
if(project['name'] == 'react-native-geocoder'){
project.configurations { compile { } }
}
}

到项目的构建。gradle文件。

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

Muhammad解决方案是正确的,只是有一个问题,当你运行yarn安装和/或删除node_modules库将回到以前的状态,你将不得不再次改变它,为了防止这种情况,你需要使用patch-package:

完成修改后,您需要在终端上运行此命令:

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

的例子:

npx patch-pacakge react-native-geocoder

这将在patch文件夹中创建一个文件,你需要提交这个文件。

下次删除node_modules或运行yarn install时,库将返回到以前的状态,因此您需要应用补丁,您只需:

npx patch-package

请注意,这次我没有添加库名,这是因为你可能有几个不同库的补丁,而这将应用所有的库。

超越:如果您不希望每次运行yarn install时都要记住执行npx patch-package,那么可以在package.jsonscripts部分中添加此命令,如下所示:

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

相关内容

最新更新