减少android大小,Android 减少安装包大小(一)Lint

package com.example;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map;

import java.util.Set;

public class UnusedResourcesClean {

static String Type_drawable = "drawable";

static String Type_layout = "layout";

static String Type_string = "string";

static String Type_color = "color";

static String Type_dimen = "dimen";

static String Type_menu = "menu";

static String Type_anim = "anim";

private static boolean cleanDrawable = true;

private static boolean cleanLayout = true;

private static boolean cleanAnim = true;

private static boolean cleanMenu = true;

private static boolean cleanColor = true;

private static boolean cleanString = true;

private static boolean cleanDimen = true;

/**

* project res dir

*/

static String sourceProjectResDir = "/Users/alvin/Documents/workproject/PerformanceOptimizationCase/reduceSizeCase/src/main/res";

/**

* lint check result file path

*/

static String lintResultFilePath = "unusedResourcesCleanTool/unusedLint";

/**

* key: resourceType

* value: resource file,or resource key name

*/

static HashMap> resMap = new HashMap<>();

public static void main(String[] args) {

try {

resMap = initUnUsedMap(lintResultFilePath);

if (resMap == null || resMap.isEmpty()) {

return;

}

printUnUsedMap();

clean(sourceProjectResDir,resMap);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* split The resource 'R.drawable.btn_bg_top_normal' appears to be unused

* lint result --> map

*

*@param resultFilePath

*/

private static HashMap> initUnUsedMap(String resultFilePath) throws Exception {

HashMap> resMap = new HashMap<>();

try {

BufferedReader bf = new BufferedReader(new FileReader(resultFilePath));

String line = "";

while ((line = bf.readLine()) != null) { //

if (line.contains("appears to be unused")) {

String resKey = line.split("'")[1];//R.drawable.btn_bg_top_normal

System.out.println(resKey);

String resType = resKey.split("\\.")[1];

String resName = resKey.split("\\.")[2];

System.out.println(resType + " , " + resName);

if (resMap.get(resType) == null) {

resMap.put(resType, new HashSet());

}

Set ids = resMap.get(resType);

ids.add(resName);

}

}

} catch (Exception e) {

e.printStackTrace();

}

return resMap;

}

private static void printUnUsedMap() {

for (Map.Entry> entry : resMap.entrySet()) {

System.out.println("------------------" + entry.getKey() + "------------------");

for (String s : entry.getValue()) {

System.out.println(s);

}

System.out.println(" ");

}

}

/**

*

*@param sourceProjectResDir 项目资源目录

*@param resMap 根据Type分类的unusedResource

* 例如 key: string value: {rateuslib_cancel,unnecessary_cancel}

*/

private static void clean(String sourceProjectResDir,HashMap> resMap) {

Map deletionFileMap = new HashMap<>();

deletionFileMap.put(Type_drawable, cleanDrawable);

deletionFileMap.put(Type_layout, cleanLayout);

deletionFileMap.put(Type_anim, cleanAnim);

deletionFileMap.put(Type_menu, cleanMenu);

deletionFileMap.put(Type_color, cleanColor);

Map deletionLineMap = new HashMap<>();

deletionLineMap.put(Type_string, cleanString);

deletionLineMap.put(Type_dimen, cleanDimen);

deletionLineMap.put(Type_color, cleanColor);

Map lineKeyMap = new HashMap<>();

lineKeyMap.put(Type_string, "

lineKeyMap.put(Type_dimen, "

for (File file1 : file.listFiles()) {

for (Map.Entry entry : deletionLineMap.entrySet()) {

String type = entry.getKey();

boolean clean = entry.getValue();

if (file1.getName().contains(type) && clean) {

resetXml(file1, resMap.get(type), lineKeyMap.get(type));

}

}

}

} else {

for (Map.Entry entry : deletionFileMap.entrySet()) {

if (file.getName().contains(entry.getKey()) && entry.getValue()) {

Set resSet = resMap.get(entry.getKey());

deleteResFile(resSet, file);

}

}

}

}

}

/**

* 从指定资源目录中,删除指定类型的文件

*

*@param resourceFiles

*@param resDir drawable/ or layout/ ...

*/

private static void deleteResFile(Set resourceFiles, File resDir) {

try {

for (File file1 : resDir.listFiles()) {

if (resourceFiles.contains(file1.getName().split("\\.")[0])) {

file1.delete();

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

*@param sourceFile 须要删除行的文件

*@param unusedSourceKeys 须要删除的行的关键词集合

*@param xmlTypeKey "

File outFile = new File(outFilename);

BufferedReader bf = new BufferedReader(new FileReader(sourceFile));

bw = new BufferedWriter(new FileWriter(outFilename));

String line = "";// App Backup & Restore

while ((line = bf.readLine()) != null) {

if (line.contains(xmlTypeKey) && unusedSourceKeys.contains(line.split("\"")[1])) {

continue;

}

bw.write(line);

bw.newLine();

}

sourceFile.delete();

outFile.renameTo(new File(sourceFile.getAbsolutePath()));

} catch (Exception e) {

} finally {

if (bw != null) {

try {

bw.flush();

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值