android Binary XML file line #1: Binary XML file line #1: Error inflating class x 问题详解

话不多少,上错误堆栈:

Process: com.mci.smagazine, PID: 25065
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mci.smagazine/com.zhangyue.iReader.idea.ActivityImageIdea}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class x
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3003)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
     Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class x
     Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class x
     Caused by: java.lang.ClassNotFoundException: android.view.x in loader com.zhangyue.iReader.plugin.d[DexPathList[[zip file "/data/app/com.mci.smagazine-1/base.apk"],nativeLibraryDirectories=[/system/lib, /vendor/lib]]]
        at com.zhangyue.iReader.plugin.d.loadClass(SourceFile:90)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:616)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:707)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:724)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:792)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.samsung.android.sdk.pen.util.SpenUtilLayout.getLayout(SourceFile:43)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.totalLayout(SourceFile:1633)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.initView(SourceFile:1983)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.construct(SourceFile:1617)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.(SourceFile:1572)
        at com.zhangyue.iReader.idea.ActivityImageIdea.initPenSettingView(SourceFile:276)
        at com.zhangyue.iReader.idea.ActivityImageIdea.initSpen(SourceFile:270)
        at com.zhangyue.iReader.idea.ActivityImageIdea.initWidget(SourceFile:140)
        at com.zhangyue.iReader.idea.ActivityImageIdea.onCreate(SourceFile:113)
        at android.app.Activity.performCreate(Activity.java:6977)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

问题现象:

debug 的包,没有问题,release 的包有问题。关键在于下面:

            zipAlignEnabled true
            shrinkResources true

release 的包会混淆代码,删除无用资源。

分析问题:

1.最开始的时候,我只看到了上面的堆栈,没有看到下面真正的错误堆栈。正真的有用堆栈在整个log 的最下面,如下:

Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class x
     Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class x
     Caused by: java.lang.ClassNotFoundException: android.view.x in loader com.zhangyue.iReader.plugin.d[DexPathList[[zip file "/data/app/com.mci.smagazine-1/base.apk"],nativeLibraryDirectories=[/system/lib, /vendor/lib]]]
        at com.zhangyue.iReader.plugin.d.loadClass(SourceFile:90)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:616)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:707)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:724)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:792)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.samsung.android.sdk.pen.util.SpenUtilLayout.getLayout(SourceFile:43)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.totalLayout(SourceFile:1633)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.initView(SourceFile:1983)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.construct(SourceFile:1617)
        at com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout.(SourceFile:1572)
        at com.zhangyue.iReader.idea.ActivityImageIdea.initPenSettingView(SourceFile:276)
        at com.zhangyue.iReader.idea.ActivityImageIdea.initSpen(SourceFile:270)
        at com.zhangyue.iReader.idea.ActivityImageIdea.initWidget(SourceFile:140)
        at com.zhangyue.iReader.idea.ActivityImageIdea.onCreate(SourceFile:113)
        at android.app.Activity.performCreate(Activity.java:6977)

Error inflating class x? 这是那个类?难道被混淆了?
我们自己重写定义了classLoader,每次findclass,我都知道,那么我就看下,debug 包加载的是哪个类,release 加载的是哪个类?那么我就知道这个类x 是什么类了,然后我把这个类keep,不混淆就行了。

不混淆的log:

2019-03-18 19:31:40.199 27007-27007/com.mci.smagazine D/IreaderClassLoader2: com.zhangyue.iReader.plugin.config.PluginConfig
2019-03-18 19:31:40.264 27007-27007/com.mci.smagazine D/IreaderClassLoader2: com.zhangyue.iReader.idea.ActivityImageIdea
2019-03-18 19:31:40.277 27007-27007/com.mci.smagazine D/IreaderClassLoader2: android.widget.RelativeLayout
2019-03-18 19:31:40.373 27007-27007/com.mci.smagazine D/IreaderClassLoader2: android.widget.ImageButton
2019-03-18 19:31:40.464 27007-27007/com.mci.smagazine D/IreaderClassLoader2: android.widget.Button
2019-03-18 19:31:40.500 27007-27007/com.mci.smagazine D/IreaderClassLoader2: android.widget.SeekBar
2019-03-18 19:31:40.536 27007-27007/com.mci.smagazine D/IreaderClassLoader2: android.widget.Switch

混淆的log:

