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.
1
2
3
4
5
  我在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.
1
  之前项目中配置渠道打包时做了如下配置:

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)
        }
    }
}
1
2
3
4
5
6
7
8
9
  升级之后,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')
        }
    }
}
1
2
3
4
5
6
7
报错三:

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
1
  在module的build.gradle文件的android —> defaultConfig节点下加入:

flavorDimensions "versionCode"
1
  问题即可解决

报错四:

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.
1
2
3
4
5
  这是项目中使用butterknife而引发的错误,需要在module的build.gradle文件的android —> defaultConfig节点下加入:

javaCompileOptions {
    annotationProcessorOptions{
        includeCompileClasspath = true
    }
}
1
2
3
4
5
  问题即可解决

报错五:

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.
1
  根据其指示,在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找不到) 
1
  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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
后续问题二:

all com.android.support libraries must use the exact same version specification(mixing versions can lead to runtime crashes)
1
  引用的第三方库的支持库版本低于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'
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
后续问题三:

  将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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
后续问题四:

java.lang.NoClassDefFoundError: com.igexin.sdk.d
1
  一个奇怪的Bug,在Android版本5.0以下都不能运行,最终原因找出是代码方法数超出限制,解决方法之前已经写过,见:https://blog.csdn.net/zxc514257857/article/details/66997689
--------------------- 
作者:zxc514257857 
来源:CSDN 
原文:https://blog.csdn.net/zxc514257857/article/details/80425711 
版权声明:本文为博主原创文章,转载请附上博文链接!

相关文章

安装环境都很简单,就是下载工具需要在华为开发者联盟里注册...
上传的方式有两种,第一种是通过bintray官方出的插件bintray...
转载自: 完美解决Error:SSLpeershutdownincorrectly打开gr...
https://www.jianshu.com/p/9220227cdfb3buildscript{ext.ko...
Markdown版本笔记我的GitHub首页我的博客我的微信我的邮箱My...
 跟着教程做的,已经有了JDK,直接进行后面的步骤,下载安装...