1. 程式人生 > >AndroidStudio環境升級 AS 3.1.1 + gradle 3.1.2 + wrapper 4.7 + sdk 27 踏坑記錄

AndroidStudio環境升級 AS 3.1.1 + gradle 3.1.2 + wrapper 4.7 + sdk 27 踏坑記錄

報錯一:

Error:Unable to find method 'com.android.build.gradle.tasks.factory.AndroidJavaCompile.setDependencyCacheDir(Ljava/io/File;)V'.Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)Re-download dependencies and
sync project (requires network) The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.Stop Gradle build processes (requires restart) Your project may be using a third-party plugin which is not compatible with the other plugins in the project or
the version of Gradle requested by the project. In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

  我在AndroidStudio還在2.3.3版本的情況下,想把wrapper版本升級至4.7結果報出上述錯誤。wrapper版本必須跟著AndroidStudio的版本走,若想使用wrapper版本4.7,就需要將AndroidStudio的版本升級至3.1以上。

報錯二:

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=zhumeiRelease, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

  之前專案中配置渠道打包時做了如下配置:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def oldFile = output.outputFile
        if (variant.buildType.name.equals('release')) {
            def releaseApkName = getProductName() + "_v${defaultConfig.versionName}_${releaseTime()}_" + variant.productFlavors[0].name + '_release.apk'
            output.outputFile = new File(oldFile.parent, releaseApkName)
        }
    }
}

  升級之後,output.outputFile變成了只讀屬性,不能使用,將output.outputFile替換為output.outputFileName即可

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        if (variant.buildType.name.equals('release')) {
            output.outputFileName = new File(getProductName() + "_v${defaultConfig.versionName}_${releaseTime()}_" + variant.productFlavors[0].name + '_release.apk')
        }
    }
}

報錯三:

Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

  在module的build.gradle檔案的android —> defaultConfig節點下加入:

flavorDimensions "versionCode"

  問題即可解決

報錯四:

Error:Execution failed for task ':youyoubao:javaPreCompileCommonDebug'.
> 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-compiler-8.6.0.jar (com.jakewharton:butterknife-compiler:8.6.0)
  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.

  這是專案中使用butterknife而引發的錯誤,需要在module的build.gradle檔案的android —> defaultConfig節點下加入:

javaCompileOptions {
    annotationProcessorOptions{
        includeCompileClasspath = true
    }
}

  問題即可解決

報錯五:

Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org.gradle.configureondemand=false in your gradle.properties file or use a Gradle version less than 4.6.

  根據其指示,在AndroidStudio中使用ctrl + shift + F 全域性搜尋 org.gradle.configureondemand 關鍵字,將true改為false;若未解決ctrl + alt + s 調出setting,取消Configure on demand 的勾選,如下圖:
這裡寫圖片描述

後續問題一:

Cannot resolve symbol KeyEventCompat(android.support.v4.view.KeyEventCompat找不到) 

  sdk升級之後,KeyEventCompat類被取消,hasNoModifiers方法已經被KeyEvent實現了,NoPreloadViewPager中的程式碼修改如下:

// 修改前程式碼
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                handled = arrowScroll(FOCUS_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                handled = arrowScroll(FOCUS_RIGHT);
                break;
            case KeyEvent.KEYCODE_TAB:
                if (KeyEventCompat.hasNoModifiers(event)) {
                    handled = arrowScroll(FOCUS_FORWARD);
                } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                    handled = arrowScroll(FOCUS_BACKWARD);
                }
                break;
            default:
                break;
        }
    }
    return handled;
}

// 修改後程式碼
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                handled = arrowScroll(FOCUS_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                handled = arrowScroll(FOCUS_RIGHT);
                break;
            case KeyEvent.KEYCODE_TAB:
                if (event.hasNoModifiers()) {
                    handled = arrowScroll(FOCUS_FORWARD);
                } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
                    handled = arrowScroll(FOCUS_BACKWARD);
                }
                break;
            default:
                break;
        }
    }
    return handled;
}

後續問題二:

all com.android.support libraries must use the exact same version specification(mixing versions can lead to runtime crashes)

  引用的第三方庫的支援庫版本低於module的 build.gradle中的支援庫版本或者與module的 build.gradle中的支援庫版本不一致時,可能會出現此問題,解決此問題可以在module的 build.gradle中新增

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.1.0'
            }
        }
    }
}

後續問題三:

  將Glide更新至4..1.1版本後,大多數的設定已經放到了RequestOptions物件中,之前的寫法會報錯

// 之前的寫法
 Glide.with(mContext)
      .load(logoUrl)
      .error(defauleRes)
      .skipMemoryCache(true)
      .centerCrop()
      .into(imageView);

// 修改後的寫法
 RequestOptions options = new RequestOptions()
        .centerCrop()
        .error(defauleRes)
        .skipMemoryCache(true);

Glide.with(mContext)
        .load(logoUrl)
        .apply(options)
        .into(imageView);

後續問題四:

java.lang.NoClassDefFoundError: com.igexin.sdk.d

因本人才疏學淺,如部落格或Demo中有錯誤的地方請大家隨意指出,與大家一起討論,共同進步,謝謝!