Android Gradle Tip 2: Favorite Command Line

Command Line

Many students who have done Android development for a short time are accustomed to using the graphical interface, and are unfamiliar or even afraid of command line operations. Encountered AS running error, helpless.

In order to ensure ease of use, AS also shields many details of command line operation on the UI interface, which causes many people to find AS difficult to use.

In this case, I often encounter it when I solve the problem of user integration and use of the Bugtags SDK. In fact, the operation of the GUI interface, in most cases, is also based on the command tool. If you're used to the command line, you'll love it because it's simple, straightforward, and in-depth.

Typical mistakes

When AS was first launched, the most frequently asked questions on stackoverflow were when entering the project, it was always in:

Gradle: resolve dependancies '_debugCompile'

state, has been unable to move forward, what is the IDE doing? can not tell.

a command line

When users encounter problems, I most often remind users to use the command line in the project root directory, such as:

mac:
./gradlew clean build --info > bugtags.log

windows:
gradlew.bat clean build --info > bugtags.log

The meaning of this command line is to run two gradle tasks, clean and build, and turn on the info parameter to output more information, and finally output all the output information to the bugtags.log file in the project root directory. The user sends me this file, and I can usually analyze the problem based on the output file.

Suppose the command line removes the redirected input command:

./gradlew clean build --info

Information will be output to the console, and the typical error just mentioned might look like this:

cmd-output

In fact, it is downloading a relatively large file, don't panic, all you have to do is just wait! As for what is being downloaded. I want to describe it in detail in the next article.

If you have some basic command line knowledge, the previous is enough, if you want to learn more, please continue.

expand

where to run

The most common question when I give this command is where to run it. The answer is the console (Terminal).

console

Under mac, there is terminal (bash/zsh, etc.), under windows, it is powershell or cmd.

The key point:

├── gradlew
├── gradlew.bat

When using Gradle, in order to be flexible or to deal with the rapid iteration of the Gradle system, it is recommended to use the Gradle wrapper: gradlew in the project root directory to use different versions.

Therefore, running commands in the console mainly gradlewdeals . This wrapper is a file with execute permission under mac: gradlew, and under windows, it is a batch file: gradlew.bat.

Usually, running the executable file in the current directory under mac is like this:

./gradlew xxx

Run the batch file in the current directory under windows like this:

gradlew.bat xxx

Terminal plugin

AS (Intellij IDEA) has made a very useful plugin:

as-terminal

Click Terminal, AS will help you complete the following operations:

  • Simulate opening the terminal
  • cd to the current project root directory

Quickly locate folders

The IDE also supports dragging and dropping a folder in the project into the Terminal window to quickly locate the folder:

terminal-drag-location

use help

To know what parameters the gradle command runs with, you can use:

$ ./gradlew --help

USAGE: gradlew [option...] [task...]
...

to obtain. Several important parameters are listed below.

build a specified module

The structure recommended by AS is multiple projectstructure , that is, under one project, multiple modules are managed. If it is a bit of a waste of time to build all the projects every time, you can use the -p moduleparameter, where module is the module you want to build:

$ ./gradlew -p app clean build

Explicitly specify that a task should not be executed

Gradle commands have dependencies, such as build task, which depends on a series of other tasks. If you want to specify that a task should not be executed, you can use the -x taskparameter , where task is the one to be ignored, and this parameter can be passed multiple times.

$ ./gradlew build -x test -x lint

Summarize

Gradle's command line has many other tricks, and the above are just a few that I use the most every day. If you are interested, you can leave a message for further discussion.

References

mac-terminal

windows-terminal

something wrong? Leave a message under the article or add a QQ group: 453503476, I hope it can help you.

To receive the latest blog posts in a timely manner, please follow:

"mobdev" WeChat public account QR code

mobdev

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326713326&siteId=291194637