android studio relies on offline configuration

Keywords: Android Android Studio Gradle

android studio relies on offline configuration

gradle settings

1. Switch the directory structure to project mode and open the gradle/wrapper/gradle-wrapper.properties file under the project directory

2. Set the gradle local dependency package location

The original configuration distributionUrl of the project is the remote gradle address. There are two modes to set:

Remote: prefixed with http and https, when changing the gradle version, it will first look in the gradle cache directory set by the system. If it is not found, it will be downloaded to the local gradle cache directory at the address configured in the distributionUrl and depend on it.

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

Local: prefixed with files. When the version is changed, it will directly go to the configured path to find the gradle dependent package

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. Set in android studio

a. open the android studio settings (set in the top File/Settings), open the build in the directory, and select Gradle

b. set use Gradle from and select Specified location

c. after setting the local to Specified location, configure the local gradle path

At this time, the gradle configuration is completed. If there is no gradle cache before, you can use the domestic image download address. You only need to download the zip and put the zip in the configured folder. When the compiler runs, it will be decompressed automatically.

Original download address

Domestic download address.

Local plug-in dependency configuration

Open the build.gradle file in the directory and replace the url with the local address

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: maven address and version downloaded for android studio compiler plug-in

allprojects: maven download address for all modules' three-party plug-ins (including the necessary packages for android Development). In many cases, the default download address will be unable to download the three-party plug-in package due to domestic firewall or local network reasons, so the domestic image address can be configured

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()
    }
}

Plug in library processing

The configured url address should download the package of plug-ins that need to be relied on in advance. If not, you can use an Internet accessible computer to install, deploy and successfully compile the Android project (it is recommended to rely on the third-party funds we need in advance within a project). This step is to solve the problem of gradle download and the required development library environment in the offline environment. If it is a copied library, you need to process the directory. You need to process xxx.xxx.xxx into the form of xxx/xxx/xxx multi-level directory.

For example, com.github.dcendents are processed as nested multi-level packages. The GitHub folder is stored under the com folder and the dcendents folder is stored under the GitHub folder.

Because there are many files, some tools can be used to process them. I use java code to process these files. After running, I can process CopyOfflinGradle.java with one click

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;

//Reprocess the jar package in the gradle cache directory used in Android for intranet offline construction
public class CopyOfflinGradle {

    static String lastName = "files-2.1";
    
    //Local dependency library path
    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");
    }

    /**
     * Handle some good in the folder.; Example: com.alibaba will be processed as 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()) {
                    //folder
                    File desFile = creatforld(file2);
                    copyFileToDes(file2, desFile);
                } else {
                    /// / file does not exist at present
                }
            }
        }
    }

    /**
     * folders copying
     *
     * @param source
     * @param des
     */
    public static void copyFileToDes(File source, File des) {
        try {
            copyDir(source.getPath(), des.getPath());
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    /**
     * folders copying
     *
     * @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();
    }


    /**
     * create folder
     *
     * @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;
    }


    // Create directory
    public static boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {// Determine whether the directory exists
            System.out.println("Failed to create directory. The target directory already exists!");
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {// Whether to end with '/'
            destDirName = destDirName + File.separator;
        }
        if (dir.mkdirs()) {
// Create target directory
            System.out.println("Directory created successfully!" + destDirName);
            return true;
        } else {
            System.out.println("Failed to create directory!");
            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()) {
//folder
                    proceessForld(file2);
                } else {
/// / file does not exist at present
                }
            }
        }
    }


    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()) {
//folder
                proceessForld(file2);
            } else {
/// / the file does not exist. / / judge whether to copy it
                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) {
//Copy to upper level directory
                        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;
    }
}

Problem description

1. The default gradle cache path is C: \ user \ username \. gradle\wrapper\dists

2. The local plug-in dependency location of the default window environment is: C: \ user \ username \. gradle\ caches\modules-2\files-2.1

3. There will be different versions of folders in the plug-in folder. Different versions of folders contain three files aar, jar and pom. If you find that the official Google plug-in is lost when configuring the offline development environment, you can download it separately. Domestic download address

4. During the process of copying the plug-in library, an error will appear in the compilation report, indicating that the dependent library is missing, but the project does not use the relevant dependencies. The reason may be that the plug-in version relied on by android studio uses the relevant library, resulting in an error due to the transfer of dependencies

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

5. The version of the plug-in that android studio depends on must be equal to or less than the current version of the android studio compiler

6. The plug-in version that Android studio depends on also corresponds to the gradle plug-in version. Correspondence

Posted by coder_ on Sat, 20 Nov 2021 15:06:53 -0800