android studio依赖离线配置

android studio依赖离线配置

gradle设置

1.目录结构切换为project模式,打开工程目录下gradle/wrapper/gradle-wrapper.properties文件
2.设置gradle本地依赖包位置

项目原本的配置distributionUrl为远端gradle地址。有两种模式可设置:

远端:以http、https为前缀,当更改gradle版本时,会先到系统设置的gradle缓存目录寻找,如果未寻找到会在distributionUrl配置的地址下载到本地gradle缓存目录中并依赖。

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-5.1.1-all.zip

本地:以files为前缀,当更改版本时会直接到配置的路径在寻找gradle依赖包

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=file:///D:/gradle/wrapper/dists/gradle-5.1.1-all/gradle-5.1.1-all.zip
3.在android studio设置

​ a.打开android studio设置(设置在顶部File/Settings),打开目录中Buile选择Gradle.

​ b. 设置 use Gradle from 选择Specified location

​ c.设置本地为Specified location后,配置本地gradle路径

此时gradle配置完成,如果之前没有gradle缓存可以用国内镜像下载地址,只需要下载zip,将zip放在配置好的文件夹下,当编译器运行时会自动解压。

原下载地址

国内下载地址

本地插件依赖配置

打开目录下的build.gradle文件将url换成本地地址

buildscript {
    repositories {
        maven {
            url 'D\\android\\files-2.1'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
    }
}

allprojects {
    repositories {
        maven {
            url 'D:\\android\\files-2.1'
        }
    }
}

buildscript: 为android studio 编译器插件下载的maven地址以及版本情况

allprojects:为所有模块三方插件(包括android开发必要的包)maven下载地址,很多情况下默认的下载地址下载三方插件包时会因为国内防火墙或者当地网络原因无法下载,那么可以配置国内的镜像地址

buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public' }//jcenter
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }//gradle-plugin
        maven { url 'https://maven.aliyun.com/repository/central' }//central
        maven { url 'https://maven.aliyun.com/repository/google' }//google
        google()
        maven { url 'https://jitpack.io' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public' }//jcenter
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }//gradle-plugin
        maven { url 'https://maven.aliyun.com/repository/central' }//central
        maven { url 'https://maven.aliyun.com/repository/google' }//google
        google()
        maven { url 'https://jitpack.io' }
        jcenter()
    }
}

插件库处理

配置的url地址应该提前下载需要依赖的插件的包,如果没有可以使用一台可上网电脑,安装、部署并成功编译Android项目(建议在一个项目内提前依赖我们需要的三方款),此步骤是为了解决离线环境中gradle下载和需要的开发库环境。如果是复制出来的库需要将目录进行处理,需要将xxx.xxx.xxx处理为xxx/xxx/xxx多级目录的形式。

例如:com.github.dcendents 处理为嵌套的多级包,com文件夹下存放github文件夹,github文件夹下存放dcendents文件夹 。

因为文件比较多所有可以用到一些工具进行处理,我用到的是java的代码对这些文件进行的处理,运行完即可一键处理CopyOfflinGradle.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;

//将Android中使用的gradle缓存目录中的jar包重新处理路径,用于内网离线构建
public class CopyOfflinGradle {

    static String lastName = "files-2.1";
    
    //本地依赖库路径
    static String path = "D:\\android\\" + lastName;
    
    
    static String stopName = "files-2.1";

    public static void main(String[] args) {
        System.out.println("Begin to copy");
        processDotForld();
        copyToLastForld();
        System.out.println("Copy finished");
    }

    /**
     * 处理文件夹中带点好的。;例:com.alibaba 会处理成 com/alibaba
     */
    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);
                    }

                }
            }
        }
    }


    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) {
// TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (fot != null) {
                    try {
                        fot.close();
                    } catch (IOException e) {
// TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }


        }
    }


    private static String getContent(String content) {
        String str = content;
        if (content != null && content.length() > 4) {
            str = content.substring(0, 4);
        }
        return str;
    }
}

问题说明

1.默认gradle缓存路径为 C:\用户\用户名\ .gradle\wrapper\dists

2.默认window环境的本地插件依赖位置为:C:\用户\用户名\ .gradle\ caches\modules-2\files-2.1

3.插件文件夹会有不同版本的文件夹,不同版本文件夹包含三个文件aar、jar、pom,.如果配置离线开发环境时,发现googel官方插件有丢失可以单独下载。 国内下载地址

4.在复制插件库的过程中会出现,在编译时报错显示依赖库缺失,但是项目并没有用到相关依赖的情况,原因可能是,android studio 所依赖插件版本用到相关库,出现传递依赖所以出现的报错

dependencies {
    classpath 'com.android.tools.build:gradle:3.4.1'
}

5.android studio 所依赖插件版本需要等于或小于android studio编译器当前版本

6.android studio 所依赖插件版本与gradle插件版本也有相应对应关系。 对应关系

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android StudioGradle离线配置是一种配置方式,可以使Android项目在没有网络连接的情况下执行构建和编译操作。通常情况下,Gradle会通过网络连接下载所需的依赖库和插件,但在某些情况下,比如缺乏网络连接或者网络不稳定,这种在线下载就会受到限制。因此,使用离线配置可以解决这个问题。 要进行Gradle离线配置,首先需要在网络连接正常的情况下,在Android Studio中打开需要离线配置的项目。然后,依次点击菜单栏中的"File"->"Settings"->"Build, Execution, Deployment"->"Gradle"。在弹出的对话框中,可以看到"Gradle offline work"选项。勾选该选项后,就可以进行离线配置了。 离线配置后,Gradle会尝试使用本地缓存来解决构建和编译所需的依赖库和插件。如果本地缓存中缺少所需的文件,Gradle会抛出一个错误,因为它无法从网络上下载它们。因此,离线配置需要首先在有网络连接的情况下进行配置,并确保所有所需的依赖库和插件都已经下载到本地缓存中。 另外,为了使离线配置生效,需要关闭Android Studio中的"Offline Mode"。该选项在工具栏中有一个小蜗牛的图标,可以点击该图标开启或关闭"Offline Mode"。 总的来说,通过Gradle离线配置,可以在没有网络连接的情况下顺利进行Android项目的构建和编译操作。通过合理配置依赖库和插件的本地缓存,可以确保项目的稳定性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值