Android Studio Run app 报错

问题描述

今天在码代码时遇到一个莫名其妙的问题,问题是这样的:

不论 clean project、rebuild project 还是 make module 都能顺利执行,但是只要 Run app 就会报如下错误

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/home/connorlin/Program/jdk1.7.0_71/bin/java'' finished with non-zero exit value 134

问题分析

通常,看到 with non-zero exit value 134 首先想到的是依赖冲突了,但细心点能发现,依赖冲突一般是with non-zero exit value 1with non-zero exit value 2,此处却是134

再看Process 'command '/home/connorlin/Program/jdk1.7.0_71/bin/java'' finished with non-zero exit value 134 似乎是JDK相关。

那么,首先排除依赖冲突,因为之前是正常的,代码又没任何改动,是突然出现报错。再看到JDK,检查是否JDK配置出问题了,因为我同时装了sun JDKOpenJDK

问题定位

一次定位

开始查看java 版本:

java -version

结果显示:

java version "1.7.0_75"
OpenJDK Runtime Environment (IcedTea 2.5.4) (7u75-2.5.4-1~trusty1)
OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)

再看:

update-alternatives --config java

结果显示:

有 3 个候选项可用于替换 java (提供 /usr/bin/java)。

  选择       路径                                          优先级  状态
------------------------------------------------------------
  0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      自动模式
* 1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      手动模式
  2            /usr/local/jdk/bin/java                          255       手动模式
  3            /usr/local/jdk1.6.0_30/bin/java                  255       手动模式

要维持当前值[*]请按回车键,或者键入选择的编号:

证明JDK环境配置没错。

接着,把Android Studio JDK改为OpenJDK:

/home/connorlin/Program/jdk1.7.0_71
改为
/usr/lib/jvm/java-7-openjdk-amd64

再次编译 Run app,再次报错,但本次报错与上次不一样:

Error:Failed to complete Gradle execution.
Cause:
Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed)

居然Gradle build daemon 异常,那肯定不是代码的问题了。

二次定位

后来,无意中发现在项目跟路径下有多个类似hs_err_pid11776.log 文件,其内容节选如下:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fcac7773d44, pid=11776, tid=140508865505024
#
# JRE version: OpenJDK Runtime Environment (7.0_75-b13) (build 1.7.0_75-b13)
# Java VM: OpenJDK 64-Bit Server VM (24.75-b04 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 2.5.4
# Distribution: Ubuntu 14.04 LTS, package 7u75-2.5.4-1~trusty1
# Problematic frame:
# V  [libjvm.so+0x82ad44]  MoveAndUpdateClosure::do_addr(HeapWord*, unsigned long)+0xa4
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   http://icedtea.classpath.org/bugzilla
#

---------------  T H R E A D  ---------------

Current thread (0x00007fcac0023800):  GCTaskThread [stack: 0x00007fcac4f42000,0x00007fcac5043000] [id=11785]

siginfo:si_signo=SIGSEGV: si_errno=0, si_code=1 (SEGV_MAPERR), si_addr=0x00000000007d0038

Registers:
RAX=0x00000000007d0020, RBX=0x00007fcac5041cb0, RCX=0x0000000000000000, RDX=0x00000000007d0020
RSP=0x00007fcac5041ba0, RBP=0x00007fcac5041be0, RSI=0x00000000007d0020, RDI=0x00000000c47529c0
R8 =0x00007fcaa5800000, R9 =0x00007fcac7d0b420, R10=0x00000000030c9100, R11=0x00007fcac850f810
R12=0x0000000000000001, R13=0x0000000000000008, R14=0x00007fcac007abe0, R15=0x00000000c47529b8
RIP=0x00007fcac7773d44, EFLAGS=0x0000000000010202, CSGSFS=0x0000000000000033, ERR=0x0000000000000004
TRAPNO=0x000000000000000e

通过查看,其中:

---------------  T H R E A D  ---------------

Current thread (0x00007fcac0023800):  GCTaskThread [stack: 0x00007fcac4f42000,0x00007fcac5043000] [id=11785]

siginfo:si_signo=SIGSEGV: si_errno=0, si_code=1 (SEGV_MAPERR), si_addr=0x00000000007d0038

表明异常时正在运行GCTaskThread 垃圾回收线程,再看到后面还有一段:

VM Arguments:
jvm_args: -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -Xmx1024m -Dfile.encoding=UTF-8 -Duser.country=CN -Duser.language=zh -Duser.variant 
java_command: org.gradle.launcher.daemon.bootstrap.GradleDaemon 2.10
Launcher Type: SUN_STANDARD

似乎是内存不足引起的。

问题解决

一次解决

尝试将 HeapSize 调大,在 app level的build.gradle中配置:

dexOptions {
    preDexLibraries = false
    javaMaxHeapSize "4g"   // 调大 HeapSize
}

很遗憾,问题依然! 那还原此修改,防止二次事故。

二次解决

实在没办法,那尝试重启大法吧!

当然,不是简单的重启,先要清除缓存再重启Android Studio。

Android Studio -> File -> Invalidate Caches / Restart ..

最后,发现重启大法果然好用,居然顺利跑起来了,目前为止没有再出现该问题。

问题结果

通过清除Android Studio缓存来解决该问题:

在 Android Studio 的 File 菜单中选择 Invalidate Caches / Restart ..来清理缓存并重启Android Studio。

Android Studio -> File -> Invalidate Caches / Restart ..

但个中缘由却并不清楚,暂且记录下来,望知情人士指教!

That’s all!

原创文章,欢迎转载,转载请注明出处

我的简书账号是ConnorLin,欢迎关注!

ConnorLin wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!

热评文章