A problem occurred evaluating project ‘:app‘. > Could not find meth ...

忿忿的泥巴坨  论坛元老 | 前天 23:46 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1909|帖子 1909|积分 5727

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个错误表明你的 build.gradle 文件中存在语法或配置题目。详细来说,Gradle 找不到 android() 方法,通常是由于 build.gradle 文件的布局不精确,大概缺少须要的插件声明。
以下是办理这个题目的步骤:

  • 查抄 build.gradle 文件布局
    你的 build.gradle 文件应该分为两个部门:
项目级别的 build.gradle(位于项目根目录)。
模块级别的 build.gradle(位于 app 目录或其他模块目录)。
项目级别的 build.gradle(build.gradle 文件)
  1. // 项目根目录下的 build.gradle
  2. buildscript {
  3.     repositories {
  4.         google()
  5.         mavenCentral()
  6.     }
  7.     dependencies {
  8.         classpath 'com.android.tools.build:gradle:8.2.0' // Android Gradle 插件版本
  9.     }
  10. }
  11. allprojects {
  12.     repositories {
  13.         google()
  14.         mavenCentral()
  15.     }
  16. }
  17. task clean(type: Delete) {
  18.     delete rootProject.buildDir
  19. }
复制代码
模块级别的 build.gradle(app/build.gradle 文件)
  1. // 模块目录下的 build.gradle
  2. plugins {
  3.     id 'com.android.application' // 应用插件
  4. }
  5. android {
  6.     namespace 'com.example.jianshen' // 命名空间
  7.     compileSdk 34 // 编译 SDK 版本
  8.     defaultConfig {
  9.         applicationId "com.example.jianshen" // 应用 ID
  10.         minSdk 28 // 最低支持的 SDK 版本
  11.         targetSdk 34 // 目标 SDK 版本
  12.         versionCode 1 // 版本号
  13.         versionName "1.0" // 版本名称
  14.         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" // 测试运行器
  15.     }
  16.     buildTypes {
  17.         release {
  18.             minifyEnabled false // 是否启用代码混淆
  19.             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  20.         }
  21.     }
  22. }
  23. dependencies {
  24.     implementation fileTree(dir: 'libs', include: ['*.jar']) // 本地库依赖
  25.     implementation 'androidx.appcompat:appcompat:1.6.1' // 依赖库
  26.     implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  27.     testImplementation 'junit:junit:4.13.2' // 测试依赖
  28.     androidTestImplementation 'androidx.test.ext:junit:1.1.5'
  29.     androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
  30. }
复制代码

  • 确保精确应用了 Android Gradle 插件
    在模块级别的 build.gradle 文件中,必须使用 plugins 块来应用 Android Gradle 插件。例如:
  1. plugins {
  2.     id 'com.android.application' // 应用插件
  3. }
复制代码
假如你使用的是旧版的 apply plugin 语法,请更换为 plugins 块。

  • 查抄 android 块的位置
    android 块必须位于模块级别的 build.gradle 文件中,而且必须在 plugins 块之后。例如:
  1. plugins {
  2.     id 'com.android.application'
  3. }
  4. android {
  5.     // 配置内容
  6. }
复制代码

  • 清算和重修项目
    完成上述修改后,清算项目并重新构建:
点击 Build > Clean Project。
点击 Build > Rebuild Project。

  • 查抄 Gradle 版本
    确保你的 Gradle 版本与 Android Gradle 插件版本兼容。打开 gradle-wrapper.properties 文件,查抄 distributionUrl 是否指向兼容的 Gradle 版本。例如:
  1. distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
复制代码

  • 查抄项目布局
    确保你的项目布局精确:
项目根目录下有一个 build.gradle 文件。
每个模块(如 app)目录下有一个 build.gradle 文件。

  • 完整示例
    以下是精确的 build.gradle 文件示例:
项目级别的 build.gradle(项目根目录)
  1. buildscript {
  2.     repositories {
  3.         google()
  4.         mavenCentral()
  5.     }
  6.     dependencies {
  7.         classpath 'com.android.tools.build:gradle:8.2.0'
  8.     }
  9. }
  10. allprojects {
  11.     repositories {
  12.         google()
  13.         mavenCentral()
  14.     }
  15. }
  16. task clean(type: Delete) {
  17.     delete rootProject.buildDir
  18. }
复制代码
模块级别的 build.gradle(app/build.gradle)
  1. plugins {
  2.     id 'com.android.application'
  3. }
  4. android {
  5.     namespace 'com.example.jianshen'
  6.     compileSdk 34
  7.     defaultConfig {
  8.         applicationId "com.example.jianshen"
  9.         minSdk 28
  10.         targetSdk 34
  11.         versionCode 1
  12.         versionName "1.0"
  13.         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  14.     }
  15.     buildTypes {
  16.         release {
  17.             minifyEnabled false
  18.             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  19.         }
  20.     }
  21. }
  22. dependencies {
  23.     implementation fileTree(dir: 'libs', include: ['*.jar'])
  24.     implementation 'androidx.appcompat:appcompat:1.6.1'
  25.     implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  26.     testImplementation 'junit:junit:4.13.2'
  27.     androidTestImplementation 'androidx.test.ext:junit:1.1.5'
  28.     androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
  29. }
复制代码
总结
确保 build.gradle 文件布局精确。
使用 plugins 块应用 Android Gradle 插件。
确保 android 块位于模块级别的 build.gradle 文件中。
清算和重修项目。
好的,关于这个题目我们今天就先分享到这里,希望能帮助到屏幕前为代码发愁的您。假如以为有帮助,希望能在淘宝搜索“鹿溪IT工作室”买一个Android小项目来练手,友友们给个好评,支持一下创作者不易
关注鹿溪IT工作室,后续我们会不定时分享新的bug修改意见,偶然候不一定全对,接待各人留言批评指正。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

忿忿的泥巴坨

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表