删除android工程不再使用的资源文件

删除android工程不再使用的资源文件

1.

先cmd调用 lint --check "UnusedResources" /Users/alvin/Documents/workspacenewjuno/Alvin_Android7bug > ~/Downloads/result.txt


2.

printusedLineFrom 方法是打印出来复制到文件里面

removeUnUsedResources() 是删除多余文件


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ClearUnUsedResource {

	public static String RESULT_PATH = "/Users/alvin/Downloads/result.txt";

	public static String projectDirPath = "/Users/alvin/Documents/workspacenewjuno/Alvin_Android7bug";

	public static String STRING_PATH = projectDirPath
			+ "/res/values/strings.xml";

	public static String colors_PATH = projectDirPath
			+ "/res/values/colors.xml";

	public static String layoutDirPath = projectDirPath + "/res/layout";
	public static String layout_arDirPath = projectDirPath + "/res/layout-ar";
	public static String drawableDirPath = projectDirPath + "/res/drawable";

	public static String resDirPath = projectDirPath + "/res";

	public static void main(String[] args) {

		/**
		 * 先cmd调用 lint --check "UnusedResources"
		 * /Users/alvin/Documents/workspacenewjuno/CocoVoice_Android7bug >
		 * ~/Downloads/result.txt
		 */

		//
		// printusedLineFrom("res/values/strings.xml:", STRING_PATH);

		// removeUnUsedResources();

		printusedLineFrom("res/values/colors.xml:", colors_PATH);

	}

	private static void printusedLineFrom(String key, String filePath) {

		Set<Integer> unUsedNums = getUnUsedLineNumFormCheckResult(key,
				RESULT_PATH);

		List<String> lines = new ArrayList<String>();

		try {
			File file = new File(filePath);
			Reader reader = new FileReader(file);
			BufferedReader bf = new BufferedReader(reader);

			String line = "";
			lines.add("");
			while ((line = bf.readLine()) != null) { //

				lines.add(line);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		int size = lines.size();

		String expen = "android.1428972613902";

		for (int i = 1; i < size; i++) {
			if (!unUsedNums.contains(i) || lines.get(i).contains(expen)) {
				System.out.println(lines.get(i));
			}

		}

	}

	private static Set<Integer> getUnUsedLineNumFormCheckResult(String key,
			String filePath) {
		Set<Integer> unUsedNums = new HashSet<Integer>();

		try {
			BufferedReader bf = new BufferedReader(new FileReader(filePath));

			int count = 0;

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

				if (line.contains(key)) {
					String[] ss = line.split(":");
					if (ss != null && ss.length == 4) {
						int numberline = -1;
						try {
							numberline = Integer.parseInt(ss[1]);
						} catch (Exception e) {

						}
						if (numberline > 0) {
							unUsedNums.add(numberline);
						}

					}
				}

			}
			System.out.println("count :  " + count);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return unUsedNums;
	}

	public static void removeUnUsedResources() {

		String resultFilePath = RESULT_PATH;

		Set<String> drawablepaths = new HashSet<String>();

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

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

				if (line.contains("UnusedResources")
						&& !line.contains("res/value")
						&& !line.contains("appcompat")) {
					int end = line.indexOf(":");
					if (end != -1) {
						String file = projectDirPath + "/"
								+ line.substring(0, end);
						new File(file).delete();
						System.out.println(file);
						drawablepaths.add(file);
					}
				}

			}
		} catch (Exception e) {
		}
		for (String layoutpath : drawablepaths) {
			System.out.println(layoutpath);
		}

	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值