1. 程式人生 > >批量去除專案中無用的資原始檔

批量去除專案中無用的資原始檔

如果專案有新增混淆的時候,打包生成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;
                            }
                        }
                    }
                }
            }
        }
    }
}