Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Library class android.content.res.XmlResourceParser implements program class org.xmlpull.v1.XmlPullParser during R8 Minification

I am encountering an error when trying to make a signed app bundle of my Android project. The build process fails during the minifyReleaseWithR8 task with the following messages:

Task :app:minifyReleaseWithR8 AGPBI: {"kind":"warning","text":"Unexpected reference to missing service implementation class in META-INF/services/org.xmlpull.v1.XmlPullParserFactory: org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer.","sources":[{"file":"D:\Projects\AndroidStudioProjects\MyAndroidApp\app\build\intermediates\merged_java_res\release\mergeReleaseJavaResource\base.jar"}],"tool":"R8"} AGPBI: {"kind":"error","text":"Library class android.content.res.XmlResourceParser implements program class org.xmlpull.v1.XmlPullParser","sources":[{}],"tool":"R8"} Library class android.content.res.XmlResourceParser implements program class org.xmlpull.v1.XmlPullParser Task :app:minifyReleaseWithR8 FAILED

Added the following ProGuard rules:

-keep class android.content.res.XmlResourceParser { *; } -keep class org.kxml2.io.KXmlParser { *; } -keep class org.kxml2.io.KXmlSerializer { *; } -keep class org.xmlpull.v1.** { *; }

Despite these efforts, the error persists. I would appreciate any insights or solutions to resolve this issue. Thank you!

Additional Details Android Studio version: Android Studio Jellyfish | 2023.3.1 Patch 1 Gradle version: 8.6 R8 version: 8.4.1

like image 494
MGB Avatar asked Dec 31 '25 06:12

MGB


1 Answers

A likely cause for the error is using a Java XML parser library not intended for Android, within Android. A common culprit is org.simpleframework.simple-xml.

The reason this is an issue is that Android provides a built-in class org.xmlpull.v1.XmlPullParser that is not available in base Java. The XML parser library provides its own implementation of the same class, since it assumes it is running in base Java, where the class does not exist. The two implementations end up clashing.

The solution is to try to exclude the custom implementation.

First, you need to figure out which of your dependencies is bringing its own XML parser. You need to look in app/build.gradle inside the dependencies section. If you're lucky, a very likely suspect will present itself simply by having "xml" in the name:

    implementation 'com.squareup.retrofit2:converter-simplexml:2.11.0'

Replacing the library completely with an Android-specific library would be the optimal option. But it's not always possible. Fortunately, the library is likely to not be implementing the class directly, but rather, depending on a different library to do it.

The next step is, therefore, to drill through the Maven dependencies, to figure out which dependency is actually bringing in the unneeded class.

For example, if we look through the mvnrepository.com page for the above package, click on the version we are using, and scroll down, we can see a Compile Dependencies listing the following artifacts:

com.squareup.retrofit2 » retrofit 2.11.0
org.simpleframework » simple-xml 2.7.1

Once again, simple-xml stands out as a likely suspect. Clicking the version number again and scrolling down, we find the following dependencies:

stax » stax-api 1.0.1
stax » stax 1.2.0
xpp3 » xpp3 1.1.3.3

Clicking through each, we can notice that xpp3's full name is MXP1: Xml Pull Parser 3rd Edition (XPP3). The keywords "Xml Pull Parser", very closely matching the offending class name, makes this a very highly likely suspect.

The next step is to exclude it from the build. We modify the line in app/build.gradle as follows:

    implementation('com.squareup.retrofit2:converter-simplexml:2.11.0') { exclude group: 'xpp3', module: 'xpp3' }

This will exclude xpp3 » xpp3 (i.e. group xpp3, module xpp3) from the build, allowing the built-in org.xmlpull.v1.XmlPullParser to be used instead. The depending libraries org.simpleframework.simple-xml and com.squareup.retrofit2.converter-simplexml will still build as normal, linking against the built-in org.xmlpull.v1.XmlPullParser instead of the one provided by xpp3.xpp3.

The exact steps for your project might vary depending on which XML parser libraries you are using directly or indirectly. In the worst case, you may have to drill down through all of your dependencies. But hopefully, whichever libraries are bringing in their own XML parser are clearly marked "xml".

like image 83
SlugFiller Avatar answered Jan 02 '26 18:01

SlugFiller