1. 程式人生 > >使用Android Studio 3.0以及新版Gradle轉移舊版專案可能遇到的幾個坑

使用Android Studio 3.0以及新版Gradle轉移舊版專案可能遇到的幾個坑

好久沒有更新部落格了,年底忙的要死,先記錄一下轉移專案需要注意的幾點。

1.一般轉移專案sdk路徑一般不一樣,這時候AS會提示更改,點選“OK”即可,這個比較簡單。


2.提示專案的Gradle沒有配置,是否要下載最新的Gradle,一般使用我們安裝新版AS時已經有了,不用再次下載了,所以點選取消。


然後就會讓你選擇Gradle地址,一般在:AS安裝目錄/gradle/gradle-xx



3.完成上面兩步之後可能會出現:

Error:Cause: com.android.build.gradle.tasks.factory.AndroidJavaCompile.setDependencyCacheDir(Ljava/io/File;)V
Consult IDE log for more details (Help | Show Log)



4.此時開啟build.gradle點選重新同步後,可能會出現:

Error:The versions of the Android Gradle plugin and Gradle are not compatible.
Please do one of the following:<ul><li>Update your plugin to version 2.4. This will require changes to build.gradle due to API changes.


此時需要修改 build.gradle的com.android.tools.build:gradle版本到你第二步所選擇的gradle所支援的版本(如果不知道的到底支援什麼版本:1. 那就改2.3.0以上的,然後滑鼠懸停在這行程式碼上,此時AS會提示最新版是多少,可以選擇根據提示修改。2.檢視你能正確執行的專案的版本,把這句程式碼copy過來即可)

dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3' //2.3.0
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

5.還有可能出現這個視窗


也就是叫你更新gradle外掛,你可以選擇更新,但是如果你使用的三方包太多,又不知道他們支不支援最新版的,可以選擇不再提醒。

6.最後執行的時候,如果你有用註解框架,還可可能提示:

Error:Execution failed for task ':Library:javaPreCompileDebug'.
> Annotation processors must be explicitly declared now.  The following dependencies on the compile classpath are found to contain annotation processor.  Please add them to the annotationProcessor configuration.
    - butterknife-7.0.1.jar (com.jakewharton:butterknife:7.0.1)
  Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior.  Note that this option is deprecated and will be removed in the future.
  See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.


此時,需要在build.gradle的 Android - defaultConfing 裡面新增:

javaCompileOptions {
    annotationProcessorOptions {
        includeCompileClasspath true
    }
}
如果你的專案依賴多個modle,每個modle都需要加上這句。

7.還有其他的話歡迎在評論區補充。