You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目生成文件出现类型不兼容:java.lang.String无法转自定义String

Fixing "incompatible types: java.lang.String cannot be converted to com.wordpress.zeel.silverglitters.String" Error

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 custom String class where you meant to use the standard one. Since java.lang.String doesn’t require an explicit import, any line like import 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 custom String class and use the standard one, explicitly write java.lang.String instead of just String in your code. For example:

    // Instead of just "String"
    java.lang.String appLabel = getResources().getString(R.string.app_name);
    

    This tells the compiler exactly which String you’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:

    1. In Android Studio, go to Build > Clean Project
    2. Then run Build > Rebuild Project
    3. If that still doesn’t work, manually delete the app/build directory from your project structure, then rebuild from scratch.
  • Rename your custom String class (best long-term fix)
    The simplest way to avoid this conflict forever is to rename your custom String class to something unique—like AppString, 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 correct java.lang.String in generated files.

  • Double-check your Gradle configurations
    Take a quick look at your app-level build.gradle file 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:04:34