Unable to load class 'org.codehaus.groovy.runtime.typehandling.ShortTypeHandling'

异常提示:

Unable to load class ‘org.codehaus.groovy.runtime.typehandling.ShortTypeHandling’

错误原因:

出现这个错误是因为你的 Android Studio 中的 Gradle 插件和 Gradle 版本不一致导致的,非常有可能是因为你在 Android Studio 中倒入了一个旧版本 Gradle 构建的项目,它在 build.gradle 中声明的是旧版本的 Gradle 或者 Gradle插件,由于新版本的 Android Studio 并不支持旧版本的 Gradle 插件(在 Android Studio 中可能出现如下警告:Error:The project is using an unsupported version of the Android Gradle plug-in (0.12.2). The recommended version is 1.2.3.),所以我们最好使用最新的或稳定版本的 Gradle 插件,当你在 build.gradle (Project级别的)修改完声明的 Gradle插件版本之后再编译项目的话,就会爆出如题的异常,你使用来新版本的 Gradle插件,但 Gradle 却还是旧版本的。

解决方法:

修改 Project 下的 build.gradle文件(不是 Module 下的),将buildscript中的

<code class="language-Groovy hljs matlab has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">dependencies <span class="hljs-cell" style="box-sizing: border-box;">{
        classpath <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.android.tools.build:gradle:0.12+'</span>
    }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

改为

<code class="language-Groovy hljs matlab has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">dependencies <span class="hljs-cell" style="box-sizing: border-box;">{
        classpath <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.android.tools.build:gradle:1.2.3'</span>
    }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

然后,找到在项目中找到 gradle 目录(不是.gradle目录),按照gradle——wrapper——.gradle的路径找到 gradle-wrapper.properties 文件,将这个文件中的 distributionUrl 设置为 distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-all.zip,现在重新 build 整个项目即可通过编译。

注意事项:

  1. 上面的解决方式只是一个例子,随着时间的推移, Gradle 和 Gradle插件会不断更新,以后遇到如题的异常,则要根据 Android Studio 的警告或提示,使用稳定版本的 Gradle 和 Gradle 插件,如果你不清楚 Gradle插件到底是怎么对应 Gradle版本 的,请查看该文参考链接【2】(请自备梯子);下表是一个简单的对照:
Android Studio Android Gradle Plugin Gradle
1.0.0+ 1.0.0 - 1.1.3 2.2.1 - 2.3
1.0.0+ 1.2+ 2.2.1+

2. 使用这种方式要重新下载 Gradle 或者 Gradle插件,如果下载失败,请自备梯子翻墙,然后再重新build,不推荐使用本地版本 Gradle,可能出现其它问题 
3. 由于新版本的 Gradle 不支持旧版本 Gradle 命令,所以在使用新版本的 Gradle 编译前,要将已经失效或替换的 Gradle 命令替换掉,例如:Gradle Plugin 0.14.0之后, runProguard 更名为minifyEnabled。Gradle中文文档请查看参考链接【5】

参考链接:

http://stackoverflow.com/questions/28923716/getting-resolve-error-while-importing-project-in-android-studio-unable-to-load 
http://tools.android.com/tech-docs/new-build-system/version-compatibility (请自备梯子) 
http://blog.csdn.net/u011570979/article/details/45824073 
http://www.2cto.com/kf/201505/400371.html 
http://pkaq.github.io/gradledoc/docs/userguide/userguide.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值