当前位置:   article > 正文

记录AndroidStudio搭建离线环境_内网安装android studio

内网安装android studio

最近要做内网离线开发,记录一下离线AndroidStudio配置

联网安装就不赘述了,这里把联网配置好的东西都拷贝到离线电脑上,然后逐步配置

1.C盘下.gradle文件夹整体打压缩包

2.sdk目录除去system-images文件夹(太大了,没什么用),其他所有文件夹打压缩包

3.androidstudio安装包,我下载的是3.4.1,放上官方地址

https://developer.android.google.cn/studio/archive.html#android-studio-2-3-2?utm_source=androiddevtools&utm_medium=website

4.下载官方提供的离线包,地址还是上边的官网地址

 

这四步拷贝到离线电脑,接下来开始安装

1.安装Androidstudio

2.将.gradle放到C盘user当前用户目录下

3.sdk放到自己想放的位置

4.androidstudio设置离线工作模式 setting -> build.execution,deployment -> gradle , 勾选offline work,或者再右侧gradle选择离线按钮

5.设置离线gradle,上边位置输入gradle路径

6.将下载的离线库解压到 C:user/当前用户/.android/manual-offline-m2,最后边文件夹应该没有,手动创建,这里需注意,最终依赖的路径应该是C:user/当前用户/.android/manual-offline-m2/gmaven_stable/各种依赖库...,解压压缩包的时候可能会有一个offline-gmaven_stable记得把这一层删掉

7.创建offline.gradle文件,目录为

文件内容如下

  1. def reposDir = new File(System.properties['user.home'], ".android/manual-offline-m2")
  2. def repos = new ArrayList()
  3. reposDir.eachDir {repos.add(it) }
  4. repos.sort()
  5. allprojects {
  6. buildscript {
  7. repositories {
  8. for (repo in repos) {
  9. maven {
  10. name = "injected_offline_${repo.name}"
  11. url = repo.toURI().toURL()
  12. }
  13. }
  14. }
  15. }
  16. repositories {
  17. for (repo in repos) {
  18. maven {
  19. name = "injected_offline_${repo.name}"
  20. url = repo.toURI().toURL()
  21. }
  22. }
  23. }
  24. }

8.到这里新建的android项目应该没问题了,下边给出如何添加其他依赖库的方式

这个目录下放的是依赖缓存,把联网电脑这里的依赖包拷贝到离线电脑的官方依赖库,基本都有了,如果需要添加新的,下载jar包按照官方依赖库的文件层级目录放进去就行,这里说一下这里的jar包层级目录跟官方依赖库的层级是不一样的,现在要做的就是先把这里的层级目录改变然后拷贝到官方依赖库就可以了

