批量去除项目中无用的资源文件

 
 
 
如果项目有添加混淆的时候,打包生成release包的时候,会在项目的app包下app\build\outputs\mapping 中生成如下几个文件
 

其中resources的文件中是将无用的资源文件压缩之后再打包的,所以虽然添加混淆可以使安装包变小,但是呢,其他无用的资源并没有完全被剔除掉,它还会增大安装包的大小。
我们可以写一个java文件将resource中删除的无用资源的语句复制过来,然后通过io操作删除项目中的文件。
public static void main(String[] args) {

       String string = "Skipped unused resource res/drawable-hdpi-v4/discovery_coupon.png: 579 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/discovery_daily.png: 769 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/discovery_forum.png: 570 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/discovery_into.png: 1440 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/discovery_quiz.png: 1051 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/discovery_registerdomain.png: 1479 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/dottedline.png: 114 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/my_pushdomain.png: 1340 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/mytrade.png: 1219 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/publci_type_bg_notchecked.png: 377 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/radiogroup_bg.png: 85 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable-hdpi-v4/remind_icon.png: 451 bytes (replaced with small dummy file of size 67 bytes)\n" +
                "Skipped unused resource res/drawable/btn_gray_selector.xml: 580 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/drawable/btn_orange_selector.xml: 580 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/drawable/btn_picture_selector.xml: 580 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/drawable/btn_red_selector.xml: 580 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/drawable/btn_white_selector.xml: 580 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/drawable/radio_vp_position_selector.xml: 580 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/abc_action_bar_up_container.xml: 448 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/abc_action_bar_view_list_nav_layout.xml: 396 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/amoydomain_search.xml: 1132 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/draw_search_activity.xml: 3608 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_couponlist.xml: 1816 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_dianzan.xml: 680 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_editsearch.xml: 1432 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_guidefirst.xml: 488 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_guidefourth.xml: 716 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_guidesecond.xml: 488 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_guidethird.xml: 488 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_messagelist.xml: 3100 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_regdomain1.xml: 644 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/layout/layout_schedulexieyi.xml: 1480 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/abort_ename.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/aprise.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/attention_domain.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/base.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/edit_mobile_bid.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/my_trade.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/nick_name.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/on_sale.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/personal_data.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/recharge.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/treasure_more.xml: 464 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/treasure_search.xml: 464 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/turn_in.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/turn_in_re_add.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/turn_out_detail.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/use_protection.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n" +
                "Skipped unused resource res/menu/wxentity.xml: 420 bytes (replaced with small dummy file of size 104 bytes)\n";


        String[] strings = string.split("\n");
        Pattern pattern=Pattern.compile(".*(drawable-hdpi-v4/|drawable/|menu/)(.*):.*");

        for (int i=0;i<strings.length;i++){
            matcher=pattern.matcher(strings[i]);
            if (matcher.find()){
                list.add(matcher.group(2));
            }
        }

        tran("项目路径\app\\src\\main\\res",list);
}

    public static  void tran(String path,ArrayList<String> list){
        ArrayList<String> strings=list;
        File file=new File(path);
        if (file.exists()){
            File[] files=file.listFiles();
            if (files.length==0){
                System.out.println("文件夹为空");
                return;
            }else {
                for (File file1:files){
                    if (file1.isDirectory()){
                        tran(file1.getAbsolutePath(),strings);
                    }else{
                        for (int i=0; i<strings.size();i++){
                            if (strings.get(i).equals(file1.getName())){
                                file1.delete();
                                strings.remove(i);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值