Gradle related commands

Specify the gradle of the corresponding version number

gradle wrapper --gradle-version [version]

compile command

Check dependencies and compile and package

./gradlew build

Compile and export the Debug package

./gradlew assembleDebug

Compile and print the Debug package and install it

./gradlew installDebug

Compile and export the Release package

./gradlew assembleRelease

Compile and print the Release package and install it

./gradlew installRelease

Debug/Release compiles and prints logs

./gradlew assembleDebug --info

./gradlew assembleRelease --info

Clear the products in the build directory

./gradlew clean

Uninstall Debug/Release installation package

./gradlew uninstallDebug

./gradlew uninstallRelease

output:

Uninstalling com.yechaoa.gradlex (from app:debug) from device 'Pixel_5_API_31(AVD) - 12' (emulator-5554).
Uninstalled com.yechaoa.gradlex from 1 device.

adb uninstall
is executed in Android Studio to directly uninstall the current project installation package. If it is executed by adb, you need to specify the package name.

adb uninstall packagename  

task related

View main tasks

./gradlew tasks

View all Tasks

./gradlew tasks --all

Execute Task

./gradlew taskName
// or
./gradlew :moduleName:taskName

View dependencies

View dependencies in the project root directory

./gradlew dependencies

View the dependencies under the app module

./gradlew :app:dependencies

View dependency output to file

./gradlew app:dependencies > dependencies.txt

performance related

Compile offline

./gradlew assembleDebug --offline

build cache

./gradlew assembleDebug --build-cache // 开启

./gradlew assembleDebug --no-build-cache // 不开启

configuration cache

./gradlew assembleDebug --configuration-cache // 开启

./gradlew assembleDebug --no-configuration-cache // 不开启

build in parallel

./gradlew assembleDebug --parallel // 开启

./gradlew assembleDebug --no-parallel // 不开启

Offline compilation, build cache, configuration cache, and parallel build all need to be configured in gradle.properties

Compile and output performance report

./gradlew assembleDebug --profile

The performance report is located in the GradleX/build/reports/profile/ path of the built project.

See the profiling report at: file:///Users/yechao/AndroidStudioProjects/GradleX/build/reports/profile/profile-2022-11-29-23-13-29.html

Compile and output a more detailed performance report

./gradlew assembleDebug --scan

The first execution requires email verification, authorization is sufficient, and the link can be opened after completion. The content of the scan report is more detailed than that of the profile.

Dynamic parameter passing

The more commonly used parameter passing attribute, –project-prop, we generally use -P to indicate that it is used to set the project properties of the root project

Get parameters

 ./gradlew assembleDebug -PisTest=true 

Here we use -P to pass an isTest field and assign it to true.

So how to get this parameter in the code? Then write the following code in build.gradle:

if (hasProperty("isTest")){
    println("---hasProperty isTest yes")
}else {
    println("---hasProperty isTest no")
}

We can use hasProperty to obtain the parameters of the command line (CLI), and the module or plug-in can also be obtained in this way:

project.property('isTest')

Then we use the above command to compile and see the output:

➜  GradleX git:(master) ✗ ./gradlew assembleDebug -PisTest=true 
---Gradle:开始初始化了
---Gradle:settingsEvaluated Settings对象评估完毕
---Gradle:projectsLoaded 准备加载Project对象了

> Configure project :
---Gradle:Projec beforeEvaluate Project开始评估,对象是 = GradleX
---hasProperty isTest yes //   --- 看这里 ---
---Gradle:Projec afterEvaluate Project评估完毕,对象是 = GradleX

> Configure project :app
---Gradle:buildFinished 构建结束了

get parameter value

We can use getProperty() to get:

if (hasProperty("isTest")) {
    println("---hasProperty isTest yes")
    if (Boolean.valueOf(getProperty('isTest'))) {
        println("---isTest true")
    } else {
        println("---isTest false")
    }
} else {
    println("---hasProperty isTest no")
}

Note that getProperty('isTest') uses single quotes here, and the parameter values ​​​​in the command line are all objects, and the basic data type needs to be converted, that is, Boolean.valueOf(getProperty('isTest')).

custom action

We can do some custom operations on the obtained parameters, such as modifying our dependencies.

app>build.gradle:

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'

    if (project.hasProperty("isTest")) {
        println("---hasProperty isTest yes")
        if (Boolean.valueOf(getProperty('isTest'))) {
            println("---isTest true")

            implementation 'com.yechaoa.gradlex.devtools:devtools:1.1.1'

        } else {
            println("---isTest false")

            implementation 'com.yechaoa.gradlex.devtools:devtools:2.2.2'
        }
    } else {
        println("---hasProperty isTest no")
    }

    testImplementation 'junit:junit:4.13.2'
}

at last

If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, but you must be able to select models, expand, and improve programming thinking. In addition, a good career plan is also very important, and the habit of learning is very important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.

If you have no direction, here I would like to share with you a set of "Advanced Notes on the Eight Major Modules of Android" written by the senior architect of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently Master the various knowledge points of Android development.
insert image description here
Compared with the fragmented content we usually read, the knowledge points of this note are more systematic, easier to understand and remember, and are arranged strictly according to the knowledge system.

Full set of video materials:

1. Interview collection

insert image description here
2. Source code analysis collection
insert image description here

3. The collection of open source frameworks
insert image description here
welcomes everyone to support with one click and three links. If you need the information in the article, directly scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓

Guess you like

Origin blog.csdn.net/datian1234/article/details/130413823