将这个文件夹先拷贝到一个新建文件夹里方便操作,以防修改我们本来的gradle,我用的位置是D:\\test\\files-2.1,给出一个工具类,运行一下就行了

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.LinkedList;
  8. import java.io.BufferedInputStream;
  9. import java.io.BufferedOutputStream;
  10. import java.io.DataInputStream;
  11. import java.io.DataOutputStream;
  12. /**
  13. * 将Android中使用的gradle缓存目录中的jar包重新处理路径,用于内网离线构建
  14. * @author yhh
  15. *
  16. */
  17. public class CopyTool {
  18. // static String path = "D:\\Androidstudio2.3.3\\Android Studio\\gradle\\m2repository";
  19. // static String path = "D:\\gradle_cache\\files-2.1";
  20. static String path = "D:\\test\\files-2.1"; //C:\Users\Administrator\.gradle\caches\modules-2\files-2.1
  21. // static String stopName= "files-2.1";
  22. static String stopName = "files-2.1";
  23. public static void main(String[] args) {
  24. System.out.println("Begin to copy");
  25. processDotForld();
  26. copyToLastForld();
  27. System.out.println("Copy finished");
  28. }
  29. /**
  30. * 处理文件夹中带点好的。;例如:D:/test/com.ifind.android/
  31. */
  32. public static void processDotForld() {
  33. File file = new File(path);
  34. if (file.exists()) {
  35. LinkedList<File> list = new LinkedList<>();
  36. File[] files = file.listFiles();
  37. for (int i = 0; i < files.length; i++) {
  38. File file2 = files[i];
  39. if (file2.isDirectory()) {
  40. //文件夹
  41. File desFile = creatforld(file2);
  42. copyFileToDes(file2, desFile);
  43. } else {
  44. //文件//目前不存在
  45. }
  46. }
  47. }
  48. }
  49. /**
  50. * 文件夹拷贝
  51. *
  52. * @param source
  53. * @param des
  54. */
  55. public static void copyFileToDes(File source, File des) {
  56. try {
  57. copyDir(source.getPath(), des.getPath());
  58. } catch (Exception e) {
  59. // TODO: handle exception
  60. }
  61. }
  62. /**
  63. * 文件夹拷贝
  64. *
  65. * @param sourcePath
  66. * @param newPath
  67. * @throws IOException
  68. */
  69. public static void copyDir(String sourcePath, String newPath) throws IOException {
  70. File file = new File(sourcePath);
  71. String[] filePath = file.list();
  72. if (!(new File(newPath)).exists()) {
  73. (new File(newPath)).mkdir();
  74. }
  75. for (int i = 0; i < filePath.length; i++) {
  76. if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
  77. copyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
  78. }
  79. if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
  80. copyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
  81. }
  82. }
  83. }
  84. public static void copyFile(String oldPath, String newPath) throws IOException {
  85. File oldFile = new File(oldPath);
  86. File file = new File(newPath);
  87. FileInputStream in = new FileInputStream(oldFile);
  88. FileOutputStream out = new FileOutputStream(file);
  89. byte[] buffer = new byte[2097152];
  90. //while((in.read(buffer)) != -1){
  91. // out.write(buffer);
  92. //}
  93. DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
  94. DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(out));
  95. int length;
  96. while ((length = dis.read(buffer)) != -1) {
  97. dos.write(buffer, 0, length);
  98. }
  99. dos.flush();
  100. dos.close();
  101. dis.close();
  102. }
  103. /**
  104. * 创建文件夹
  105. *
  106. * @param file
  107. */
  108. public static File creatforld(File file) {
  109. String path = file.getAbsolutePath();
  110. if (path != null) {
  111. String temp = "files-2.1";
  112. temp = stopName;
  113. String temS[] = path.split(temp);
  114. if (temS != null && temS.length == 2) {
  115. String firstName = temS[0];
  116. String dotName = temS[1];
  117. if (dotName != null) {
  118. String[] lasts = dotName.split("\\.");
  119. int count = lasts.length;
  120. if (count < 2) {
  121. return null;
  122. }
  123. String pathNew = firstName + temp;
  124. for (int i = 0; i < count; i++) {
  125. if (i == 0) {
  126. pathNew = pathNew + lasts[i];
  127. } else {
  128. pathNew = pathNew + "\\" + lasts[i];
  129. }
  130. }
  131. if (pathNew != null && !pathNew.equals("")) {
  132. File fileForld = new File(pathNew);
  133. if (!fileForld.exists()) {
  134. fileForld.mkdirs();
  135. }
  136. return fileForld;
  137. }
  138. }
  139. }
  140. }
  141. return null;
  142. }
  143. public static ArrayList<File> getFile(File file) {
  144. ArrayList<File> list = new ArrayList<>();
  145. if (file.isDirectory()) {
  146. File[] filesTemp = file.listFiles();
  147. for (int i = 0; i < filesTemp.length; i++) {
  148. ArrayList<File> result = getFile(filesTemp[i]);
  149. list.addAll(result);
  150. }
  151. } else {
  152. list.add(file);
  153. }
  154. return list;
  155. }
  156. // 创建目录
  157. public static boolean createDir(String destDirName) {
  158. File dir = new File(destDirName);
  159. if (dir.exists()) {// 判断目录是否存在
  160. System.out.println("创建目录失败,目标目录已存在!");
  161. return false;
  162. }
  163. if (!destDirName.endsWith(File.separator)) {// 结尾是否以"/"结束
  164. destDirName = destDirName + File.separator;
  165. }
  166. if (dir.mkdirs()) {// 创建目标目录
  167. System.out.println("创建目录成功!" + destDirName);
  168. return true;
  169. } else {
  170. System.out.println("创建目录失败!");
  171. return false;
  172. }
  173. }
  174. public static void copyToLastForld() {
  175. File file = new File(path);
  176. if (file.exists()) {
  177. LinkedList<File> list = new LinkedList<>();
  178. File[] files = file.listFiles();
  179. for (int i = 0; i < files.length; i++) {
  180. File file2 = files[i];
  181. if (file2.isDirectory()) {
  182. //文件夹
  183. proceessForld(file2);
  184. } else {
  185. //文件//目前不存在
  186. }
  187. }
  188. }
  189. }
  190. private static void proceessForld(File file) {
  191. File[] files = file.listFiles();
  192. for (int i = 0; i < files.length; i++) {
  193. File file2 = files[i];
  194. if (file2.isDirectory()) {
  195. //文件夹
  196. proceessForld(file2);
  197. } else {
  198. //文件//目前不存在//判断是否进行拷贝
  199. try {
  200. proceessFile(file2);
  201. } catch (FileNotFoundException e) {
  202. // TODO Auto-generated catch block
  203. e.printStackTrace();
  204. }
  205. }
  206. }
  207. }
  208. private static void proceessFile(File file) throws FileNotFoundException {
  209. if (file != null) {
  210. String path = file.getAbsolutePath();
  211. if (path != null) {
  212. String[] lasts = splitString(path);
  213. if (lasts != null && lasts.length > 0) {
  214. int count = lasts.length;
  215. String last = lasts[count - 1];
  216. String last2 = lasts[count - 2];
  217. if (last2 != null && last2.length() > 20) {
  218. //拷贝到上一级目录
  219. String des = null;
  220. if (count < 2) {
  221. return;
  222. }
  223. for (int i = 0; i < count - 2; i++) {
  224. if (i == 0) {
  225. des = lasts[i];
  226. } else {
  227. des = des + "\\\\" + lasts[i];
  228. }
  229. }
  230. des = des + "\\\\" + last;
  231. String strParentDirectory = file.getParent();
  232. File parentFile = new File(strParentDirectory);
  233. strParentDirectory = parentFile.getParent() + "\\" + last;
  234. copy(file, path, strParentDirectory);
  235. } else {
  236. // System.out.println("source = "+path);
  237. }
  238. // System.out.println("source = "+path);
  239. // System.out.println("des = "+des);
  240. }
  241. }
  242. }
  243. }
  244. private static String[] splitString(String path) {
  245. String[] lasts = null;
  246. lasts = path.split("\\\\");
  247. int count = lasts.length;
  248. boolean isFirst = true;
  249. for (int i = 0; i < count; i++) {
  250. String str = lasts[i];
  251. if (str != null && str.contains(".")) {
  252. if (isFirst) {
  253. isFirst = false;
  254. System.out.println("\n\n\n\n");
  255. System.out.println("path=" + path + "");
  256. }
  257. System.out.println("str=" + str + "");
  258. }
  259. }
  260. return lasts;
  261. }
  262. /**
  263. * copy动作
  264. *
  265. * @param file
  266. * @param source
  267. * @param des
  268. * @throws FileNotFoundException
  269. */
  270. private static void copy(File file, String source, String des) throws FileNotFoundException {
  271. if (file != null) {
  272. FileInputStream fis = null;
  273. FileOutputStream fot = null;
  274. byte[] bytes = new byte[1024];
  275. int temp = 0;
  276. File desFile = new File(des);
  277. if (desFile.exists()) {
  278. return;
  279. }
  280. try {
  281. fis = new FileInputStream(file);
  282. fot = new FileOutputStream(desFile);
  283. while ((temp = fis.read(bytes)) != -1) {
  284. fot.write(bytes, 0, temp);
  285. fot.flush();
  286. }
  287. } catch (IOException e) {
  288. e.printStackTrace();
  289. } finally {
  290. if (fis != null) {
  291. try {
  292. fis.close();
  293. } catch (IOException e) {
  294. e.printStackTrace();
  295. }
  296. }
  297. if (fot != null) {
  298. try {
  299. fot.close();
  300. } catch (IOException e) {
  301. e.printStackTrace();
  302. }
  303. }
  304. }
  305. }
  306. }
  307. private static String getContent(String content) {
  308. String str = content;
  309. if (content != null && content.length() > 4) {
  310. str = content.substring(0, 4);
  311. }
  312. return str;
  313. }
  314. }

等处理完,在文件夹里按时间排序,修改前文件夹名称里会带有.的路径,修改后的就是一层一层的了,把修改的文件夹复制到官方依赖库里就可以了

9.到这里就配置完成了,去studio重新编译一下就可以了,这里注意一点,在project下的build.gradle里之前依赖的仓库都不能用了,都注释掉

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/298619
推荐阅读
相关标签
  

闽ICP备14008679号