问题
This is our old gradle
classpath 'com.android.tools.build:gradle:3.4.2'
...
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
With the above gradle, we are able to use the following string resource
<string name="reminder_doesnt_work_description"><![CDATA[doesn't]]></string>
But, if we were upgraded to latest gradle
classpath 'com.android.tools.build:gradle:3.5.0'
...
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
We will get the following error
> Multiple task action failures occurred:
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
> Android resource compilation failed
C:\yocto\noteplus\app\src\main\res\values\strings.xml:696:5-81: AAPT: error: unescaped apostrophe in string
C:\yocto\noteplus\app\src\main\res\values\strings.xml:696:5-81: AAPT: error: not a valid string.
Any idea why such error happen?
Take note, both gradles able to handle string without single quote.
<string name="reminder_doesnt_work_description"><![CDATA[doesnt]]></string>
回答1:
I had this problem too with unescaped apostrophes in a string resource also using CDATA after Android Studio upgrading my project from Gradle 5.1.1 to 5.4.1.
Funny thing is that after I fixed these AAPT errors by escaping the apostrophes with backslashes, the CDATA text in the string resource lost its carriage returns when being displayed in the app, and appeared just like one huge block of text.
Problem went away when I downgraded back to Gradle 5.1.1
Based on this behavior, I suspect that there is a bug with the way Gradle 5.4.1 is integrated with the Android Studio, causing it to ignore the CDATA token. And so the resource contents are being treated as if no CDATA token is specified. The bug may not be in Gradle itself but its AAPT plugin or some configuration in-between.
I filed an Android Studio bug on this at https://issuetracker.google.com/issues/145430021
I say for now just downgrade Gradle back to 5.1.1
回答2:
Just Replace all ' in your string resource to \'
回答3:
Why not just escape the string as AAPT2
demands it?
<string name="reminder_doesnt_work_description">doesn\'t</string>
CDATA sections in XML are meant to be skipped by the XML parser, but this obviously doesn't apply in this case, because that CDATA
"section" is within the text-content of a node (and therefore this has zero effect, because it does not contain any node). And if you really would like to use CDATA
, then at least encode the <
and >
to <
and >
(but this still requires escaping the apostrophes).
来源:https://stackoverflow.com/questions/57984642/issue-with-latest-gradle-5-4-1-unescaped-apostrophe-in-string