The gradle download is very slow when using compile under build.gradle

1 question

(1) When there are too many third-party modules referenced in the project, it will be very slow when opening Android Studio, prompting that the module dependency has been resolved, and clicking the sync button to synchronize the third-party code is also very slow. The reasons for the slowness are:

When referencing a third-party module in gradle, it is dependent on maven. Every time you open Android Studio or click the sync button, you will go to the maven central repository to fetch third-party library files, usually jar or aar files.

If the file is not available locally, download it back. Since accessing the default maven central warehouse jcenter of Android Studio through the network is very slow, the synchronization will be very time-consuming, especially when it is synchronized for the first time, or when it is rebuilt after cleaning, generally in about ten minutes,

Even if the library file has been downloaded locally, it will access the maven repository, so Android Studio will be very slow when there are too many dependencies on third-party libraries through maven.

(2) Why is Android Studio slow to compile?

When Android Studio is compiling, if the third-party code is not synchronized, or any gradle file is changed, it is not synchronized, then clicking the Run button will automatically synchronize first, that is, synchronize first and then compile. Usually, we feel the reason for the slow compilation. The main reason is that the synchronization code is too slow. After the real synchronization is completed, the compilation is actually very fast.

2 What is maven dependency?

svn is used to manage text files, but it is not good at managing binary files. At this time, maven comes on the scene. To put it simply, maven is svn for managing binary files , so it should be understandable. Generally, when we use other people's libraries, we need to download the jar file and then depend on our project. Now the author publishes the jar file to the public maven repository. In gradle, we only need to simply do the following:

dependencies {
    //使用square开源的网络访问框架okhttp
    compile 'com.squareup.okhttp:okhttp:2.5.0'
}
    
    
  • 1
  • 2
  • 3
  • 4

When synchronizing the code, Android Studio will automatically download the jar (or aar) file released by the author. If the author has released a new version, you only need to simply modify the version number that follows. If you don't want this library, delete it directly. This line of code is enough, doesn’t it feel very convenient in an instant?
In web development, you only need to use the maven plugin to achieve the same function, but the dependencies are written differently. This is the way to solve different version dependencies of different modules - maven Dafa.
However, because our access to foreign servers is too slow, such advanced production tools have become tasteless, which is the root cause of Android Studio's lag and slowness.

3 Solutions

method one:

(1) After synchronizing the code for the first time, the third-party libraries are downloaded to the local, and gradle is set to offline mode, so that there is no need to access the network next time, so the synchronization speed is shortened from minutes to seconds, but the disadvantage is Once a new maven dependency is introduced, the library file does not exist locally, and it still has to access the network, which is also slow in synchronization.

Method Two:

(2) Build the maven warehouse by yourself, and put the commonly used third-party libraries on the maven server built by yourself, so that the access to the internal maven server is much faster, and it is convenient for sub-module development and packaging, but it is troublesome to manage the server. With high technical content, it is more suitable for large-scale projects that require sub-module development, and when multiple modules are developed out of sync, this method must be used when modules need to be dependent, packaged, and released in sub-versions. For example, module A has been developed. , Module B is under development, but the version is about to be released at this time, what should I do? The solution is to rely on the previous version of module B, which is the aar file of the previous version

Method three:

(3) Download the third-party library to the local, and then copy the code to your own project. This method is to use other people's code directly at the code level. The advantage is that you no longer need to rely on other people's libraries. It is integrated with its own project. The disadvantage is that it is too hard to copy, not only the code files, but also the resource files, which is prone to errors. In addition, the coupling is serious, and it is very troublesome to delete this library. Breaking own code style and naming conventions

Method four:

(4) The recommended method is: try to use the jar and aar files of the third-party library to import your own project, or download the third-party library to the local, and then import your own project as a local module, do not use the maven dependency in gradle anymore span

In this way, there is no need to copy the code hard, and it is very convenient to delete it. Just delete the module and realize the plug and play of the module. The disadvantage is that it will cause too many modules in the project, which is not beautiful, and many libraries written by eclipse. After importing Android Studio, you have to modify it yourself to compile and pass. Many libraries are Android projects, not standard Android libraries.

Fortunately, many well-known libraries now provide libraries, which can be imported directly, and Android Studio can directly import the eclipse project as a module, so this method is still very simple.

This method is also the way eclipse used third-party libraries in the past

4 Methods of directly referencing aar files

