0. Tell a story 0.1 Ant, I really thought you were an ant I really started to get close to programming in 2012. At the end of the year, my boss said that it would take too long to manually build our app when it was released, so we should study the ant script. At that time, I didn't even know what HashMap was, and I had almost no development experience. A small ant script really made me feel deeply that the world was full of malice. Fortunately, I later figured out what target and other weird things were, otherwise there would be no more. 0.2 Maven, do you really know how to read this word? Maven /`meivn/ I came into contact with Maven because I read Chen Xionghua's "Spring in Action". His source code was actually built with Maven. As a result, I learned Spring in a mess, but I was able to use Maven smoothly. Like Ant, Maven can be used to build Java projects. Like Ant, Maven's configuration is described in XML. However, Maven can manage dependencies, which allows you to "get what you want with just one sentence." For example, if I want gson, Maven says yes, just write it down and I'll get it for you when I build it later.
You are really the boss. However, Maven is a bit difficult to learn. Many beginners get stuck when setting up the environment. Do you think it is because of the steep learning curve of Maven? Of course not. It is because the central repository of Maven was xed, so you just have to watch the "cannot resolve dependencies" every day. Later, OSChina got close to Alibaba, and there was maven.oschina.net. When I found a job last year and was thinking about doing something, I found out that maven.oschina.net was probably locked up by Alibaba, and died for a few days, but now it is alive again. So what? Anyway, the incident of the central warehouse being hacked is a thing of the past. 0.3 Gradle, is your father Google? ! In 2013, when I excitedly told the big brother mentioned above that Maven was a good comrade, the big brother said that Google recommended using Gradle. So, I thought, Gradle, is your father Google? Or at least a godfather. In fact, none of this matters. After all, Gradle is really easy to use. Compared with the XML configuration methods of the previous two, using code directly is definitely more flexible. Not only that, Gradle can actually use Maven warehouse to manage dependencies, just like a simplified version of Maven. If you don’t see the pom file, you would think you are still using Maven (of course, since you are using Maven’s warehouse, you naturally can’t do without Maven). Oh, you are an Ant user, that doesn’t matter, if you don’t believe it, just look:
1. Build with Gradle 1.1 Project Structure As shown in the figure, this is an Android gradle project that couldn’t be more ordinary. The settings.gradle file in the root directory is mainly used to include submodules. For example, if our project has a submodule called app, the content of settings.gradle is as follows:
· The build.gradle file in the root directory contains some common configurations that can be used in various submodules. · The properties contained in the gradle.properties file will become members of the project's properties. For example, we added the property hello. · hello=Hello Tas! Then we create the task in build.gradle:
The output is the same:
· The local.properties file is encountered in Android projects. We usually set the Android SDK and NDK paths in it. Of course, Android Studio will help us set it up. To understand this more clearly, I have extracted part of the source code of the Android Gradle plugin: SDK.groovy, the following code mainly includes the operation of loading sdk and ndk paths.
BasePlugin.groovy, through these two methods, we can get the paths of sdk and ndk in the gradle script
For example:
The above is just the most common hierarchy structure. There is also a flat structure. Figure 1 is a flat structure and Figure 2 is a hierarchy structure. If you are interested, you can Google it. 1.2 Several important concepts The order of appearance in this section is basically the same as that in build.gradle. 1.2.1 Repository and Dependency If you are just writing Android programs, then the dependency problem may not be so annoying - but if you are writing server-side programs in Java, then it will be a lot of pain and tears. The emergence of the repository has completely solved this problem. When developing, we only need to know the dependency ID and version. As for where it is stored, I don’t care; what it depends on, the build tool can help us find and fix it in the repository. All this is so natural. How about a latte and let the code build for a while? It is said that in the history of Java development, many repositories have emerged, but the most popular one is of course Maven. Maven uses groupId and artifactId to lock components, and then configures the version. Then the Maven repository can finally lock a certain version of the component for you to use. For example, in the example at the beginning,
Maven can help you get gson-2.4.jar with just a few configurations. Not only that, it will also help you get javadoc and source according to your settings. Mom no longer has to worry about me not being able to see the source code of the component. So where is this magical Maven repository? Maven Central, the central repository, is the originator of the Maven repository. Most other repositories will proxy it and add their own special repositories according to needs. Here are a few concepts: · Proxy warehouse: If you want to rent a house, go to Soufang.com. If you want to sign up for a driving school, I am the driving school agent. You find me and I will find the driving school. Here, there is a little difference. Once someone downloads a specific component from the proxy warehouse, the component will be cached by the proxy warehouse, and there is no need to find the proxy warehouse to download it in the future. · Private warehouse: Socialism with Chinese characteristics. Go your own way, why do you care about me? There are several hosted warehouses in the company's internal warehouse. These warehouses are unique to our company, and the components in them are also uploaded by our internal colleagues for team development and use. · Local warehouse: A hidden treasure hidden in the city. It is very similar to the proxy warehouse, except that this warehouse is stored on your own hard drive. · By the way, there is an extra directory under Android SDK, and many dependencies in it are also organized in the form of Maven repositories. But this is a Google feature, and they are so awesome that they don't upload to Maven's central repository. There is really nothing we can do about it. 1.2.2 SourceSets Source code set, which mainly contains the paths of your various types of code, such as 'src/main/java' and so on. 1.2.3 Properties As we mentioned earlier, properties are actually properties of gradle. In the gradle source code, we find the Project.java interface and see:
It is not difficult to know that properties is actually a map. We can define properties in gradle.properties or through gradle scripts:
We have already mentioned how to use it, so I won’t go into details here. 1.2.4 Project and Task If you have used ant, then project is basically similar to ant's project tag, and task is similar to ant's target tag.
In fact, calling
A task is created and the behavior of the task is defined through <<. We can see that the task has the following overloads:
So the following definition is also legal:
Simply put, project is a logical entity of the entire build project, and task is the specific task point of this project. For more information, please refer to the official website documentation and gradle source code. 2. Release components To publish components, we still rely on the repository. We still take the Maven repository as an example. Most private repositories use sonatype. 2.1 UI Release If the administrator has given you this permission, you will see the Upload Artifact tab on the UI. Select the artifact you want to upload, configure the corresponding parameters, and click Upload. 2.2 Using Maven Plugin This means using Maven's gradle plugin to upload directly during the build process. The built artifacts need to be signed, please download GPG4WIN (windows) or GPGTOOLS (mac) to generate your own key. Directly on the code: gradle.properties
build.gradle
Then run gradle uploadArchives to publish the packaged aar to the company's Maven repository. The method of jar package is similar and will not be listed here. 2.3 Using Maven Commands This can be done by publishing the component directly in cmdline through mvn. Command instructions:
Of course, there is still an authentication problem here. We need to add it to the Maven settings configuration first:
Then we can upload it using the command:
3. Plugins 3.1 What is a plugin? Plugins are actually used to make us lazy. Without plugins, if we want to build a Java project, we have to define sourceSets, classpath, build steps, etc. by ourselves. Simply put, a plugin is actually a collection of configurations and tasks. There are three main forms of gradle plugins: gradle file, you can write a plugin in your build.gradle to import it directly:
· The buildSrc project is a standard Groovy plug-in project under the root directory of your project. The directory is buildSrc, and you can directly reference the plug-ins written in it. · Independent projects are structurally the same as buildSrc projects, except that they need to be referenced by publishing to a repository. Usually, the plugins we come into contact with are in this form. · For details, please refer to: Chapter 61. Writing Custom Plugins 3.2 Common plugins The plug-ins currently available are as follows: java, build java projects war, used to publish war packages and build web projects groovy, build groovy projects com.android.application, build Android app project com.android.library, builds Android library, usually outputs aar sign Maven, publish to the Maven repository org.jetbrains.intellij, build IntelliJ plugin project 3.3 Write a plugin yourself Create a normal groovy project (it doesn't matter if it's a java project), create a src/main/groovy directory, and write the following code:
Create a META-INF/gradle-plugins directory in src/main/resources and create a greetings.properties file:
Where greettings is your plugin id. build.gradle
Run uploadArchives to publish to the local repository, then you can find your own plug-in. Since no artifactId is specified, the artifactId of our plug-in is the name of our project, such as deployplugin here. So how do we introduce this plugin? First, add dependencies to buildScript:
Then:
In this way, our task "hello" is introduced. 4. Gradle runs slowly? Friends who have used Gradle will feel that it is sometimes slow. We can speed up your Gradle through the following three methods. Don't use central repository. If your repository is configured with mavenCentral, leave it alone. People all over the world are trying to abuse it, so don't get involved. Try jCenter. · Upgrade the latest Gradle version. Currently the latest version is 2.4. Android Studio uses Gradle 2.4 by default since 1.3. · Turn on Gradle's electric motor. In gradle.properties (look familiar? Yes, that's it!!) Add the following configuration inside: If your task has no timing requirements, then turning on this option can process multiple tasks concurrently and make full use of hardware resources. Well, if you have a single-core CPU. Just ignore what I said. org.gradle.parallel=true This can also be started in the command line as a parameter and is valid for 3 hours. The daemon process can greatly shorten the compilation time org.gradle.daemon=true It depends on the requirements. Gradle runs on the Java virtual machine. This specifies that the heap memory of this virtual machine is initialized to 256M and the maximum is 1G. If you only have 2G of memory, just ignore what I said. org.gradle.jvmargs=-Xms256m -Xmx1024m Of course, the recommended way is to create a gradle.properties file under .gradle/ in your user directory to avoid pitfalls for your teammates. . . For more information, please follow our WeChat ID: weixinBugly or search for our WeChat ID: weixinBugly. Tencent Bugly Bugly is an external version of Tencent's internal product quality monitoring platform, supporting the two major platforms of iOS and Android. Its main function is to monitor and report crashes and freezes that occur on the user side after the App is released, so that developers can understand the quality of the App in real time and make timely modifications. Currently, all Tencent's internal products are using it to monitor the crash of online products. The internal team of Tencent spent 4 years to polish it. Currently, all internal products of Tencent are using it, basically covering the mobile devices and network environments in the Chinese market, and the reliability is guaranteed. When using Bugly, you use the same quality assurance method as mobile QQ, QQ Space, and Mobile Manager. |
<<: iOS 9.2 released, 360 found 5 security vulnerabilities and was thanked
The global production capacity of 100 million set...
[[421640]] Preface Android has three levels of ca...
Baidu information flow account opening generally ...
In the vast river of evolution The rhino family h...
Since the beginning of autumn, the number of pati...
618 is coming and the battle is about to begin. H...
Question: My personal website has traffic but is ...
1 WeChat is a semi-closed circle. “Good wine need...
This cold wave was so severe that it affected eve...
Pancreatic neuroendocrine cell tumor Amyotrophic ...
Looking back at the marketing industry in 2019, I...
The annual "Spring Limited" marketing c...
【51CTO.com Quick Translation】Recently, a friend n...
Now we finally have Huawei that can compete with ...
bilibili, also known as Bilibili or simply B Stat...