Windows環境でAndroidのDataBindingを使ってる時にMalformedByteSequenceExceptionが出た時の対処方法


Windows環境でAndroidのDataBindingを使ってると以下のようなエラーが出ることがある。

Error:com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3バイトのUTF-8シーケンスのバイト3が無効です

これはレイアウトXMLで直接日本語を使ってるいると起きる。
例えばこんな感じの記述

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="hogeFlag"
            type="Boolean"/>
    </data>

....

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        text="@{hogeFlag ? `だめな日本語` : `foo`}"/>

</layout>

こういう時は素直にstringリソースを使うようにすれば正しく動く

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        text="@{hogeFlag ? @string/hoge : @string/foo}"/>

一時的なものとして入れてたら思わぬ問題が起きてしまった。