Introduce Gradle dependencies in Android projects like npm

Introduce Gradle dependencies in Android projects like npm

  [[206604]]

1. Introduction

As an Android developer, have you ever envied the way node.js imports third-party libraries? The node.js community has prepared a fast and reliable dependency management library for developers. This dependency management library makes it very convenient for node.js to import dependency libraries. It can be done with just one line of code: npm install <library>.

In order to solve this problem, a foreign programmer @cesarferreira created a library to support such a function drone.

Github address:

https://github.com/cesarferreira/drone

Next, let’s take a look at the usage and detailed principles of drone.

2. Details of drone usage

2.1 What is a drone?

Drone implements the functionality of npm for node.js in Android projects.

It maintains 50 commonly used third-party dependency libraries, which can cover almost 90% of our commonly used open source libraries, such as: rxjava, Gson, Picasso, recyclerview-v7, etc.

When we need to use a library, we can introduce it into the dependencies of our project's build.gradle with just one line of command. In this process, the latest version will be introduced by default, and there is no need to worry about the latest version number of the third-party library that should be introduced.

It is a very useful package import tool under Android.

Because drone itself is implemented using node.js, you need an npm environment to install it. Node.js is basically a no-brainer to install, and it is assumed that you have already installed the node.js environment.

Installing drone is also very simple, and it can be done with one line of code.

  1. npm install -g drone

After the installation is complete, we can use it normally.

2.2 Using drone

To add dependent libraries using drone, just use the drone add command.

  1. drone add creator/library module

drone add needs to specify the library alias and the App Module that needs to be imported.

As you can see, it will actively query the libraries we need and insert them into the dependencies of build.gradle.

Drone supports many functions. Adding, deleting, modifying and checking are the most normal operations. For other operations, you can refer to its help documentation.

Drone is very easy to use. You can basically learn how to use it by reading the help documentation. I will not go into details here.

3. Drone Principle

Drone is very easy to use. Are you curious about how it maintains these libraries? We just specify the name of a library and it will immediately determine its latest stable version number.

Next, let’s continue to explore the principles of drone to satisfy our curiosity.

First, let’s take a look at the entire project structure of drone. It is completely developed using node.js, and all its supported commands are defined in the tasks directory.

The entry point of drone is router.js, and all commands need to be distributed through router.js.

Let's take a look at one of the most commonly used commands, add, and look at its details.

As you can see, it first uses QuickSearch.search() to find the details of the library we entered, then uses hive.getWithVersions() to find the version number of the ***stable version of the third-party library we need. Finally, it uses the handleGradleDependencyInjection() method to input it into the specific location of the build.gradle of the module we specified.

You don't need to look at the handleGradleDependencyInjection() method, it should be an io operation to write the build.gradle file.

Let's first look at the QuickSearch.search() method.

Here we use the string-similarity string similarity comparison library to find out the details of the library we need to import. This information is configured in the constants.js file.

As you can see, this is maintained by another github open source library drone-hive.

https://github.com/cesarferreira/drone-hive

In drone-hive, information about 50 commonly used open source libraries is maintained.

Let’s just pick one and take a look, here is airbnb/lottie-android.

As you can see, it maintains various information about the open source library, and the repository specifies the type of open source library, which can ultimately be used to distinguish how we use to obtain the latest version number.

This logic is in the metadata.js file.

For example, the airbnb/lottie-android viewed here is queried through jitpack.io. Of course, drone uses the query API of jitpack.io to query the latest version number. I will not go into details here.

IV. Summary

At this point, my curiosity has been resolved, and I will review the details.

Drone is mainly used for the management of Android open source libraries.

Drone can automate Gradle dependencies.

Drone maintains 50+ open source libraries, and all information is maintained in another library, drone-hive.

drone-hive maintains the information of these open source libraries in the xxx/xxx.json files of different libraries.

Depending on the repository type configured in Hive, different methods are used to obtain the stable version number.

Finally, use io operation to modify the build.gradle file of our specified Module.

[This article is an original article by 51CTO columnist "Zhang Yang". Please contact the author through WeChat official account to obtain authorization for reprinting]

Click here to read more articles by this author

<<:  On Android, what should a complete UDP communication module look like?

>>:  Tech Neo September Issue: IT Operation and Maintenance Based on Algorithms

Recommend

360 search promotion account opening, 360 promotion fees

As a search engine bidding promotion media, 360 S...

How to debug CSS compatibility issues in iPhone Safari browser

If our computer browser has CSS compatibility iss...

How much does it cost to develop a self-service group buying app?

Self-service group buying mini program, how much ...

Kotlin 1.8 will completely remove Kotlin Synthetics

The Kotlin Android Extensions Gradle plugin was o...

A Guide to KOL Advertising on TikTok

What is Douyin KOL? Users who frequently post vid...

Five little-known facts about peony

(Copyrighted image from the gallery, no permissio...

Why is Mount Everest still growing taller?

Mountains are an important geomorphic unit on Ear...

3 Tips to Avoid Pitfalls in Kuaishou Paid Promotion and Increasing Followers

Operators will come into contact with paid promot...