Android项目生成文件出现类型不兼容:java.lang.String无法转自定义String
Hey there, let's break down why this error keeps popping up even after you tweak those generated files—because manually editing Manifest.java or BuildConfig.java won’t stick—they get regenerated every single build! Here’s what’s going on and how to fix it for good:
Root Cause
The issue boils down to a naming conflict: you’ve got a custom class named String in your com.wordpress.zeel.silverglitters package, which is clashing with Java’s default java.lang.String. The Android build tools are accidentally referencing your custom String class instead of the standard one in those auto-generated files.
Step-by-Step Solutions
Check for accidental imports in your source code
Flip through your project files and make sure you haven’t imported your customStringclass where you meant to use the standard one. Sincejava.lang.Stringdoesn’t require an explicit import, any line likeimport com.wordpress.zeel.silverglitters.String;will force the compiler to prioritize your custom class, triggering the conflict in generated files. Delete those imports if they’re unnecessary.Use fully qualified class names when needed
If you have to keep both the customStringclass and use the standard one, explicitly writejava.lang.Stringinstead of justStringin your code. For example:// Instead of just "String" java.lang.String appLabel = getResources().getString(R.string.app_name);This tells the compiler exactly which
Stringyou’re referring to, no confusion allowed.Clean and rebuild your project (this is critical!)
Generated files are cached, so manual edits get overwritten on every build. Do a full clean to wipe old generated content:- In Android Studio, go to
Build > Clean Project - Then run
Build > Rebuild Project - If that still doesn’t work, manually delete the
app/builddirectory from your project structure, then rebuild from scratch.
- In Android Studio, go to
Rename your custom String class (best long-term fix)
The simplest way to avoid this conflict forever is to rename your customStringclass to something unique—likeAppString,CustomString, or any name that doesn’t match Java’s core class name. This eliminates the naming clash entirely, so the build tools will always reference the correctjava.lang.Stringin generated files.Double-check your Gradle configurations
Take a quick look at your app-levelbuild.gradlefile to make sure there aren’t any unusual settings or dependencies that might be forcing incorrect imports. This is less common, but it’s worth ruling out edge cases.
Final Note
Always remember: never edit auto-generated files like Manifest.java or BuildConfig.java directly—they’re regenerated automatically during each build, so your changes will be lost instantly. Focus on fixing the root cause in your source code or project setup instead.
内容的提问来源于stack exchange,提问作者Harsh Vasoya

