@TryLoveCatch
2021-10-21T06:52:42.000000Z
字数 5614
阅读 3047
android android基础 gradle 清除缓存 全局搜索
点击 Clean Project 会执行 clean、:app:generateDebugSources task
其中执行 clean 时会找到根项目和所有子项目的 clean task,所以一般来讲,会执行两个 task,:clean 和 :app:clean。
他们区别在于:
:clean 删除 /build 文件夹
:app:clean 删除 /app/build 文件夹
invalidate Caches/Restart 会删除 android studio 的缓存。
详细内容可查看 idea 的官方文档
这个缓存指的是代码更改记录的缓存,即local history,清除之后的效果:
无法查看 local history
所有项目都需要重新 build
这样看来,invalidate Caches / Restart 和 Rebuild Project 并没有什么区别。
删除项目根目录下 .idea/libraries 目录的内容,或删除整个目录。
.idea/libraries 里的文件用来记录第三方库的 classes, javadoc 以及 sources 所对应的路径。
当更新依赖、切换git分支后,往往会出现某个库的代码报红的情况,这时候就是.idea/libraries 里的路径没有更新,所以找不到。
删除 /.idea/libraries 里的内容后,再次同步时,会重新生成。
~/.gradle/caches/transforms-1/files-1.1/ 是 gradle 缓存的库解压后的目录。
/.idea/libraries 中的文件指向的路径就是这里。
删除该目录或其中某个库后,当再次执行 generateDebugSources 时,不会重新下载,会从 modules-2 (下面要讲的)目录下寻找并解压。
~/.gradle/caches/modules-2/files-2.1/ 是 gradle 缓存的库的源文件。
删除该目录或其中某个库后,当再次执行 generateDebugSources 时,会重新从网络下载。
再理一遍它们的关系:
第一次执行 generateDebugSources task,会在 preBuild 时下载依赖库到 ~/.gradle/caches/modules-2/files-2.1/,并解压到 ~/.gradle/caches/transforms-1/files-1.1/
as 同步时,会生成 .idea/libraries,记录依赖库缓存的路径,即 ~/.gradle/caches/transforms-1/files-1.1/...
注:
android studio 的 make、clean、sync、build 等都会执行到 generateDebugSources task。
每个组件都是aar依赖(aar变成了class文件) → 当想搜索某个字符串在哪个组件中使用的时候却查不到,如果不是class文件 可以搜到,如果是class文件 搜不到
public class Grep {public static final String GRADLE_CACHE_PATH = "/Users/xxx/.gradle/caches/transforms-1/files-1.1";public static final String TARGET_PATH = "/Users/xxx/Desktop/result";private static int mCopyCount = 0;private static int mUnZipCount = 0;private static int mDeleteCount = 0;public static void main(String[] args) {File file = new File(GRADLE_CACHE_PATH);// 1 先把缓存中的文件拷贝到指定的文件夹中traverseCopy(file);System.out.println("traverseCopy file number ="+mCopyCount);// 2 把所有的jar文件解压出来到指定的文件夹中tarJarFile(TARGET_PATH);// 3 删除掉之前存在的jar文件deleteJarFile(TARGET_PATH);}/*** 遍历复制gradle cache 中的classes.jar到 指定目录中去** @param file*/private static void traverseCopy(File file) {File[] fs = file.listFiles();for (File f : fs) {if (f.isDirectory()) //若是目录,则递归打印该目录下的文件{traverseCopy(f);} else if (f.isFile() && "classes.jar".equals(f.getName())) //若是文件,直接打印{mCopyCount++;String sourcePath = f.getAbsolutePath();String aarName = getSimpleAarPathName(f.getAbsolutePath());String targetPath = getTargetPathName(aarName);File fileTargetFile = new File(targetPath);// 如果文件已经存在就不用去拷贝了if(fileTargetFile.exists()){continue;}copyFile(sourcePath, targetPath);}}}/*** 返回类似 homepage-api-2.5.30* @param path* @return*/private static String getSimpleAarPathName(String path) {String aarNameFirst = path.substring(path.indexOf("files-1.1/") + 10, path.indexOf(".aar/"));String aarNameLast = path.substring(path.indexOf(".aar/") + 5, path.indexOf(".aar/") + 10);return aarNameFirst+"-"+aarNameLast;}private static String getTargetPathName(String aarName) {return TARGET_PATH + "/" + aarName +".jar";}public static void copyFile(String source, String target) {try (FileInputStream fis = new FileInputStream(new File(source));FileOutputStream fos = new FileOutputStream(new File(target))) {FileChannel sourceChannel = fis.getChannel();FileChannel targetChannel = fos.getChannel();MappedByteBuffer mappedByteBuffer = sourceChannel.map(FileChannel.MapMode.READ_ONLY, 0, sourceChannel.size());targetChannel.write(mappedByteBuffer);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}private static void deleteJarFile(String targetPath) {File[] fs = new File(targetPath).listFiles();for (File f : fs) {if (f.isFile() && f.getName().contains(".jar")) {mDeleteCount++;f.delete();}}System.out.println(" deleteJarFile file number = " + mDeleteCount);}/*** 把指定目录中的 classes.jar 解压到文件中*/private static void tarJarFile(String targetPath) {File[] fs = new File(targetPath).listFiles();for (File f : fs) {if (f.isFile() && f.getName().contains(".jar")) {mUnZipCount++;String directory = f.getName().substring(0, f.getName().lastIndexOf(".jar"));String targetPathDirectory = TARGET_PATH + "/" + directory + "/";File targetPathDirectoryFile = new File(targetPathDirectory);// 如果已经创建过文件夹就不要去重复创建了if (targetPathDirectoryFile.exists()) {continue;}targetPathDirectoryFile.mkdirs();UnZipUtils.unZip(f, targetPathDirectory);}}System.out.println(" tarJarFile file number = " + mUnZipCount);}}
/*** 对文件进行解压操作*/public class UnZipUtils {private static final int BUFFER_SIZE = 2 * 1024;/*** zip解压* @param srcFile zip源文件* @param destDirPath 解压后的目标文件夹* @throws RuntimeException 解压失败会抛出运行时异常*/public static void unZip(File srcFile, String destDirPath) throws RuntimeException {long start = System.currentTimeMillis();// 判断源文件是否存在if (!srcFile.exists()) {throw new RuntimeException(srcFile.getPath() + "所指文件不存在");}// 开始解压ZipFile zipFile = null;try {zipFile = new ZipFile(srcFile);Enumeration<?> entries = zipFile.entries();while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();// 如果是文件夹,就创建个文件夹if (entry.isDirectory()) {String dirPath = destDirPath + "/" + entry.getName();File dir = new File(dirPath);dir.mkdirs();} else {// 如果是文件,就先创建一个文件,然后用io流把内容copy过去File targetFile = new File(destDirPath + "/" + entry.getName());// 保证这个文件的父文件夹必须要存在if(!targetFile.getParentFile().exists()){targetFile.getParentFile().mkdirs();}targetFile.createNewFile();// 将压缩文件内容写入到这个文件中InputStream is = zipFile.getInputStream(entry);FileOutputStream fos = new FileOutputStream(targetFile);int len;byte[] buf = new byte[BUFFER_SIZE];while ((len = is.read(buf)) != -1) {fos.write(buf, 0, len);}// 关流顺序,先打开的后关闭fos.close();is.close();}}long end = System.currentTimeMillis();} catch (Exception e) {throw new RuntimeException("unzip error from ZipUtils", e);} finally {if(zipFile != null){try {zipFile.close();} catch (IOException e) {e.printStackTrace();}}}}}
grep "thread_1" -r /Users/xxx/Desktop/result