先说几个需要更改的地方:
1. 根目录下的“build.gradle”文件
- 【buildscript】闭包和【allprojects】闭包的【repositories】块中添加“google()”
- 【buildscript】闭包中的【dependencies】块中的“classpath”里将gradle版本修改为对应版本,如3.1.3
buildscript {
repositories {
google() //此处需添加“google()”
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3' // 旧版本,修改前
classpath 'com.android.tools.build:gradle:3.1.3' // 新版本,修改后
}
}
allprojects {
repositories {
google() //此处需添加“google()”
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2. module目录下的“build.gradle”文件
- 删除【android】闭包下的“buildToolsVersion”代码行。Android Studio3.0之后不需要再指定buildToolsVersion,每个版本的Android Gradle插件都有默认版本的构建工具,不删除只会报警告,但是这句指定构建版本的语句会被忽略
- 【dependencies】闭包中的关键词作如下修改:
① “compile” 替换为 “implementation” ; “api”
② “testCompile” 替换为 “testImplementation” ; “testApi”
③ “androidTestCompile” 替换为 “androidTestImplementation” “androidTestApi”
④ “androidTestApi” 替换为 “androidTestImplementation”
⑤ “testApi” 替换为 “testImplementation”
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3" // 删掉该行
defaultConfig {
applicationId "com.dandelion.www.dandelion"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { // “androidTestCompile”改为“androidTestImplementation”
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:27.1.1' // “compile”改为“implementation”
testImplementation 'junit:junit:4.12' // “testCompile”改为“testImplementation”
}
3. 根目录下的 "gradle--wrapper" 文件夹下的 "gradle-wrapper.properties文件"
- 修改 "distributionUrl" 的地址
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
注意:多数教程中都没有提及这个文件,如果不改此处的地址,编译报错
Gradle DSL method not found: 'google()' Possible causes: The project 'app' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
1. grade配置Error: Could not find com.android.tools.build:gradle:2.2.1
解决方法:
在根目录下(与app同级)的“build.gradle”文件,找到:
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
修改为:
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
}
2. Minimum supported Gradle version is 4.4. Current version is 3.3.
直接点击其下给出的解决方案:
Please fix the project's Gradle settings.
Fix Gradle wrapper and re-import project //点击该行修复
Gradle settings
3. All flavors must now belong to a named flavor dimension.
在module下的“build.gradle”中添加一行代码即可解决:
android{
...
flavorDimensions "versionCode"
...
}