The main reason for the Android Studio card is that gradle is too slow to download the library files in the maven repository, then we go directly to the maven repository to download the third-party library files, and reference them into the project to solve the problem. However, many projects on Github only give the use of maven dependencies, and do not give the address of the jar or aar file at all. Let's take the well-known facebook image loading framework fresco on Android as an example:

https://github.com/facebook/fresco

Only the following usage method is given on fresco's Github homepage:

compile 'com.facebook.fresco:fresco:0.9.0+'
    
    
  • 1

How to do it? Can I only use maven dependencies to download slowly? If you have built a maven server or published a framework written by yourself, you will know that in the end we still use the jar or aar file packaged by others. The above string just points to this aar file. In the end, gradle is still To download the aar file back, then the question is, where is this file? Let's find this file:
(1) Baidu "maven" warehouse

write picture description here

Select the first one, and we enter the maven central warehouse, which is the following URL

maven central repository: http://mvnrepository.com/

(2) Search for fresco, the general keyword is the name of the library

write picture description here

Compare the maven dependency string com.facebook.fresco, so choose the first one

write picture description here

You can see that there are many versions. You will find out whether the version number after the maven string above matches the version number here. In fact, the maven dependency string tells gradle how to find the library file. Click the latest version 0.9.0 in the red box

At the time of writing this article, the latest version is 0.9.0. Later, the website was revised, and the method of downloading aar files changed a bit, so I updated the following paragraph. At this time, the version has reached 0.11.0. You are flexible and flexible, let's download the aar file

write picture description here

After clicking in, you will find that there are many files, explain, the file with the suffix xxxx-javadoc.jar is the help document, and the file of xxxx-sources.jar is the source code, neither of which we need, what we need is pure xxx.jar file, or xxx.aar file, as shown in the figure:

write picture description here

Ok, found it, just click fresco-0.11.0.aar to download it. In fact, the maven dependency string tells gradle how to find the aar file.
So how to use the aar file?

(3) Using aar files
Aar files are different from jar files, jar files only contain class files, and aar files not only contain class files, but also resource files, such as pictures, layout files, etc. The aar file is used as follows.
Add the following code to the app's build.gradle file:

//本地仓库,用于引用aar文件
repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    //facebook的图片加载框架fresco
    compile(name: 'fresco-0.9.0', ext: 'aar') 
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(4) Click the sync button to synchronize the code. If you still can't find the class in the library, then click the run button. Even if it doesn't run, you can find the class in the library we introduced by running it once.

(5) Well, here I learned to skip the maven dependency and use the aar file directly. Is it very simple? After I figured out the principle, gradle's maven dependency is actually the same thing. If it is not used well, it becomes a chicken rib.

5 Some other notes

(1) Try to delete unused modules, because every time sync will synchronize all gradle files, even if our main project does not use it, it will be synchronized, sync is global

(2) Download gradle to the local, and then specify the location of gradle in Android Studio to avoid downloading gradle when you create a project for the first time, which is also a very slow process

(3) Reduce the number of synchronization codes, that is, don’t click the sync button if you have nothing to do. Each synchronization code is a very resource-consuming process.

6 Summary

(1) The methods mentioned on the Internet about solving Android Studio stuck, such as: opening a special daemon thread for gradle, increasing the memory of Android Studio, enabling gradle offline mode, etc. It has been proved by practice that it is of no use, because they The real reason for not finding the Android Studio card is that the network speed is too slow when gradle syncs the code

7 Description

The blogger wrote this article mainly because when he was working on a project, he used more than a dozen third-party libraries that maven depended on. As a result, every time he opened Android Studio or clicked the sync button to synchronize the code, it took more than ten minutes, ten minutes That is a must. You can have a cup of coffee once you synchronize. I can't bear it anymore, so Baidu solved it. However, I found that those methods on the Internet were not useful, so I figured out a solution by myself. All the imported third-party libraries are all Change to reference aar file or jar file, no longer use maven dependency, ha, it will be fast

Through the practice of bloggers, using this method, the synchronization code has been shortened from more than ten minutes to less than one minute.

8 For reprints, please indicate the blog from "Indus Trees That Rain": http://blog.csdn.net/fuchaosz/article/details/51146091

Tips:
If you think this blog is helpful to you or like the blogger's writing style, please leave a comment or like the blogger to encourage bloggers to create more high-quality blogs, thank you.

