I'm following this solution to use enetities in my string resource file:
Is it possible to do string substitution in Android resource XML files directly?
I'm using an external file in the resource tree: /res/raw/entities.dtd
, its content:
<!ENTITY ent_devicename "MyDeviceName">
In the string.xml
resource file:
<!DOCTYPE resources [
<!ENTITY % ent_devicename SYSTEM "../raw/entities.dtd">
%ent_devicename;
]>
<resources>
<string name="name">The name is &ent_devicename;</string>
</resources>
but I get this error:
The entity "ent_devicename" was referenced, but not declared.
As you can see Android Studio recognizes the external entity file:
and the entity:
Can someone provide full a correct example to make things work? I mean a full compilable Android Studio project, with entities declarations in a separated file.
UPDATE
Ok, if you pay more attention to this w3schools link:
https://www.w3schools.com/xml/xml_dtd_entities.asp
you see the solution:
the external entities.dtd
files contains
<!ENTITY ent_devicename "MyDeviceName">
then the new string resource resource:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY ent_devicename SYSTEM "../raw/entities.dtd">
]>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="Typos">
<string name="ent_devicenamxxe2">&ent_devicename;</string>
I changed <!ENTITY % ent_devicename
to <!ENTITY ent_devicename
(no more %)
I deleted %ent_devicename;
Now it compiles but the resulting APK seems to ignore the entity values (uses an empty string). So the problem is not resolved!
Let me know!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…