乐闻世界logo
搜索文章和话题

What are the commonly used Gradle commands? How to optimize build performance?

2月22日 14:08

Gradle provides rich command-line tools and options. Mastering these commands can greatly improve development efficiency. Here's a detailed explanation of commonly used Gradle commands:

Basic Commands

View Help

bash
# View help information ./gradlew help # View task help ./gradlew help --task build # View all available tasks ./gradlew tasks # View tasks for specific group ./gradlew tasks --group=build # View all tasks (including hidden tasks) ./gradlew tasks --all

View Project Information

bash
# View project information ./gradlew projects # View project properties ./gradlew properties # View dependencies ./gradlew dependencies # View dependencies for specific configuration ./gradlew dependencies --configuration implementation # View dependencies for specific project ./gradlew :app:dependencies

Build Commands

Basic Build

bash
# Build project ./gradlew build # Clean and build ./gradlew clean build # Build skipping tests ./gradlew build -x test # Run only tests ./gradlew test # Run specific test class ./gradlew test --tests MyTest # Run specific test method ./gradlew test --tests MyTest.testMethod

Build Variants

bash
# Build specific variant ./gradlew assembleDebug ./gradlew assembleRelease # Build all variants ./gradlew assemble # Build specific module ./gradlew :module1:build ./gradlew :module2:build

Task Execution

Execute Single Task

bash
# Execute specific task ./gradlew clean # Execute multiple tasks ./gradlew clean build test # Execute task and pass parameters ./gradlew build -Pprofile=production

Task Dependencies

bash
# View task dependency graph ./gradlew build --dry-run # View task execution order ./gradlew build --console=plain # Force re-execution of tasks ./gradlew build --rerun-tasks

Performance Optimization Commands

Parallel Build

bash
# Enable parallel build ./gradlew build --parallel # Specify parallel thread count ./gradlew build --parallel --max-workers=4 # Configure on-demand build ./gradlew build --configure-on-demand

Build Cache

bash
# Enable build cache ./gradlew build --build-cache # Clean build cache ./gradlew cleanBuildCache # Use offline mode ./gradlew build --offline

Configuration Cache

bash
# Enable configuration cache ./gradlew build --configuration-cache # Clean configuration cache ./gradlew cleanConfigurationCache

Debugging and Diagnostics

Verbose Output

bash
# Show verbose logs ./gradlew build --info # Show debug logs ./gradlew build --debug # Show stack trace ./gradlew build --stacktrace # Show full stack trace ./gradlew build --full-stacktrace

Performance Analysis

bash
# Generate build report ./gradlew build --scan # Generate performance report ./gradlew build --profile # View build time ./gradlew build --console=plain

Dependency Analysis

bash
# View dependency tree ./gradlew :app:dependencies # View details of specific dependency ./gradlew dependencyInsight --dependency spring-boot-starter-web # Find dependency conflicts ./gradlew dependencies | grep -i conflict

Continuous Build

File Monitoring

bash
# Enable continuous build ./gradlew build --continuous # Specify monitoring interval (seconds) ./gradlew build --continuous --interval=5 # Continuous testing ./gradlew test --continuous

Custom Task Execution

Pass Parameters

bash
# Pass project properties ./gradlew build -Pversion=1.0.0 # Pass system properties ./gradlew build -Dspring.profiles.active=production # Pass JVM arguments ./gradlew build -Dorg.gradle.jvmargs="-Xmx2048m" # Pass multiple parameters ./gradlew build -Penv=prod -Dlog.level=debug

Conditional Execution

bash
# Execute task only under specific conditions ./gradlew build -PenableFeature=true # Use environment variables ENV=production ./gradlew build

Plugin Management

View Plugins

bash
# View applied plugins ./gradlew plugins # View plugin details ./gradlew plugins --detail

Update Plugins

bash
# Update dependencies ./gradlew dependencyUpdates # Update Wrapper ./gradlew wrapper --gradle-version=8.0

Multi-Project Build

Project Selection

bash
# Build specific project ./gradlew :app:build # Build multiple projects ./gradlew :app:build :library:build # Build all projects ./gradlew build # Exclude specific project ./gradlew build -x :module1:build

Project Dependencies

bash
# View project dependency relationships ./gradlew projects # View dependencies for specific project ./gradlew :app:dependencies

Android Specific Commands

Android Build

bash
# Build Debug version ./gradlew assembleDebug # Build Release version ./gradlew assembleRelease # Install to device ./gradlew installDebug ./gradlew installRelease # Uninstall app ./gradlew uninstallDebug ./gradlew uninstallRelease

Android Testing

bash
# Run unit tests ./gradlew test # Run instrumentation tests ./gradlew connectedAndroidTest # Run specific test ./gradlew test --tests com.example.MyTest

Android Other Commands

bash
# Generate Lint report ./gradlew lint # Generate signed APK ./gradlew assembleRelease # Generate Bundle ./gradlew bundleRelease

Publishing and Deployment

Publish to Repository

bash
# Publish to local repository ./gradlew publishToMavenLocal # Publish to remote repository ./gradlew publish # Publish specific module ./gradlew :app:publish

Version Management

bash
# View version ./gradlew --version # Use specific version ./gradlew build --gradle-version=8.0

Common Options

General Options

bash
# Specify settings file ./gradlew build --settings-file=custom-settings.gradle # Specify build file ./gradlew build --build-file=custom-build.gradle # Specify Gradle user home directory ./gradlew build --gradle-user-home=/custom/path # Specify project directory ./gradlew build --project-dir=/custom/project

Output Control

bash
# Console output mode ./gradlew build --console=plain ./gradlew build --console=auto ./gradlew build --console=rich # Color output ./gradlew build --color=always ./gradlew build --color=never ./gradlew build --color=auto # Quiet mode ./gradlew build --quiet

Troubleshooting

Clean and Retry

bash
# Clean build ./gradlew clean # Clean all caches ./gradlew clean cleanBuildCache cleanConfigurationCache # Force re-download dependencies ./gradlew build --refresh-dependencies # Re-execute all tasks ./gradlew build --rerun-tasks

Network Issues

bash
# Use offline mode ./gradlew build --offline # Configure proxy ./gradlew build -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080

Best Practices

1. Use Aliases

bash
# Create aliases in shell alias gb='./gradlew build' alias gt='./gradlew test' alias gc='./gradlew clean'

2. Use Scripts

bash
# Create build script #!/bin/bash ./gradlew clean build --parallel --build-cache

3. Use Configuration Files

properties
# gradle.properties org.gradle.parallel=true org.gradle.caching=true org.gradle.configureondemand=true org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m

4. Use Gradle Daemon

bash
# Enable Gradle Daemon ./gradlew build --daemon # Stop all Daemons ./gradlew --stop # View running Daemons ./gradlew --status

Common Problem Solving

1. Out of Memory

bash
# Increase JVM memory ./gradlew build -Dorg.gradle.jvmargs="-Xmx4096m -XX:MaxMetaspaceSize=1024m"

2. Slow Build

bash
# Enable parallel build and cache ./gradlew build --parallel --build-cache --configuration-cache

3. Dependency Conflicts

bash
# View dependency tree ./gradlew dependencies # Use dependency analysis tool ./gradlew dependencyInsight --dependency <dependency-name>

4. Tasks Not Executing

bash
# Force re-execution of tasks ./gradlew build --rerun-tasks # View task status ./gradlew build --info
标签:Gradle