[关闭]
@TryLoveCatch 2021-10-21T06:52:42.000000Z 字数 5614 阅读 1538

Android基础之AS中的各种清除缓存操作

android android基础 gradle 清除缓存 全局搜索


Clean Project

点击 Clean Project 会执行 clean、:app:generateDebugSources task

其中执行 clean 时会找到根项目和所有子项目的 clean task,所以一般来讲,会执行两个 task,:clean 和 :app:clean。

他们区别在于:

:clean 删除 /build 文件夹
:app:clean 删除 /app/build 文件夹

Invalidate Cache/Restart

invalidate Caches/Restart 会删除 android studio 的缓存。
详细内容可查看 idea 的官方文档

这个缓存指的是代码更改记录的缓存,即local history,清除之后的效果:

无法查看 local history
所有项目都需要重新 build
这样看来,invalidate Caches / Restart 和 Rebuild Project 并没有什么区别。

删除 .idea/libraries

删除项目根目录下 .idea/libraries 目录的内容,或删除整个目录。

.idea/libraries 里的文件用来记录第三方库的 classes, javadoc 以及 sources 所对应的路径。

当更新依赖、切换git分支后,往往会出现某个库的代码报红的情况,这时候就是.idea/libraries 里的路径没有更新,所以找不到。

删除 /.idea/libraries 里的内容后,再次同步时,会重新生成。

删除 ~/.gradle/caches/transforms-1/files-1.1/

~/.gradle/caches/transforms-1/files-1.1/ 是 gradle 缓存的库解压后的目录。

/.idea/libraries 中的文件指向的路径就是这里。

删除该目录或其中某个库后,当再次执行 generateDebugSources 时,不会重新下载,会从 modules-2 (下面要讲的)目录下寻找并解压。

删除 ~/.gradle/caches/modules-2/files-2.1/

~/.gradle/caches/modules-2/files-2.1/ 是 gradle 缓存的库的源文件。

删除该目录或其中某个库后,当再次执行 generateDebugSources 时,会重新从网络下载。

/.idea/libraries 与 transforms-1 、modules-2

再理一遍它们的关系:

第一次执行 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。

Android如何全局搜索字节码中的内容?

每个组件都是aar依赖(aar变成了class文件) → 当想搜索某个字符串在哪个组件中使用的时候却查不到,如果不是class文件 可以搜到,如果是class文件 搜不到

