[Android] リソースファイルに画像を追加したらR.javaのdrawableでエラーが起きてコンパイルが通らなくなった件

R.javaのdrawableでエラー

仕事でJavaを使ったAndroidアプリの開発をしていて、ちょっとハマったのでメモ。

画像ファイルの追加でコンパイルが通らなくなった

開発中のアプリに画像ファイルを追加したら、なぜかコンパイルが通らなくなってしまいました。

レイアウトのxmlファイルやsrc配下のファイルには特に問題が見られず不思議に思っていたのですが、よくよく見ると自動生成されるgenフォルダ配下のファイルにエラーが出ていました。

スクリーンショット 2013-12-27 18.44.04

drawableってことは、画像に何か問題あり?

原因は追加した画像ファイル?

R.javaの該当箇所を見てみると、どうやら新しく追加した画像ファイルでエラーが出ている模様。

スクリーンショット 2013-12-27 18.43.44

エラーの内容は、”Underscores can only be used with source level 1.7 or greater” となっている。

スクリーンショット 2013-12-27 18.45.31

JREを1.7に変更?画像追加しただけなのに??

300px-おまえは何を言っているんだ

困ったときのStack Overflow

上記エラーメッセージでググっても、なかなか有力な情報は得られず時間だけが過ぎていく。こんな時に頼れるのはやっぱりStack Overflowでしょう!サルベージしてみたところ、すぐにピンポイントな情報にありつけました。

  1. Java identifiers cannot start with a digit. The first character should be a letter.
  2. In Java 7, they introduced alternative syntaxes for integer literals; e.g. 1_000 is the same as1000.

So what is happening is that the compiler is parsing 5_content_new as 5_ content_new … which is reasonable if the source level was Java 7, and then telling you that you are not using Java 7. If you HAD been using Java 7, that compilation error would have been replaced by an error that said that an integer literal (5_) was not legal at that point.

In short, the code contains something so “off the wall” that the compiler writer didn’t anticipate it in the compiler diagnostic code.

android – Strange error in R.java, even after cleaning the project: “Underscores can only be used with source level 1.7 or greater” – Stack Overflow

凄いぜStack Overflow。

リソースファイル名でも先頭に数字は使えない

というわけでファイル名の先頭に振っていた連番を末尾に持っていくよう変更したら、ビルドが通るようになりました。

コンパイル時にファイル名がプロパティとして自動生成されているんですね。

スクリーンショット 2013-12-27 18.43.44

Javaの基本ルールがリソースファイル名にも適用されるとは…

Android経験値が足りない

画像ファイルはデザイン担当の方からいただいていたのですが、ファイル名が引っかかっているという発想に至らず解決に時間を食ってしまいました。原因が分かってみると何ともお粗末な話です。

スクリーンショット 2013-12-27 18.45.31

しかしながらいきなりこのエラーメッセージを見て「ファイル名だ!」とはならないのは、やはり経験値が不足しているからなのか…

参考

android – Strange error in R.java, even after cleaning the project: “Underscores can only be used with source level 1.7 or greater” – Stack Overflow

Strange error in R.java, even after cleaning the project: “Underscores can only be used with source level 1.7 or greater” : Android Community – For Application Development

星影

Tech Hunter代表。マルチポテンシャライト。 ガジェット、アニメ、ゲーム、インターネットが好き。

関連記事

特集記事

コメント

この記事へのコメントはありません。

TOP