Android宿主apk和pluginapkR文件id冲突

    xiaoxiao2025-10-14  7

    背景描述:

    在launcher开发中,我们老大做了一个插件化的酷生活。可以在原生launcher的基础上方便的一页(-1屏)。显示一些常用应用,服务,和新闻。如下图:

    这一页整个是一个view,添加在celllayout中,然后把这个celllayout添加在workspace里就好了。

    private void initCustomPage( CellLayout cell ) { if( hasCustomContent() ) { try { mCustomPageView = FavoritesPageManager.getInstance( this.getContext() ).getView(); if( mCustomPageView != null ) { cell.addView( mCustomPageView ); cell.setCustomPage( true ); } } catch( Exception e ) { e.printStackTrace(); } } }

    plugin单独编译一个apk,放在宿主apk的assets目录。宿主apk运行时,调用初始化方法,动态的加载plugin.apk的代码和资源。(老大说用到了什么classLoader,小白表示不是很懂)

    问题描述:

    给客户移植的过程中遇到问题了,导致客户launcher一起重启。后来发现是plugin.apk自动生成的R文件和客户launcher.apk自动生成的R文件中id有冲突导致。

    解决方案:

    作为一个爱学习的程序猿,遇到问题当然是上网求答案。经过一番查找,终于找到几种方法:

    1、给冲突id的控件定义自定义id(@id而不是@+id)

    layout文件

    <TextView android:id="@id/textView15" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:paddingBottom="@dimen/icon_padding_top" android:text="TextView" />

    values下面新建的ids.xml

    <?xml version="1.0" encoding="utf-8"?> <resources> <!-- //****** add whole file //解决“launcher3的R文件中id和酷生活R文件中id冲突”的问题 --> <item type="id" name="textView15" /> </resources>

    2、限定plugin.apk中,id的范围

    <TextView android:id="@+id/textView15" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:paddingBottom="@dimen/icon_padding_top" android:text="TextView" />

    values下面新建的ids.xml

    <?xml version="1.0" encoding="utf-8"?> <resources> <!-- //****** add whole file //解决“launcher3的R文件中id和酷生活R文件中id冲突”的问题 --> <public-padding name="zmap" start="0x7f030000" end="0x7f030fff" type="id"/> </resources>

    效果:在自动生成的R文件中可以看到,所有的id都在start和end之间

    name:随意取一个名字。

    start/end:起始位置/终止位置

    type:id 或者其他资源

    3、  Android中如何修改编译的资源ID值(默认值是0x7F...可以随意改成0x02~0x7E)

    这个方法暂时尚未验证,啊哈哈。。。。

    以上三种方法都是上网扒到的,前两种已经验证。第三种由于环境限制,没有试验过。

    如果还有其他方式,欢迎留言评论。

    ps:第一篇博客,写的不好,多多包涵。

    转载请注明原文地址: https://ju.6miu.com/read-1303145.html
    最新回复(0)