Android 源码下载

Win10环境下 安卓源码下载

不涉及编译,我也没编过,只是提供尽可能快速下载的方式,就用java的环境就可以
执行脚本时 git 对应进度信息没有及时刷出来因为git的下载就是下载的一个zip包,下载完成后一起刷出来的。
注意,在下载framework的base 库的时候 这个库有几个G,要等很长时间,还有概率下载失败,可以直接创建对应目录,git 命令手动下载或重试
或加上 --depth 1 ,这个参数意请具体百度。

git clone https://aosp.tuna.tsinghua.edu.cn/platform/frameworks/base.git --depth 1

源码的两个源一个官方 一个清华镜像

新建文件夹进入目录,执行命令
git clone https://android.googlesource.com/platform/manifest.git
//没有梯子使用清华源
git clone https://aosp.tuna.tsinghua.edu.cn/platform/manifest.git

查看相关分支

clone 完成后 cd manifest 文件夹
git branch -a 查看所有分支
切换到要下载源码的对应分支
此时会看到文件目录下有个default.xml 其实里面对应的都是不同的git 仓库路径

脚本下载 或安装repo ,repo 这里不说了

因为我是win10,所以用脚本 。
看网上都是python的脚本,因为是家里的游戏本,所以转成java 的用java 脚本下载
进度没有及时展示出来,要确认是否正在下载去对应的文件夹右键属性查看下大小就能知道了

package bundle;

import java.io.File;
import java.io.IOException;
import java.security.KeyStore.PrivateKeyEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.naming.InsufficientResourcesException;
import javax.naming.directory.DirContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class AndroidSource {

	// import xml.dom.minidom
	// import os
	// from subprocess import call
	//
	// # 1. 修改为源码要保存的路径
	// rootdir = "D:/androidSourceCode/Android_6_0_1"
	//
	// # 2. 设置 git 安装的路径
	// git = "D:/Git/bin/git.exe"
	//
	// # 3. 修改为第一步中 manifest 中 default.xml 保存的路径
	// dom = xml.dom.minidom.parse("D:/androidSourceCode/manifest/default.xml")
	// root = dom.documentElement
	//
	// #prefix = git + " clone https://android.googlesource.com/"
	// # 4. 没有梯子使用清华源下载
	// prefix = git + " clone https://aosp.tuna.tsinghua.edu.cn/"
	// suffix = ".git"
	//
	// if not os.path.exists(rootdir):
	// os.mkdir(rootdir)
	//
	// for node in root.getElementsByTagName("project"):
	// os.chdir(rootdir)
	// d = node.getAttribute("path")
	// last = d.rfind("/")
	// if last != -1:
	// d = rootdir + "/" + d[:last]
	// if not os.path.exists(d):
	// os.makedirs(d)
	// os.chdir(d)
	// cmd = prefix + node.getAttribute("name") + suffix
	// call(cmd)

	private static String inPutFile = "F:\\sources\\manifest\\default.xml";
	private static String rootDir = "F:\\sources\\platform10";
//	private static String gitPath = "E:\\Program Files\\Git\\bin\\git.exe";
	private static String gitPath = "git";

	// private static String sourcePath = " clone
	// https://android.googlesource.com/";
	private static String sourcePath = " clone https://aosp.tuna.tsinghua.edu.cn/";
	private static String suffix = ".git";

	private static String prefix = gitPath + sourcePath;

	private static String[] whitelist = new String[] { "frameworks" };

	public static void main(String[] args) {
		// 打印环境变量 没配git环境变量 还是写死吧
		// printfSystemProperty();

		File rootDirFile = new File(rootDir);
		if (!rootDirFile.exists()) {
			rootDirFile.mkdirs();
		}

		parseXml();

	}

	private static void parseXml() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.创建DocumentBuilder对象
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document d = builder.parse(inPutFile);
			NodeList sList = d.getElementsByTagName("project");
			// element(sList);
			System.out.println("pase xml start");
			node(sList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void node(NodeList list) {
		System.out.println("loopNode list"+ list.getLength());
		for (int i = 0; i <list.getLength() ; i++) {
            Node node = list.item(i);
            String path = node.getAttributes().getNamedItem("path").getTextContent();
            String name = node.getAttributes().getNamedItem("name").getTextContent();
           
            
            
            int  index = path.lastIndexOf("/");
            String nodeDir = path;
            if(index!=-1) {
            	nodeDir = path.substring(0,index);
            }
            
            if(!contains(path)) {
            	continue;
            }
       	 	 nodeDir = rootDir+"/"+nodeDir+"/";

            File file = new File(nodeDir);
            
            if(!file.exists()) {
            	file.mkdirs();
            }
            
            String cmd = prefix+ name+suffix;	
            System.out.println(nodeDir+"--"+ cmd);
            excute(cmd, nodeDir);
            
        }
 
	}
	
	private static boolean contains(String item) {
		for(String white:whitelist) {
			if(item.startsWith(white)) {
				return true;
			}
		}
		
		
		return false;
	}

	private static void excute(String cmds, String path) {
		Process proc = null;
		try {
			System.out.println("command start: " + cmds + "开始");
			proc = Runtime.getRuntime().exec(cmds, null, new File(path));
			ArrayList<String> logList = new ArrayList<>();
			DamenThread2 stdoutUtil = new DamenThread2(proc.getInputStream(), logList);
			DamenThread2 erroroutUtil = new DamenThread2(proc.getErrorStream(), logList);
			stdoutUtil.start();
			erroroutUtil.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (proc != null) {
			try {
				int exitcode = proc.waitFor();
				proc.destroy();
				System.out.println(exitcode);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("command start: " + cmds + "结束");

		}
	}

	private static void printfSystemProperty() {
		Map map = System.getenv();
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Entry entry = (Entry) it.next();
			System.out.print(entry.getKey() + "=");
			System.out.println(entry.getValue());
		}
	}
}


package bundle;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;


public class DamenThread2 implements Runnable{
	 
	    // 设置读取的字符编码
	    private String character = "GB2312";
	    private List<String> list;
	    private InputStream inputStream;
	    public DamenThread2(InputStream inputStream, List<String> list) {
	        this.inputStream = inputStream;
	        this.list = list;
	    }

	    public void start() {
	        Thread thread = new Thread(this);
	        thread.setDaemon(true);//将其设置为守护线程
	        thread.start();
	        
	    }
	    
	  
	    public void run() {
	        BufferedReader br = null;
	        try {
	            br = new BufferedReader(new InputStreamReader(inputStream, character));
	            String line = null;
	            while ((line = br.readLine()) != null) {
	                if (line != null) {
	                	System.out.println(line);
	                }
	            }
	               
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                //释放资源
	                inputStream.close();
	                br.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }

	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值