文件遍历搜索法

  1. public class Grep {
  2. public static final String GRADLE_CACHE_PATH = "/Users/xxx/.gradle/caches/transforms-1/files-1.1";
  3. public static final String TARGET_PATH = "/Users/xxx/Desktop/result";
  4. private static int mCopyCount = 0;
  5. private static int mUnZipCount = 0;
  6. private static int mDeleteCount = 0;
  7. public static void main(String[] args) {
  8. File file = new File(GRADLE_CACHE_PATH);
  9. // 1 先把缓存中的文件拷贝到指定的文件夹中
  10. traverseCopy(file);
  11. System.out.println("traverseCopy file number ="+mCopyCount);
  12. // 2 把所有的jar文件解压出来到指定的文件夹中
  13. tarJarFile(TARGET_PATH);
  14. // 3 删除掉之前存在的jar文件
  15. deleteJarFile(TARGET_PATH);
  16. }
  17. /**
  18. * 遍历复制gradle cache 中的classes.jar到 指定目录中去
  19. *
  20. * @param file
  21. */
  22. private static void traverseCopy(File file) {
  23. File[] fs = file.listFiles();
  24. for (File f : fs) {
  25. if (f.isDirectory()) //若是目录,则递归打印该目录下的文件
  26. {
  27. traverseCopy(f);
  28. } else if (f.isFile() && "classes.jar".equals(f.getName())) //若是文件,直接打印
  29. {
  30. mCopyCount++;
  31. String sourcePath = f.getAbsolutePath();
  32. String aarName = getSimpleAarPathName(f.getAbsolutePath());
  33. String targetPath = getTargetPathName(aarName);
  34. File fileTargetFile = new File(targetPath);
  35. // 如果文件已经存在就不用去拷贝了
  36. if(fileTargetFile.exists()){
  37. continue;
  38. }
  39. copyFile(sourcePath, targetPath);
  40. }
  41. }
  42. }
  43. /**
  44. * 返回类似 homepage-api-2.5.30
  45. * @param path
  46. * @return
  47. */
  48. private static String getSimpleAarPathName(String path) {
  49. String aarNameFirst = path.substring(path.indexOf("files-1.1/") + 10, path.indexOf(".aar/"));
  50. String aarNameLast = path.substring(path.indexOf(".aar/") + 5, path.indexOf(".aar/") + 10);
  51. return aarNameFirst+"-"+aarNameLast;
  52. }
  53. private static String getTargetPathName(String aarName) {
  54. return TARGET_PATH + "/" + aarName +".jar";
  55. }
  56. public static void copyFile(String source, String target) {
  57. try (FileInputStream fis = new FileInputStream(new File(source));
  58. FileOutputStream fos = new FileOutputStream(new File(target))) {
  59. FileChannel sourceChannel = fis.getChannel();
  60. FileChannel targetChannel = fos.getChannel();
  61. MappedByteBuffer mappedByteBuffer = sourceChannel.map(FileChannel.MapMode.READ_ONLY, 0, sourceChannel.size());
  62. targetChannel.write(mappedByteBuffer);
  63. } catch (FileNotFoundException e) {
  64. e.printStackTrace();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. private static void deleteJarFile(String targetPath) {
  70. File[] fs = new File(targetPath).listFiles();
  71. for (File f : fs) {
  72. if (f.isFile() && f.getName().contains(".jar")) {
  73. mDeleteCount++;
  74. f.delete();
  75. }
  76. }
  77. System.out.println(" deleteJarFile file number = " + mDeleteCount);
  78. }
  79. /**
  80. * 把指定目录中的 classes.jar 解压到文件中
  81. */
  82. private static void tarJarFile(String targetPath) {
  83. File[] fs = new File(targetPath).listFiles();
  84. for (File f : fs) {
  85. if (f.isFile() && f.getName().contains(".jar")) {
  86. mUnZipCount++;
  87. String directory = f.getName().substring(0, f.getName().lastIndexOf(".jar"));
  88. String targetPathDirectory = TARGET_PATH + "/" + directory + "/";
  89. File targetPathDirectoryFile = new File(targetPathDirectory);
  90. // 如果已经创建过文件夹就不要去重复创建了
  91. if (targetPathDirectoryFile.exists()) {
  92. continue;
  93. }
  94. targetPathDirectoryFile.mkdirs();
  95. UnZipUtils.unZip(f, targetPathDirectory);
  96. }
  97. }
  98. System.out.println(" tarJarFile file number = " + mUnZipCount);
  99. }
  100. }
  1. /**
  2. * 对文件进行解压操作
  3. */
  4. public class UnZipUtils {
  5. private static final int BUFFER_SIZE = 2 * 1024;
  6. /**
  7. * zip解压
  8. * @param srcFile zip源文件
  9. * @param destDirPath 解压后的目标文件夹
  10. * @throws RuntimeException 解压失败会抛出运行时异常
  11. */
  12. public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
  13. long start = System.currentTimeMillis();
  14. // 判断源文件是否存在
  15. if (!srcFile.exists()) {
  16. throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
  17. }
  18. // 开始解压
  19. ZipFile zipFile = null;
  20. try {
  21. zipFile = new ZipFile(srcFile);
  22. Enumeration<?> entries = zipFile.entries();
  23. while (entries.hasMoreElements()) {
  24. ZipEntry entry = (ZipEntry) entries.nextElement();
  25. // 如果是文件夹,就创建个文件夹
  26. if (entry.isDirectory()) {
  27. String dirPath = destDirPath + "/" + entry.getName();
  28. File dir = new File(dirPath);
  29. dir.mkdirs();
  30. } else {
  31. // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
  32. File targetFile = new File(destDirPath + "/" + entry.getName());
  33. // 保证这个文件的父文件夹必须要存在
  34. if(!targetFile.getParentFile().exists()){
  35. targetFile.getParentFile().mkdirs();
  36. }
  37. targetFile.createNewFile();
  38. // 将压缩文件内容写入到这个文件中
  39. InputStream is = zipFile.getInputStream(entry);
  40. FileOutputStream fos = new FileOutputStream(targetFile);
  41. int len;
  42. byte[] buf = new byte[BUFFER_SIZE];
  43. while ((len = is.read(buf)) != -1) {
  44. fos.write(buf, 0, len);
  45. }
  46. // 关流顺序,先打开的后关闭
  47. fos.close();
  48. is.close();
  49. }
  50. }
  51. long end = System.currentTimeMillis();
  52. } catch (Exception e) {
  53. throw new RuntimeException("unzip error from ZipUtils", e);
  54. } finally {
  55. if(zipFile != null){
  56. try {
  57. zipFile.close();
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63. }
  64. }
  1. grep "thread_1" -r /Users/xxx/Desktop/result

参考

https://blog.csdn.net/Gdeer/article/details/83049615

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注