解决Android SDK的离线文档(Docs)加载速度慢问题

原文:http://my.oschina.net/willedward/blog/91260

sdk下载的android 离线文档加载速度很慢 ,查找了一些资料发现可以通过设置浏览器脱机工作解决, 究其原因,是因为html中有大量在线加载js的代码

原文中通过一小段java注释html中的某行Javascript代码 

我先设置了ANDROID_SDK_HOME  但还是提示 Detected the Android SDK Installation path:


于是直接设置了sdk目录,最后成功解决 代码如下


package com.sadhu.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

public class Test {private static final String ANDROID_SDK_HOME = "ANDROID_SDK_HOME";
private static final String TAG = "";
private static String androidSDKHome;

public static void main(String[] args) {
    Map map = System.getenv();
    //这里填写android sdk的目录
    androidSDKHome = "G:\\developToop\\adt-bundle-windows-x86_64-20140702\\sdk";
    if (androidSDKHome != null) {
        System.out.println("Detected the Android SDK Installation path:"
                + androidSDKHome);
        File docs = new File(androidSDKHome + "\\docs");
        System.out.println("It will take several minutes, please wait...");
        traverse(docs);
        System.out
                .println("Operation Down!!! Now you can visit Android Doc without waiting...");
    } else {
        System.out
                .println("Please configure the Android SDK Environment variable (ANDROID_SDK_HOME)!");
    }
}

/**
 * 遍历Android SDK安装目录下的docs目录
 * 
 * @param dir
 */
private static void traverse(File dir) {
    if (dir.isDirectory()) {
        File[] files = dir.listFiles(new FileFilter() {

            public boolean accept(File file) {
                if (file.isFile() && file.getName().endsWith(".html")) {
                    BufferedReader reader = null;
                    try {
                        reader = new BufferedReader(new FileReader(file));
                        String buffer;
                        while ((buffer = reader.readLine()) != null) {
                            if (buffer.trim().equalsIgnoreCase(TAG)) {
                                return true;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else if (file.isDirectory()) {
                    return true;
                }
                return false;
            }
        });
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("Scanning Folder:"
                        + file.getAbsolutePath());
                traverse(file);
            } else {
                doChange(file);
            }
        }
    }
}

/**
 * 注释html中的某行Javascript代码
 * 
 * @param file
 * @return
 */
private static boolean doChange(File file) {
    System.out.println("Extracting File:" + file.getAbsolutePath() + "...");
    String desPath = file.getParent() + "\\" + UUID.randomUUID().toString() + ".html";
    File des = new File(desPath);
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try {
        des.createNewFile();
        reader = new BufferedReader(new FileReader(file));
        writer = new BufferedWriter(new FileWriter(des));
        String buffer;
        while ((buffer = reader.readLine()) != null) {
            if (buffer.trim().equalsIgnoreCase(TAG)) {
                buffer = "";
            }
            writer.write(buffer + "\n");
            writer.flush();
        }
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
            writer.close();
            file.delete();
            des.renameTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}


}



耐心等待 几分钟后 提示Operation Down!!! Now you can visit Android Doc without waiting...

然后再打开index.html ,enjoy it吧


你可能感兴趣的:(解决Android SDK的离线文档(Docs)加载速度慢问题)