2019-03-18 19:29:09.273 26520-26520/com.mci.smagazine D/IreaderClassLoader2: com.zhangyue.iReader.plugin.config.PluginConfig
2019-03-18 19:29:09.344 26520-26520/com.mci.smagazine D/IreaderClassLoader2: com.zhangyue.iReader.idea.ActivityImageIdea
2019-03-18 19:29:09.357 26520-26520/com.mci.smagazine D/IreaderClassLoader2: android.widget.RelativeLayout
2019-03-18 19:29:09.454 26520-26520/com.mci.smagazine D/IreaderClassLoader2: android.widget.ImageButton
2019-03-18 19:29:09.543 26520-26520/com.mci.smagazine D/IreaderClassLoader2: android.widget.x
2019-03-18 19:29:09.545 26520-26520/com.mci.smagazine D/IreaderClassLoader2: android.webkit.x
2019-03-18 19:29:09.546 26520-26520/com.mci.smagazine D/IreaderClassLoader2: android.app.x
2019-03-18 19:29:09.547 26520-26520/com.mci.smagazine D/IreaderClassLoader2: android.view.x

怎么加载的是 android.widget.Button? 这个类怎么可能找不到?好吧,尝试一下keep 一下:

-keep class android.widget.*

运行之后,不行,还是报错崩溃。
殊不知,上面走了一条错误的路。不过没关系。

好了,上绝招:

资源混淆?那么我看下最终的apk 里面的这个资源,到底是什么样的!

android Binary XML file line #1: Binary XML file line #1: Error inflating class x 问题详解_第1张图片
妈的,这个xml竟然真的只是一个x、难道是被资源优化,处理掉了?
我又找了一个在apk 里面,只有一个 的xml,查下引用,妈的,果然没有地方引用。如下图:

android Binary XML file line #1: Binary XML file line #1: Error inflating class x 问题详解_第2张图片

知道了,那些被优化的资源,被删掉的资源,不是真的被删除掉了,而是被弄成一个47b 的(空文件)!

android Binary XML file line #1: Binary XML file line #1: Error inflating class x 问题详解_第3张图片

我又不可能直接把shrinkResources 直接弄成false,这样可能包会增大。查下官网,果然有方法,可以保持资源不被shrinkResources 掉。

解决方法:在你的工程的res/raw/ 下面新建一个keep.xml

	

tools:keep="@layout/setting_*" keep 里面写上你需要保持的资源文件。

以下为波折的过程记录:

因为我的资源文件是在layout-xxxhdpi 下面的,layout 文件夹下没有这个文件, 那么我就用下面的keep 文件去尝试,结果不行:

	
	


我以为因为是layout-xxxhdpi 文件夹里面的文件,需要 @layout-xxxhdpi/setting_color_palette_layout_v41 这样写,其实是错的。
后来反复尝试,看到项目里面有一个keep 文件,虽然是aar 里面的,但是他写的很简单,@layout/a.xml
我懂了,应该是shrinkResources 的时候,只需要写@layout/a.xml ,

不需要管你想要保持的资源文件是在layout-xxh,还是在layout-xxxh 里面,只需要写layout 就可以了。

我改了下写法,可以了。如下:原来需要这样写

	

知识点:

怎么知道自己的资源被干掉了呢?
看build\outputs\mapping\Samsung\debug\resources.txt 文件,里面记录了资源文件处理情况:如下,setting_color_picker_layout_v41 被视为无用的资源,并且被优化了。

Skipped unused resource res/layout/setting_color_picker_layout_v41.xml: -1 bytes (replaced with small dummy file of size 104 bytes)

总结:

1.最好不要把layout 的加载写在字符串里面。如下面这种,如非必要,不推荐。

这样shrinkResources 的时候,可能给你优化掉了。

    this.mTotalLayout = (RelativeLayout)this.mLayoutUtil.getLayout("setting_pen_layout_v40");
    public View getLayout(String layoutName) {
    if (this.mSdkResources == null) {
        return null;
    } else {
        Log.d("SpenUtilLayout", "getLayout : layoutName = " + layoutName);
        int resID = this.mSdkResources.getIdentifier(layoutName, "layout", Spen.getSpenPackageName());
        Log.d("SpenUtilLayout", "getLayout : " + layoutName + " : resID = " + resID);
        return this.mInflater.inflate(this.mSdkResources.getLayout(resID), (ViewGroup)null);
    }

2.分析问题,要看准log。看看有没有和你的有关的。

3.有时候,问题并不出现在源码里面,而是出现在编译成apk 之后,所以遇到奇葩问题,一定要看最终的apk 里面是什么样子的。

参考:android 官网
https://developer.android.com/studio/build/shrink-code

你可能感兴趣的:(Android)