Android Studio 3.5 で XML のタグがソートされてしまう問題の対応

AS 3.5 Canary 8 以降で、 xml を reformat するとタグ名でソートされてしまう不具合が発生していました。

これについては Issue が発行されており、 AS 3.5 Canary 12 で修正されたようです。

Issue #129323345: Code Formatting changes the order of components in Layout hierarchy

I only overwrite old arrangement rules with the fixed new ones if they're all equal to the old default. If a user has changed them from the default, I don't overwrite them to preserve user customization. For the record, the complete set of fixed new rules look like the following. The only difference is the <XML_ATTRIBUTE /> elements that make them match attributes and not elements.

  • <XML_ATTRIBUTE /> を追加して、 attribute だけがフォーマットのルールにマッチするようにした。
  • デフォルトのフォーマットルールを変更していない場合は、この修正が適用される。ルールを変更している場合は、自分で修正を適用する必要がある。
    • デフォルトの定義はここにありました ~/Library/Preferences/AndroidStudioPreview3.5/codestyles/Default.xml

とのことなので、デフォルトルールを変更している場合や、プロジェクトでフォーマットルールを共有している場合は、以下の対応を入れましょう。

  <section>
    <rule>
      <match>
        <AND>
          <NAME>xmlns:android</NAME>
+         <XML_ATTRIBUTE />
          <XML_NAMESPACE>.*</XML_NAMESPACE>
        </AND>
      </match>
    </rule>
  </section>
  <section>
    <rule>
      <match>
        <AND>
          <NAME>xmlns:.*</NAME>
+         <XML_ATTRIBUTE />
          <XML_NAMESPACE>.*</XML_NAMESPACE>
        </AND>
      </match>
      <order>BY_NAME</order>
    </rule>
  </section>
  :