关于DataBinding引发的java.lang.reflect.InvocationTargetException

转载请注明出处: https://blog.csdn.net/hx7013/article/details/120985553

一、事故起因

换了台电脑,Git Pull同样的代码、相同版本AS、相同Gradle version、相同Kotlin version,但是代码就是跑不起来。
build 报错为

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

二、错误详情

ava.lang.reflect.InvocationTargetException(no error message),没有其它错误信息,这种情况直接gradlew assembleDebug --stacktrace,拿到报错详情,详细往下看,可以看到

......

Caused by: com.sun.tools.javac.processing.AnnotationProcessingError: android.databinding.tool.util.LoggedErrorException: failure, see logs for details.
Exception while handling step android.databinding.annotationprocessor.ProcessExpressions@4726c337 javax.xml.bind.UnmarshalException
 - with linked exception:
[org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.]
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:308)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:548)

......

Invalid byte 3 of 3-byte UTF-8 sequence,考虑编码问题。搜索一番后,错误原因可以基本确定为使用DataBinding时在布局里做了中文字符串拼接,例如

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text='@{"条码: " + x.detailCode}'
    android:textColor="@color/text_black"
    android:textSize="12sp"
    tools:text="条码" />

三、解决办法

知道问题,那么就好解决了。翻阅文章后大部分文章都说需要更改全部布局文件,改为@string引入

<string name="csdn">csdn x024 %s</string>
...
android:text="@{@string/csdn(firstName)}"

或者

<string name="csdn">csdn x024</string>
...
android:text='@{@string/csdn + firstName}'  

但是这是已有工程,全部修改工程量太大了,而且最开始也说 同样的代码、相同版本AS、相同Gradle version、相同Kotlin version ,新电脑就是不行,那么应该还有其它解决办法。最后发现老的电脑给JVM配置有指定UTF8编码,给新电脑配置后,正常运行。

-Dfile.encoding=UTF-8

复制上面的编码,打开AndroidStudio > Help > Edit Custom VM Options..…
在打开的新窗口里,复制进去,保存,关闭AndroidStudio,重启电脑,问题解决。
DataBinding中文拼接异常

转载请注明出处: https://blog.csdn.net/hx7013/article/details/120985553

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值