当前位置:   article > 正文

如何在离线情况下使用Android Studio正常运行项目_android studio不联网可以运行吗

android studio不联网可以运行吗

 

前段时间,项目组需要进行代码安全扫描,并且扫描环境是fortify的离线环境(无奈),最无奈的是,需要在一台离线服务器上搭建Android Studio环境,并且需要把项目整改成可以离线编译的,最后踩了不少坑才完成了,整理成文章望大家能够分享经验不用再浪费时间了!注:fortify扫描离线Android项目时,需要在项目根目录下添加“build-cache”和“files-2.1”(此处files-2.1即为联网电脑的.gradle下的files-2.1文件夹,不用做迁移)文件夹,这样能够防止代码编译报错

原始电脑路径:C:\User\aaa\xxx

离线电脑路径:C:\User\bbb\xxx

1.File->Setting->Build,Excecution,Deployment->Gradle中的Offline work必须勾选!将C:\Users\aaa\.android\build-cache下对应build版本的文件夹整个拷到另一台电脑的C:\Users\bbb\.android\build-cache下:

2.将C:\Users\aaa\.gradle 下每个文件夹中的各个文件夹都要拷贝到另外一台电脑的C:\Users\bbb\.gradle(路径可自定义,这个路径就是项目引用的gradle路径)文件夹下,如果考虑内存占用,就只拷贝对应版本的文件夹即可。

3.

3.1将C:\Users\aaa\.gradle\caches\modules-2\files-2.1(jar包所在路径,对于android中自带的jar包,可以通过图2的方式下载https://developer.android.google.cn/studio#downloads图3)中所有有关项目的jar包都要放到一个叫做C:\\Users\\bbb\\xxx\extras\m2repository(自定义路径,该路径就是项目的build.gradle文件中最后引用的路径maven{url 'file://C:\\Users\\bbb\\xxx\\extras\\m2repository'}) 的文件夹下,此处需要注意一点,在C:\Users\aaa\.gradle\caches\modules-2\files-2.1路径下的目录结构是com.android.xxx/common/1.1.1/xxx/xxx.jar这个格式的,而在C:\Users\xxx\extras\m2repository路径下的文件目录结构必须是com/android/xxx格式的(这样才能在项目的build.gradle中引用),那么有两种方式可以实现:1)用代码(代码在文章最后附上,拷贝别人的代码,找到链接之后会附上链接)进行两个文件夹中文件的整体迁移;2)也可以手动拷贝对应的每个文件,如果不嫌麻烦的话(不建议使用)

 

3.2对于项目中使用implementation 'xxx'方式引用的jar包,都要在Maven仓库中找到对应的jar包放入到libs文件夹下,这一步是一个重复操作,操作比较简单,拷贝完之后在app的build.gradle文件中添加libs文件夹的引用。注:需要在libs文件夹下添加android.jar,防止找不到对应的android源包的引用。

  1. apply plugin: 'com.android.application'
  2. android {
  3. compileSdkVersion 28
  4. buildToolsVersion '28.0.3'
  5. defaultConfig {
  6. applicationId "com.xxx.xxx"
  7. minSdkVersion 26
  8. targetSdkVersion 28
  9. versionCode 15
  10. versionName "2.1.0"
  11. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  12. packagingOptions {
  13. exclude 'META-INF/proguard/android-annotations.pro'
  14. }
  15. ndk {
  16. abiFilters "armeabi"
  17. }
  18. multiDexEnabled true
  19. }
  20. buildTypes {
  21. release {
  22. buildConfigField "boolean", "LOG_DEBUG", "false" //false不显示log true显示log
  23. minifyEnabled false
  24. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  25. shrinkResources false
  26. signingConfig signingConfigs.release
  27. debuggable false
  28. }
  29. debug {
  30. buildConfigField "boolean", "LOG_DEBUG", "true" //显示log
  31. debuggable false
  32. }
  33. }
  34. aaptOptions { cruncherEnabled = false }
  35. dexOptions {
  36. javaMaxHeapSize "4g"
  37. }
  38. lintOptions {
  39. checkReleaseBuilds false
  40. // Or, if you prefer, you can continue to check for errors in release builds,
  41. // but continue the build even when errors are found:
  42. abortOnError false
  43. }
  44. productFlavors {
  45. }
  46. allprojects {
  47. repositories {
  48. maven { url "https://jitpack.io" }
  49. }
  50. }
  51. compileOptions {
  52. sourceCompatibility '1.8'
  53. targetCompatibility '1.8'
  54. }
  55. repositories {
  56. flatDir {
  57. dirs 'libs'
  58. }
  59. }
  60. }
  61. dependencies {
  62. //添加libs文件夹下jar引用
  63. implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
  64. }

3.3将整个xxx\extras文件夹拷贝到另一台电脑上(比如路径为C:\bbb\),并且在本地项目的build.gradle文件中添加maven {url 'file://C:\\Users\\bbb\\xxx\\extras\\m2repository'}

 

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. buildscript {
  3. repositories {
  4. //把所有引用网络的代码删除,只保留本地对应的maven仓库路径
  5. // mavenCentral()
  6. // jcenter{
  7. // url 'http://jcenter.bintray.com'
  8. // }
  9. // maven {
  10. // url 'https://maven.google.com/'
  11. // name 'Google'
  12. // }
  13. // maven {
  14. // url "http://jcenter.bintray.com"
  15. // }
  16. // google()
  17. maven {
  18. url "file://C:\\Users\\bbb\\xxx\\extras\\m2repository"
  19. }
  20. }
  21. dependencies {
  22. classpath 'com.android.tools.build:gradle:3.4.2'
  23. // NOTE: Do not place your application dependencies here; they belong
  24. // in the individual module build.gradle files
  25. }
  26. }
  27. allprojects {
  28. repositories {
  29. // mavenCentral()
  30. // google()
  31. // jcenter{
  32. // url 'http://jcenter.bintray.com'
  33. // }
  34. //
  35. // maven {
  36. // url "http://jcenter.bintray.com"
  37. // }
  38. // maven {
  39. // url 'https://maven.google.com/'
  40. // name 'Google'
  41. // maven { url "https://jitpack.io" }
  42. // }
  43. // maven {
  44. // url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'
  45. // }
  46. }
  47. }
  48. task clean(type: Delete) {
  49. delete rootProject.buildDir
  50. }

4.重新同步代码,编译成功!

附迁移文件代码:

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

 个人原创文字,总结了最近遇到的一些离线编译问题,如果有错误欢迎大家批评指正!

如有疑问可以微信:cai-niao-bu-ke-yi; QQ:1125325256 留言 个人网站:www.sevenyoung.net(不日即将开启,欢迎大家访问)

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号