Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for java reserved works in AndroidResource items #9537

Merged
merged 6 commits into from
Nov 26, 2024

Conversation

dellis1972
Copy link
Contributor

@dellis1972 dellis1972 commented Nov 21, 2024

Context #9461

Some users are seeing the following error from aapt2.

APT2258 invalid symbol name 'com.companyname.mauiapp1:drawable/import'.
C:\Users*User\AppData\Local\Temp\2kevcthb.kih\com\companyname\mauiapp1\R.java*"

This is because import is a java reserved word. The aapt2 code generator is not taking that into account (like we do for C# designer.cs). As a result we get this error. Its not very helpful.
So lets introduce a new error APT0004 which will explicitly check for java keywords in resource filenames.
This will allow users to know why their build is failing.

@dellis1972 dellis1972 requested review from jonathanpeppers and removed request for jonathanpeppers November 21, 2024 12:10
Fix space before paren, use `$@"…"` strings so `\` doesn't need escaping.
@dellis1972
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jonpryor
Copy link
Member

Draft commit message:

Context: /~https://github.com/dotnet/android/issues/9461

[Android Resource names become Java identifiers][0], so if you have
an image named `Resources/drawable/import.png`, *eventually* this
will become part of an `R.java` file with contents:

	/* partial */ class R {
	    public static class drawable {
	        public static final int import = 0xdeadbeef;
	    }
	}

at which point `javac` gets irate, because `import` is a Java keyword
and that's not a valid location for that keyword.

Other Android tooling also knows about and respects this limitation,
e.g. `aapt2`, which could report:

	…com\example\app\R.java: error APT2258: invalid symbol name 'com.example.app:drawable/import'.

This isn't something we can fix; Java keywords cannot be used as
Android Resource names.

What we *can* do is improve the UX, and explicitly call this out.

Introduce a new error `APT0004` error which will explicitly check for
Java keywords in resource filenames.  This will allow users to know
why their build is failing:

	error APT0004: Invalid file name: Filenames cannot use Java reserved words.

[0]: https://developer.android.com/guide/topics/resources/providing-resources#Accessing

@jonpryor
Copy link
Member

@dellis1972: while it's good to capture the filename case, that's not the only place where Resource names can appear and become Java identifiers. Two more places that come to mind are:

  • Layouts

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
      <Button android:id="@+id/import" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello" />
    </LinearLayout>

    The android:id="@+id/import" should cause things to break.

  • Strings:

    <resources>
      <string name="import">lol</string>
    </resources>

Can we check for these other occurrences as well? Should that be done as part of this PR, or separately?

@dellis1972
Copy link
Contributor Author

@dellis1972: while it's good to capture the filename case, that's not the only place where Resource names can appear and become Java identifiers. Two more places that come to mind are:

  • Layouts

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
      <Button android:id="@+id/import" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello" />
    </LinearLayout>
    

    The android:id="@+id/import" should cause things to break.

  • Strings:

    <resources>
      <string name="import">lol</string>
    </resources>
    

Can we check for these other occurrences as well? Should that be done as part of this PR, or separately?

Lets do this in a new PR. The current task does not process the actual files. Plus we might need to take into account custom classes which happen later in the build system.

@jonpryor jonpryor merged commit 87a7947 into main Nov 26, 2024
58 checks passed
@jonpryor jonpryor deleted the dev/dellis1972/reservedwords branch November 26, 2024 19:08
@github-actions github-actions bot locked and limited conversation to collaborators Dec 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants