Gradle DSL method not found:'compile()'

今天早上想着把之前看过的Material Design部分的内容在一个小demo上实现一下,可是添加Design support库时报错了,报错的内容是

特别纳闷,之前跟着第一行代码练的时候都好好的,这会怎么出错了。于是在网上找解决方法,现将各种方法整理如下:

第一种:报这种错的原因是:添加库的时候添加错位置了,一个项目里有两个build.gradle文件,一个在project下,一个在app下。正确的添加方式应该是在app/build.gradle里面添加所需要的依赖库,如果不慎添加在了project下,这时会报错,解决方法就是把需要添加的库添加到app/build.gradle下的dependencies闭包下(我自己在上面添加库的时候并没有犯这种错,我是在app/build.gradle中添加的库)。

第二种:网上说的改正方式在project下的build.gradle中allprojects闭包下添加maven,如下图所示:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }


    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
这也并不能解决问题。

第三种:网上说的改正方式在project下的build.gradle中allprojects闭包下的repositories闭包下添加google(),这种解决方式的前提是Android studio的版本是3.0。

问题的症结在于sdk的版本和相应的支持库版本是否一致,如果project/build.gradle里面classpath 是'com.android.tools.build:gradle:3.0.0',那么app/build.gradle里面的buildToolsVersion就应该是"26.0.2";

如果project/build.gradle里面classpath是 'com.android.tools.build:gradle:2.3.3',那么app/build.gradle里面的buildToolsVersion就应该是"25.0.2",并且添加依赖库的版本号应该和编译版本号一致,比如说要添加design support库,如果app/build.gradle中Android闭包下的compileSdkVersion 是25,那么下面的dependencies闭包里面支持库的版本号也应该25.多,具体是25.多少,Android studio会有提示,一般情况下跟上面的 appcompat的版本号一样;如果app/build.gradle中Android闭包下的compileSdkVersion 是26,那么下面的dependencies闭包里面支持库的版本号也应该26.多,具体是26.多少,Android studio会有提示,一般情况下跟上面的 appcompat的版本号一样。

解决之后我自己project/build.gradle文件为:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
app/build.gradle文件为:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "sun.example.music"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.4.0'
    compile 'com.android.support:design:25.4.0'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}
一般情况下不要更改project/build.gradle文件,使用系统默认的就行。

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值