jbendtsen / tiny-android-template

Minimalist Android Project w/o Gradle
MIT License
42 stars 6 forks source link

link.pl's gen_proj_rtxt generates duplicates for values-v## folders (e.g. styles.xml) #2

Closed Leif-W closed 2 years ago

Leif-W commented 2 years ago

res/values*:

res/values/strings.xml
res/values/styles.xml
res/values-v21/styles.xml

relevant R.txt:

int style AppTheme 0x7f050003
int item @android:windowActionBar 0x7f050004
int item @android:windowNoTitle 0x7f050005
int style AppTheme 0x7f060000
int item @android:windowActionBar 0x7f060001
int item @android:windowNoTitle 0x7f060002

relevant R.java:

        public static final class style {
                public static final int AppTheme=0x7f050000;
        }
        public static final class item {
                public static final int windowActionBar=0x7f050004;
                public static final int windowNoTitle=0x7f050005;
        }
        public static final class style {
                public static final int AppTheme=0x7f050000;
        }
        public static final class item {
                public static final int windowActionBar=0x7f060001;
                public static final int windowNoTitle=0x7f060002;
        }

relevant link.pl output:

Compiling project R.java...
build/R.java:33: error: class style is already defined in class R
        public static final class style {
                            ^
build/R.java:36: error: class item is already defined in class R
        public static final class item {
                            ^
2 errors

How do we handle support for more than one version of Android? This built as-is with a "minimalist" (bloated) gradle on the device itself using Termux as proof of concept. Now I want absolute minimalism, but correctness.

Thanks for this project. Interesting idea.

Possibly relevant docs:

https://developer.android.com/guide/topics/resources/providing-resources#AlternativeResources

At the end of table 2, the API version qualifiers, i.e. folders of the format res/defaultname-qualifier used to specify default and alternate resources to be chosen at runtime to support multiple devices (layouts and API levels).

In my case, the default style (values) is API v11, and a more modern style requires API v21 so values-v21 overrides the default.

The docs don't make clear exactly what the actual resultant code in R.java ought look like for alternate resource qualifiers, e.g. style and style_v21, and item and item_v21, for example.

Example docs of my correct usage of (regexp) /values(|-v\d+)/ folder for alternate resources.

https://developer.android.com/develop/ui/views/theming/themes#Versions

This tutorial indicates something interesting:

http://www.java2s.com/Tutorials/Android/Android_Tutorial/0080__Android_Resources_Configuration.htm

R.java should have one identifier for the base name, not for each alternate resource qualifier.

Does it mean that gen_proj_rtxt should only generate identifiers for resource base names, not additional qualifiers?

If yes, then this line maybe the problem?

if ($dir =~ /values/) {

The regexp probably ought to be explicitly bounded by start and end.

/^values$/

A more generalized solution needs to be devised for all base resource names and qualifiers. Assuming base names never have a hyphen (-), this is easier, perhaps /^[^-]+$/ or something?

Either fix the glob foreach my $dir (<res/*>) { (tricky glob syntax? I forget...) or (more simply but less elegant) filter hyphenated folders at the beginning of the loop or something?

foreach my $dir (<res/*>) {
        next if $dir =~ /-/;

I'm not sure, but I guess the Android system does magic, so as long as alternate resource folders are in the .apk, they'll be found and used? I may be wrong. It feels wrong. Like, they should be explicitly referenced somewhere? I have no idea...

jbendtsen commented 2 years ago

Thanks for pointing this out! R.java is what the app's code uses to access a resource, which means it should only contain the base version of that resource. The specific version/configuration is decided by Android at run time.

This has now been fixed: https://github.com/jbendtsen/tiny-android-template/commit/7d26cfdb0844666ce647a85270f4cab05caf830a