赞
踩
目录
首先非常感谢博主:
https://blog.csdn.net/tiny_cao/article/details/118386544
现将搭建Android Studio 搭建离线开发环境方法步骤做个总结:
a、 Android Studio 3.5.2安装包
b、Android SDK压缩包
c、.gradle 压缩包:外网环境下生成的C:/users[username]/.gradle下的文件压缩备内网使用
d、offline-gmaven-stable.zip(离线包管理工具):在AS下载页面找到"Offline components" 下载 “Google Maven dependencies”
安装 Android Studio,以下称为AS
将Android SDK压缩包解压到任意方便的位置,然后配置到AS中
把在线环境安装测试好 的 .gradle 压缩包,复制到离线环境,然后解压.gradle压缩包并拷贝或覆盖到 C:\users[username]\ 下,覆盖离线环境的 .gradle
在%USER_HOME%/.gradle下新建init.d/offline.gradle文件(如果没有文件则新建命名为init.d的文件夹,再在里面新建offline.gradle),offline.gradle文件里写以下代码:
- def reposDir = new File(System.properties['user.home'], ".android/manual-offline-m2")
- def repos = new ArrayList()
- reposDir.eachDir {repos.add(it) }
- repos.sort()
-
- allprojects {
- buildscript {
- repositories {
- for (repo in repos) {
- maven {
- name = "injected_offline_${repo.name}"
- url = repo.toURI().toURL()
- }
- }
- }
- }
- repositories {
- for (repo in repos) {
- maven {
- name = "injected_offline_${repo.name}"
- url = repo.toURI().toURL()
- }
- }
- }
- }
将offline-gmaven-stable.zip解压到 %USER_HOME%/.android/manual-offline-m2/下,manual-offline-m2这个文件如果没有自行创建
在外网环境下,搭建一个简单的demo,然后把内网项目中的所用到的依赖都加到build.gradle中,然后sync,最后运行项目(主要为了下载一些jar文件)
1、将下载下来的jar,即%USER_HOME%/.gradle/caches/modules-2/files-2.1中的文件,压缩,传递给内网使用
2、解压缩合并到内网中%USER_HOME%/.gradle/caches/modules-2/files-2.1中
到这里,将项目导入到AS中,编译发现根本找不到依赖,接下来非常重要的一步
在sdk的extras下创建m2repository文件夹,然后将%USER_HOME%/.gradle/caches/modules-2/files-2.1,全部copy到里面,然后运行下面的Java测试代码生成可认识的依赖(缓存中的文件比如:androidx.recyclerview,需要变为androidx/recyclerview)
- public class CopyTool {
- // 我的sdk的目录在e盘下,这里可根据自己的位置修改
- static String path = "E:\\android\\AndroidSDK\\Sdk\\extras\\m2repository";
- static String stopName = "m2repository";
-
- public static void main(String[] args) {
- System.out.println("Begin to copy");
- processDotForld();
- copyToLastForld();
- System.out.println("Copy finished");
- }
-
- /**
- * 处理文件夹中带点好的。;例如:D:/test/com.ifind.android/
- */
- public static void processDotForld() {
- File file = new File(path);
- if (file.exists()) {
- LinkedList<File> list = new LinkedList<>();
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- File file2 = files[i];
- if (file2.isDirectory()) {
- //文件夹
- File desFile = creatforld(file2);
- copyFileToDes(file2, desFile);
- } else {
- //文件//目前不存在
- }
- }
- }
- }
-
- /**
- * 文件夹拷贝
- *
- * @param source
- * @param des
- */
- public static void copyFileToDes(File source, File des) {
- try {
- copyDir(source.getPath(), des.getPath());
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
-
- /**
- * 文件夹拷贝
- *
- * @param sourcePath
- * @param newPath
- * @throws IOException
- */
- public static void copyDir(String sourcePath, String newPath) throws IOException {
- File file = new File(sourcePath);
- String[] filePath = file.list();
-
- if (!(new File(newPath)).exists()) {
- (new File(newPath)).mkdir();
- }
-
- for (int i = 0; i < filePath.length; i++) {
- if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
- copyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
- }
-
- if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
- copyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
- }
-
- }
- }
-
- public static void copyFile(String oldPath, String newPath) throws IOException {
- File oldFile = new File(oldPath);
- File file = new File(newPath);
- FileInputStream in = new FileInputStream(oldFile);
- FileOutputStream out = new FileOutputStream(file);
-
- byte[] buffer = new byte[2097152];
-
- //while((in.read(buffer)) != -1){
- // out.write(buffer);
- //}
-
- DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
- DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(out));
-
- int length;
- while ((length = dis.read(buffer)) != -1) {
- dos.write(buffer, 0, length);
- }
- dos.flush();
- dos.close();
- dis.close();
- }
-
-
- /**
- * 创建文件夹
- *
- * @param file
- */
- public static File creatforld(File file) {
- String path = file.getAbsolutePath();
- if (path != null) {
- String temp = "files-2.1";
- temp = stopName;
- String temS[] = path.split(temp);
-
- if (temS != null && temS.length == 2) {
- String firstName = temS[0];
- String dotName = temS[1];
- if (dotName != null) {
- String[] lasts = dotName.split("\\.");
- int count = lasts.length;
- if (count < 2) {
- return null;
- }
- String pathNew = firstName + temp;
- for (int i = 0; i < count; i++) {
- if (i == 0) {
- pathNew = pathNew + lasts[i];
- } else {
- pathNew = pathNew + "\\" + lasts[i];
- }
- }
- if (pathNew != null && !pathNew.equals("")) {
- File fileForld = new File(pathNew);
- if (!fileForld.exists()) {
- fileForld.mkdirs();
- }
- return fileForld;
- }
- }
- }
- }
- return null;
- }
-
- public static ArrayList<File> getFile(File file) {
- ArrayList<File> list = new ArrayList<>();
- if (file.isDirectory()) {
- File[] filesTemp = file.listFiles();
- for (int i = 0; i < filesTemp.length; i++) {
- ArrayList<File> result = getFile(filesTemp[i]);
- list.addAll(result);
- }
-
- } else {
- list.add(file);
- }
- return list;
- }
-
-
- // 创建目录
- public static boolean createDir(String destDirName) {
- File dir = new File(destDirName);
- if (dir.exists()) {// 判断目录是否存在
- System.out.println("创建目录失败,目标目录已存在!");
- return false;
- }
- if (!destDirName.endsWith(File.separator)) {// 结尾是否以"/"结束
- destDirName = destDirName + File.separator;
- }
- if (dir.mkdirs()) {// 创建目标目录
- System.out.println("创建目录成功!" + destDirName);
- return true;
- } else {
- System.out.println("创建目录失败!");
- return false;
- }
- }
-
-
- public static void copyToLastForld() {
- File file = new File(path);
- if (file.exists()) {
- LinkedList<File> list = new LinkedList<>();
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- File file2 = files[i];
- if (file2.isDirectory()) {
- //文件夹
- proceessForld(file2);
- } else {
- //文件//目前不存在
- }
- }
- }
- }
-
-
- private static void proceessForld(File file) {
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- File file2 = files[i];
- if (file2.isDirectory()) {
- //文件夹
- proceessForld(file2);
- } else {
- //文件//目前不存在//判断是否进行拷贝
- try {
- proceessFile(file2);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
-
-
- private static void proceessFile(File file) throws FileNotFoundException {
- if (file != null) {
- String path = file.getAbsolutePath();
- if (path != null) {
- String[] lasts = splitString(path);
- if (lasts != null && lasts.length > 0) {
- int count = lasts.length;
- String last = lasts[count - 1];
- String last2 = lasts[count - 2];
-
-
- if (last2 != null && last2.length() > 20) {
- //拷贝到上一级目录
- String des = null;
- if (count < 2) {
- return;
- }
- for (int i = 0; i < count - 2; i++) {
- if (i == 0) {
- des = lasts[i];
- } else {
- des = des + "\\\\" + lasts[i];
- }
- }
- des = des + "\\\\" + last;
- String strParentDirectory = file.getParent();
- File parentFile = new File(strParentDirectory);
- strParentDirectory = parentFile.getParent() + "\\" + last;
- copy(file, path, strParentDirectory);
- } else {
- // System.out.println("source = "+path);
- }
- // System.out.println("source = "+path);
- // System.out.println("des = "+des);
- }
- }
- }
- }
-
-
- private static String[] splitString(String path) {
- String[] lasts = null;
- lasts = path.split("\\\\");
- int count = lasts.length;
- boolean isFirst = true;
- for (int i = 0; i < count; i++) {
- String str = lasts[i];
- if (str != null && str.contains(".")) {
- if (isFirst) {
- isFirst = false;
- System.out.println("\n\n\n\n");
- System.out.println("path=" + path + "");
- }
- System.out.println("str=" + str + "");
- }
- }
- return lasts;
- }
-
- /**
- * copy动作
- *
- * @param file
- * @param source
- * @param des
- * @throws FileNotFoundException
- */
- private static void copy(File file, String source, String des) throws FileNotFoundException {
- if (file != null) {
- FileInputStream fis = null;
- FileOutputStream fot = null;
- byte[] bytes = new byte[1024];
- int temp = 0;
- File desFile = new File(des);
- if (desFile.exists()) {
- return;
- }
- try {
- fis = new FileInputStream(file);
- fot = new FileOutputStream(desFile);
- while ((temp = fis.read(bytes)) != -1) {
- fot.write(bytes, 0, temp);
- fot.flush();
-
-
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (fot != null) {
- try {
- fot.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
-
- }
- }
-
-
- private static String getContent(String content) {
- String str = content;
- if (content != null && content.length() > 4) {
- str = content.substring(0, 4);
- }
- return str;
- }
- }
在buildscirp和allprojects中添加maven依赖:
- maven { url ‘file://E:\android\AndroidSDK\Sdk\extras\m2repository’ }
-
-
-
- buildscript {
- repositories {
- google()
- jcenter()
- // 这里写上上面代码生成的文件路径,参考我的
- maven { url 'file://E:\\android\\AndroidSDK\\Sdk\\extras\\m2repository' }
- maven { url 'https://jitpack.io' }
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:3.5.2'
-
- // NOTE: Do not place your application dependencies here; they belong
- // in the individual module build.gradle files
- }
- }
-
- allprojects {
- repositories {
- maven { url 'file://E:\\android\\AndroidSDK\\Sdk\\extras\\m2repository' }
- maven { url 'https://jitpack.io' }
- }
- }
-
- task clean(type: Delete) {
- delete rootProject.buildDir
- }
工作那么多年,第一次写博客,参考正在上传…重新上传取消飞奔的蜗牛丶,再次感谢
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。