1 question

(1) When there are too many third-party modules referenced in the project, it will be very slow when opening Android Studio, prompting that the module dependency has been resolved, and clicking the sync button to synchronize the third-party code is also very slow. The reasons for the slowness are:

When referencing a third-party module in gradle, it is dependent on maven. Every time you open Android Studio or click the sync button, you will go to the maven central repository to fetch third-party library files, usually jar or aar files.

If the file is not available locally, download it back. Since accessing the default maven central warehouse jcenter of Android Studio through the network is very slow, the synchronization will be very time-consuming, especially when it is synchronized for the first time, or when it is rebuilt after cleaning, generally in about ten minutes,

Even if the library file has been downloaded locally, it will access the maven repository, so Android Studio will be very slow when there are too many dependencies on third-party libraries through maven.

(2) Why is Android Studio slow to compile?

When Android Studio is compiling, if the third-party code is not synchronized, or any gradle file is changed, it is not synchronized, then clicking the Run button will automatically synchronize first, that is, synchronize first and then compile. Usually, we feel the reason for the slow compilation. The main reason is that the synchronization code is too slow. After the real synchronization is completed, the compilation is actually very fast.

2 What is maven dependency?

svn is used to manage text files, but it is not good at managing binary files. At this time, maven comes on the scene. To put it simply, maven is svn for managing binary files , so it should be understandable. Generally, when we use other people's libraries, we need to download the jar file and then depend on our project. Now the author publishes the jar file to the public maven repository. In gradle, we only need to simply do the following:

dependencies {
    //使用square开源的网络访问框架okhttp
    compile 'com.squareup.okhttp:okhttp:2.5.0'
}
  
  
  • 1
  • 2
  • 3
  • 4

When synchronizing the code, Android Studio will automatically download the jar (or aar) file released by the author. If the author has released a new version, you only need to simply modify the version number that follows. If you don't want this library, delete it directly. This line of code is enough, doesn’t it feel very convenient in an instant?
In web development, you only need to use the maven plugin to achieve the same function, but the dependencies are written differently. This is the way to solve different version dependencies of different modules - maven Dafa.
However, because our access to foreign servers is too slow, such advanced production tools have become tasteless, which is the root cause of Android Studio's lag and slowness.

3 Solutions

method one:

(1) After synchronizing the code for the first time, the third-party libraries are downloaded to the local, and gradle is set to offline mode, so that there is no need to access the network next time, so the synchronization speed is shortened from minutes to seconds, but the disadvantage is Once a new maven dependency is introduced, the library file does not exist locally, and it still has to access the network, which is also slow in synchronization.

Method Two:

(2) Build the maven warehouse by yourself, and put the commonly used third-party libraries on the maven server built by yourself, so that the access to the internal maven server is much faster, and it is convenient for sub-module development and packaging, but it is troublesome to manage the server. With high technical content, it is more suitable for large-scale projects that require sub-module development, and when multiple modules are developed out of sync, this method must be used when modules need to be dependent, packaged, and released in sub-versions. For example, module A has been developed. , Module B is under development, but the version is about to be released at this time, what should I do? The solution is to rely on the previous version of module B, which is the aar file of the previous version

Method three:

(3) Download the third-party library to the local, and then copy the code to your own project. This method is to use other people's code directly at the code level. The advantage is that you no longer need to rely on other people's libraries. It is integrated with its own project. The disadvantage is that it is too hard to copy, not only the code files, but also the resource files, which is prone to errors. In addition, the coupling is serious, and it is very troublesome to delete this library. Breaking own code style and naming conventions

Method four:

(4) The recommended method is: try to use the jar and aar files of the third-party library to import your own project, or download the third-party library to the local, and then import your own project as a local module, do not use the maven dependency in gradle anymore span

In this way, there is no need to copy the code hard, and it is very convenient to delete it. Just delete the module and realize the plug and play of the module. The disadvantage is that it will cause too many modules in the project, which is not beautiful, and many libraries written by eclipse. After importing Android Studio, you have to modify it yourself to compile and pass. Many libraries are Android projects, not standard Android libraries.

Fortunately, many well-known libraries now provide libraries, which can be imported directly, and Android Studio can directly import the eclipse project as a module, so this method is still very simple.

This method is also the way eclipse used third-party libraries in the past

4 Methods of directly referencing aar files

