先说几个需要更改的地方:
1. 根目录下的“build.gradle”文件
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”文件
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=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"
...
}