正如從 2017 跨 2018 一樣树姨,Android Studio 也要從 2+ 升級(jí)至 3+ 匿辩。
但是由于條件限制浑侥,不能及時(shí)升級(jí)的話级解,如何打開一個(gè) 3+ 項(xiàng)目贰锁?
我用 Picasso 來進(jìn)行直播一下窃祝,從 GitHub 導(dǎo)入到 Android Studio 后
原因:是由于項(xiàng)目編譯時(shí)所使用的 Gradle 插件更新至 3+ 了沛简,那么對(duì)應(yīng)的 Android Studio 也需要同步更新才行秽晚,是有匹配關(guān)系的呕缭。
首先堵泽,我很自然的打開了項(xiàng)目的全局 build.gradle 文件,想將 Gradle 插件版本降低成 2.3.3
插一嘴恢总,我們要分清楚什么是 Gradle 什么是 Gradle 插件迎罗,兩者不能混為一談。>>>不是很清楚的戳這里<<<
build.gradle 中依賴的 classpath 'com.android.tools.build:gradle:2.3.3'
是編譯所需要的 Gradle 插件版本(下載AS時(shí)會(huì)下載)
gradle-wrapper.properties 中的distributionUrl=https://services.gradle.org/distributions/gradle-2.10-all.zip
是 Android Studio 中使用了Gradle Wrapper 指定 Gradle 版本進(jìn)行項(xiàng)目構(gòu)建
想具體了解的也可以去全面熟悉一下 Gradle
發(fā)現(xiàn)結(jié)構(gòu)有點(diǎn)不對(duì)勁片仿,長得不一樣纹安,以前我們的項(xiàng)目該文件一般長這樣
// 依賴的插件
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
現(xiàn)在 Picasso 項(xiàng)目的 build.gradle 長這樣
subprojects {
buildscript {
repositories {
jcenter()
google()
}
}
repositories {
jcenter()
google()
}
plugins.apply('checkstyle')
task('checkstyle', type: Checkstyle) {
configFile rootProject.file('checkstyle.xml')
source 'src/main/java'
ignoreFailures false
showViolations true
include '**/*.java'
classpath = files()
}
group = GROUP
version = VERSION_NAME
afterEvaluate {
tasks.findByName('check').dependsOn('checkstyle')
}
}
apply from: rootProject.file('gradle/dependencies.gradle')
現(xiàn)在連 classpath 'com.android.tools.build:gradle:2.3.3'
都看不到了,一臉懵逼.jpg
傷心過度又眼力過好的我在最后一行瞟了一眼這個(gè)
apply from: rootProject.file('gradle/dependencies.gradle')
根據(jù)相對(duì)路徑找到該文件:
打開一看:
ext {
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
minSdkVersion = 14
targetSdkVersion = 26
sourceCompatibilityVersion = JavaVersion.VERSION_1_7
targetCompatibilityVersion = JavaVersion.VERSION_1_7
okhttpVersion = '3.6.0'
supportLibrariesVersion = '26.0.1'
dep = [
androidPlugin : 'com.android.tools.build:gradle:3.0.1',
okhttp : "com.squareup.okhttp3:okhttp:$okhttpVersion",
mockWebServer : "com.squareup.okhttp3:mockwebserver:$okhttpVersion",
pollexor : 'com.squareup:pollexor:2.0.4',
supportV4 : "com.android.support:support-v4:$supportLibrariesVersion",
supportAnnotations : "com.android.support:support-annotations:$supportLibrariesVersion",
junit : 'junit:junit:4.12',
truth : 'com.google.truth:truth:0.36',
robolectric : 'org.robolectric:robolectric:3.1',
mockito : 'org.mockito:mockito-core:1.9.5'
]
isCi = "true" == System.getenv('CI')
}
終于發(fā)現(xiàn)了
androidPlugin : 'com.android.tools.build:gradle:3.0.1'
那就將其改成我們熟悉的 2.3.3
編譯走起砂豌。厢岂。。噠噠噠阳距。塔粒。。
根據(jù)路徑找到 picasso moudle 下的 build.gradle 在 dependencies {} 中可找到 api
dependencies {
api dep.okhttp
compileOnly dep.supportAnnotations
testImplementation dep.junit
testImplementation dep.truth
testImplementation dep.supportV4
testImplementation dep.robolectric
testImplementation dep.mockito
testImplementation dep.mockWebServer
}
Google 一下便知 這是 Gradle 3.0.0 以上的編譯語法筐摘,我們還是要改成以前的窗怒,修改規(guī)則:
- api / compileOnly / implementation ——> compile
- testImplementation ——> testCompile
然后 Gradle 就會(huì)去下載所需的依賴了,再編譯就成功了蓄拣,噢耶扬虚!
如果還會(huì)出現(xiàn)其他錯(cuò)誤也可以根據(jù)提示,一步步來球恤,一些項(xiàng)目用到的依賴或者Support 版本是不是更新到最新的了辜昵,好好檢查下,一定可以成功跑起來咽斧。
例如檢查 dependencies.gradle 中的