AndroidStudio环境升级 AS 3.1.1 + gradle 3.1.2 + wrapper 4.7 + sdk 27 踏坑记录

版权声明:--------------------------------------------博文可随意转载,但请注明出处,谢谢!-------------------------------------------- https://blog.csdn.net/zxc514257857/article/details/80425711

编写不易,如有转载,请声明出处: 梦回河口:https://blog.csdn.net/zxc514257857/article/details/80425711

报错一:

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

  一个奇怪的Bug,在Android版本5.0以下都不能运行,最终原因找出是代码方法数超出限制,解决方法之前已经写过,见:https://blog.csdn.net/zxc514257857/article/details/66997689

因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!

猜你喜欢

转载自blog.csdn.net/zxc514257857/article/details/80425711