The main reason for the Android Studio card is that gradle is too slow to download the library files in the maven repository, then we go directly to the maven repository to download the third-party library files, and reference them into the project to solve the problem. However, many projects on Github only give the use of maven dependencies, and do not give the address of the jar or aar file at all. Let's take the well-known facebook image loading framework fresco on Android as an example:

https://github.com/facebook/fresco

Only the following usage method is given on fresco's Github homepage:

compile 'com.facebook.fresco:fresco:0.9.0+'
  
  
  • 1

How to do it? Can I only use maven dependencies to download slowly? If you have built a maven server or published a framework written by yourself, you will know that in the end we still use the jar or aar file packaged by others. The above string just points to this aar file. In the end, gradle is still To download the aar file back, then the question is, where is this file? Let's find this file:
(1) Baidu "maven" warehouse

write picture description here

Select the first one, and we enter the maven central warehouse, which is the following URL

maven central repository: http://mvnrepository.com/

(2) Search for fresco, the general keyword is the name of the library

write picture description here

Compare the maven dependency string com.facebook.fresco, so choose the first one

write picture description here

You can see that there are many versions. You will find out whether the version number after the maven string above matches the version number here. In fact, the maven dependency string tells gradle how to find the library file. Click the latest version 0.9.0 in the red box

At the time of writing this article, the latest version is 0.9.0. Later, the website was revised, and the method of downloading aar files changed a bit, so I updated the following paragraph. At this time, the version has reached 0.11.0. You are flexible and flexible, let's download the aar file

write picture description here

After clicking in, you will find that there are many files, explain, the file with the suffix xxxx-javadoc.jar is the help document, and the file of xxxx-sources.jar is the source code, neither of which we need, what we need is pure xxx.jar file, or xxx.aar file, as shown in the figure:

write picture description here

Ok, found it, just click fresco-0.11.0.aar to download it. In fact, the maven dependency string tells gradle how to find the aar file.
So how to use the aar file?

(3) Using aar files
Aar files are different from jar files, jar files only contain class files, and aar files not only contain class files, but also resource files, such as pictures, layout files, etc. The aar file is used as follows.
Add the following code to the app's build.gradle file:

//本地仓库,用于引用aar文件
repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    //facebook的图片加载框架fresco
    compile(name: 'fresco-0.9.0', ext: 'aar') 
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(4) Click the sync button to synchronize the code. If you still can't find the class in the library, then click the run button. Even if it doesn't run, you can find the class in the library we introduced by running it once.

(5) Well, here I learned to skip the maven dependency and use the aar file directly. Is it very simple? After I figured out the principle, gradle's maven dependency is actually the same thing. If it is not used well, it becomes a chicken rib.

5 Some other notes

(1) Try to delete unused modules, because every time sync will synchronize all gradle files, even if our main project does not use it, it will be synchronized, sync is global

(2) Download gradle to the local, and then specify the location of gradle in Android Studio to avoid downloading gradle when you create a project for the first time, which is also a very slow process

(3) Reduce the number of synchronization codes, that is, don’t click the sync button if you have nothing to do. Each synchronization code is a very resource-consuming process.

6 Summary

(1) The methods mentioned on the Internet about solving Android Studio stuck, such as: opening a special daemon thread for gradle, increasing the memory of Android Studio, enabling gradle offline mode, etc. It has been proved by practice that it is of no use, because they The real reason for not finding the Android Studio card is that the network speed is too slow when gradle syncs the code

7 Description

The blogger wrote this article mainly because when he was working on a project, he used more than a dozen third-party libraries that maven depended on. As a result, every time he opened Android Studio or clicked the sync button to synchronize the code, it took more than ten minutes, ten minutes That is a must. You can have a cup of coffee once you synchronize. I can't bear it anymore, so Baidu solved it. However, I found that those methods on the Internet were not useful, so I figured out a solution by myself. All the imported third-party libraries are all Change to reference aar file or jar file, no longer use maven dependency, ha, it will be fast

Through the practice of bloggers, using this method, the synchronization code has been shortened from more than ten minutes to less than one minute.

8 For reprints, please indicate the blog from "Indus Trees That Rain": http://blog.csdn.net/fuchaosz/article/details/51146091

Tips:
If you think this blog is helpful to you or like the blogger's writing style, please leave a comment or like the blogger to encourage bloggers to create more high-quality blogs, thank you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986329&siteId=291194637