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

AndroidStudio构建签名App启动报错:Error inflating class Button

Fixing Fatal InflateException on Signed App Launch

Hey there, let's work through this crash you're hitting with your signed Android app. The error points to a Button failing to inflate from your XML at line 58—this is super common when moving from debug to release builds, so let's break down the most likely fixes:

Common Causes & Solutions

1. Resource Shrinking/Obfuscation Breakage

Release builds often enable resource shrinking and code obfuscation to cut APK size, but this can accidentally strip away resources or code your Button depends on.

  • Lock down ProGuard rules: If you're using a custom Button subclass, add a rule to keep its constructors from being obfuscated:
    -keep public class your.package.path.YourCustomButton {
        public <init>(android.content.Context);
        public <init>(android.content.Context, android.util.AttributeSet);
        public <init>(android.content.Context, android.util.AttributeSet, int);
    }
    
  • Test without shrinking: In your app's module-level build.gradle, set minifyEnabled false and shrinkResources false in the release block, then rebuild the signed APK. If the crash disappears, you know the issue is with obfuscation/resource stripping—adjust your rules to keep the necessary assets.

2. Missing Debug-Only Resources

Your XML might be referencing resources that only exist in the debug build (like debug-specific drawables or styles).

  • Open the XML file at line 58 and check all attributes on the Button (e.g., android:background, style). Make sure every referenced resource lives in your main res directory, not just debug/res.
  • If you need debug-only resources, use BuildConfig.DEBUG to conditionally apply them in code or use resource qualifiers to separate debug/release assets.

3. Missing Custom Button Constructors

If you're using a custom Button class, it must have all three required constructors for XML inflation to work in release builds:

public class CustomButton extends Button {
    public CustomButton(Context context) {
        super(context);
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

Skip any of these, and the system will fail to instantiate the view from XML.

4. APK Signing Corruption

Sometimes the signing process can corrupt resources or manifest entries.

  • Regenerate your signed APK using Android Studio's "Generate Signed Bundle/APK" wizard—double-check your keystore path, alias, and passwords are correct.
  • If you've been using a debug keystore for test releases, switch to a proper release keystore to rule out signing-related quirks.

Quick Debugging Tips

  • Compare Debug vs. Release: Install the debug version of your app—if it works fine, the issue is definitely tied to release build configurations.
  • Check the Full Stack Trace: The error you shared cuts off, but there's almost certainly a Caused by: line below that reveals the real root cause (e.g., ResourceNotFoundException or ClassNotFoundException). Grab the full Logcat output to narrow things down faster.
  • Inspect the Release APK: Unzip your signed APK and verify that the Button's referenced resources exist in the res directory, and that your custom Button class is present in the classes.dex file.

内容的提问来源于stack exchange,提问作者user5458201

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:52:38