React Native Android报错:app:processDebugResources FAILED 原因及解决方法
Hey there! Let's break down why this AAPT error is happening and how to fix it right away.
报错原因
Android's AAPT (Android Asset Packaging Tool) has strict rules for naming package names and component attributes like android:name. The core issue here is that hyphens (-) are not allowed in these values—they violate Java's identifier naming conventions, which only permit letters, numbers, underscores (_), and require package names to follow reverse domain notation (e.g., com.yourcompany.yourapp).
When you build your React Native app, the merged debug manifest (the one in the intermediates folder) inherits these invalid hyphenated values from your main AndroidManifest.xml or third-party dependencies, triggering the errors you see.
解决步骤
Follow these steps to eliminate the invalid characters:
Fix your main AndroidManifest.xml
Openandroid/app/src/main/AndroidManifest.xml:- Locate the root
<manifest>tag'spackageattribute, replace any hyphens with underscores:<!-- Before --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my-app"> <!-- After --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my_app"> - Check all component tags (like
<activity>,<service>,<receiver>) for theirandroid:nameattributes and replace hyphens there too:<!-- Before --> <activity android:name="com.my-app.MainActivity"> <!-- After --> <activity android:name="com.my_app.MainActivity">
- Locate the root
Sync your build.gradle configuration
Openandroid/app/build.gradleand update theapplicationIdto match your modified package name:defaultConfig { applicationId "com.my_app" // Must match the fixed package name // ... other existing configs }Update Java/Kotlin source files
If you have Java/Kotlin files (likeMainApplication.javaorMainActivity.java), update the package declaration at the top of the file:// Before package com.my-app; // After package com.my_app;Also, double-check any references to component names in these files to ensure they use the updated underscore format.
Clean build cache
Old build artifacts might still hold the invalid manifest data, so clean them up before rebuilding:cd android ./gradlew clean cd .. react-native run-android
特殊情况:第三方依赖引发的问题
If the invalid hyphens come from a third-party library's manifest, you can override the problematic attributes in your own AndroidManifest.xml using manifest merging. For example, if a library has a service with an invalid name, add this to your manifest:
<service android:name="com.invalid-lib-name.MyService" tools:replace="android:name" android:name="com.invalid_lib_name.MyService" />
(Note: You'll need to add xmlns:tools="http://schemas.android.com/tools" to your root <manifest> tag to use the tools:replace attribute.)
内容的提问来源于stack exchange,提问作者Lankylame

