導入其餘人的項目對於我們開發人員來講是一個基礎,可有些時候導入了10分鐘20分鐘甚至更久死活一直導不進來。這裏就教你們一個輕鬆便捷的導入方法,順便跟你們探討一下導入程序的正確姿式。java
拿到項目以後先不着急打開導入,我們先查看一下根目錄的build.gradle文件查看。react
完整的註釋步驟以下:android
apply from: "dependencies.gradle"
buildscript {
repositories {
jcenter()
// 第二步,註釋
// google()
}
dependencies {
// 第一步,修改版本號
classpath 'com.android.tools.build:gradle:2.2.2'
// 第三步 ,註釋不相關的版本發佈
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
// 第二步,註釋
// google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
複製代碼
注意,必定要注意不要輕易刪除某些東西。git
#Thu May 25 13:52:26 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
複製代碼
我這裏使用的2.14.1,大家直接改爲大家對應版本便可。github
到這裏最主要且必須的兩步都作完了,下面就能夠導入項目了,導入以後仍是會報各類錯誤,不要着急我們慢慢來。api
由於Google在3.0的時候廢棄了compile,改成implementation和api了,具體區別能夠自行百度。若是你要導入的項目是3.X,而你的版本是Android Studio 2.X,那麼就須要把全部implementation改回compile。bash
如如下:
3.0以前
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.3'
3.0以後
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.3'
除以上以外,還有如下:
androidTestCompile(前)
androidTestImplementation(後)
testCompile(前)
testImplementation (後)
複製代碼
若是你的項目是從Github上下載的,你須要註釋掉做者發版的信息。這裏你能夠對比你以前能夠運行的項目的build.gradle文件,把不相關的文件通通註釋掉,注意依然是註釋哦。app
到這裏就能夠build一下了,可能還會有報錯,哪裏報錯我們再接着修改哪裏。maven
這裏是由於你的sdk版本低於27版本,簡單粗暴的方法是直接修改爲你的sdk版本便可。對於該類錯誤,可參考Failed to resolve:com.android.support:appcompat-v7:報錯處理(圖片也來源於此博客)gradle
當使用maven導入依賴時,可能會出現不一樣的依賴包含了同一個jar,在編譯期間不會報錯。可一旦run app的時候,就會報
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: xxx.class
複製代碼
解決的方法是在引入依賴的時候把相同的jar去除掉。具體能夠參照Android Studio 中如何解決重複依賴致使的app:transformClassesWithJarMergingForDebug
這是由於 compileSdkVersion 和buildToolsVersion 的版本不對應,因此纔會出現這種問題,解決辦法就是須要改爲對應的版本。具體參照In FontFamilyFont, unable to find attribute android:font的報錯處理
具體錯誤是:
Warning:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (3.0.1) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
複製代碼
解決辦法是在項目的app根目錄中build.gradle中加入
android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
複製代碼
這是我目前總結的關於導入項目時的方法和錯誤,但願你們能夠積極留言說下本身導包的姿式和補充本身遇到的問題。若是對你們有幫助,還但願你們點贊支持或留言。