Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 137 additions & 24 deletions docs/android/README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,148 @@

# Gradle for Android

Gradle is one of the most popular build tools in the Android space.
It is used for [Kotlin and Kotlin Multiplatform](../kotlin/README.md),
[Flutter](https://flutter.dev/), [React Native](https://reactnative.dev/), and other toolchains.
Gradle is one of the most popular build tools in the Android ecosystem. It is used for [Kotlin and Kotlin Multiplatform](../kotlin/README.md), [Flutter](https://flutter.dev/), [React Native](https://reactnative.dev/), and other toolchains.

## Official Documentation

The official documentation for Android development with Gradle
is provided by Google and the community on the
Android Developers site.
You can find this documentation [here](https://developer.android.com/build).
The official documentation for Android development with Gradle is provided by Google and the community on the Android Developers site. You can find this documentation [here](https://developer.android.com/build).

## Featured Recipes

- [Gradle build overview](https://developer.android.com/build/gradle-build-overview) - Understand how Gradle builds
Android applications with Gradle using Android Gradle Plugin (AGP), key concepts and the structure of a standard
Android project.
- [Android build management](https://developer.android.com/build) - Configure your Android builds for better packaging
to test, build, sign and distribute your app bundles and APKs.
- [Managing dependencies](https://developer.android.com/build/dependencies) - Configure remote repositories add external
binaries or other library modules into your build as dependencies.
- [Building project](https://developer.android.com/build/building-cmdline) - Execute tasks available for Android
projects using Gradle wrapper to build and deploy APKs and app bundles.
- [Optimizing Builds](https://developer.android.com/build/optimize-your-build) - Configure your builds to decrease the
build time resulting in a faster development of your Android projects.
- [Extending AGP](https://developer.android.com/build/extend-agp) - AGP contains extension points for plugins to control
build inputs and extend its functionality through new steps that can be integrated with standard build tasks.
- [Migration Guide](https://developer.android.com/build/migrate-to-kotlin-dsl) - Guide for migrating from Groovy to
Kotlin DSL, version catalogs, Kapt (Kotlin Annotation Processing Tool) to KSP (Kotlin Symbol Processing).
- [Troubleshooting AGP](https://developer.android.com/build/troubleshoot) - Troubleshooting guide in you encounter
issues with Android Gradle Plugin (AGP).
- [Gradle build overview](https://developer.android.com/build/gradle-build-overview) - Understand how Gradle builds Android applications with Gradle using Android Gradle Plugin (AGP), key concepts, and the structure of a standard Android project.

- [Android build management](https://developer.android.com/build) - Configure your Android builds for better packaging to test, build, sign, and distribute your app bundles and APKs.

- [Managing dependencies](https://developer.android.com/build/dependencies) - Configure remote repositories to add external binaries or other library modules into your build as dependencies.

- [Building project](https://developer.android.com/build/building-cmdline) - Execute tasks available for Android projects using the Gradle wrapper to build and deploy APKs and app bundles.

- [Optimizing Builds](https://developer.android.com/build/optimize-your-build) - Configure your builds to decrease the build time, resulting in faster development of your Android projects.

- [Extending AGP](https://developer.android.com/build/extend-agp) - AGP contains extension points for plugins to control build inputs and extend its functionality through new steps that can be integrated with standard build tasks.

- [Migration Guide](https://developer.android.com/build/migrate-to-kotlin-dsl) - Guide for migrating from Groovy to Kotlin DSL, version catalogs, Kapt (Kotlin Annotation Processing Tool) to KSP (Kotlin Symbol Processing).

- [Troubleshooting AGP](https://developer.android.com/build/troubleshoot) - Troubleshooting guide if you encounter issues with the Android Gradle Plugin (AGP).

## Expanded Sections

### **1. Gradle Build Overview**
Gradle builds Android applications using the **Android Gradle Plugin (AGP)**, which provides the flexibility to define build types, product flavors, and dependencies. Understanding these key concepts is crucial for creating efficient and scalable Android projects.

#### Key Concepts:
- **Build Variants**: Manage multiple variants of your app such as `debug` and `release`.
- **Build Types**: Use predefined types (e.g., `debug`, `release`) to control various build configurations.
- **Kotlin DSL**: Gradle uses Kotlin DSL by default for better type safety and editor support in Android builds.

#### Example in Kotlin DSL:
```kotlin
android {
buildTypes {
release {
minifyEnabled(true)
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}
```
For more details, visit the [Gradle build overview](https://developer.android.com/build/gradle-build-overview).

### **2. Android Build Management**
Android build management allows you to define various configurations for packaging, testing, signing, and distributing your app. The ability to manage product flavors, build types, and dynamic delivery modules ensures flexibility across different versions of your app.

#### Example of Build Flavors:
```kotlin
android {
flavorDimensions("version")
productFlavors {
free {
dimension = "version"
}
paid {
dimension = "version"
}
}
}
```
For more information, visit [Android build management](https://developer.android.com/build).

### **3. Managing Dependencies**
Gradle enables the addition of external binaries or library modules as dependencies, which is essential for maintaining large Android projects. You can also use **version catalogs** to resolve dependency version conflicts and handle dependencies in **Kotlin Multiplatform** projects.

#### Example of Dependencies in Kotlin DSL:
```kotlin
dependencies {
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation(platform("com.google.firebase:firebase-bom:26.7.0"))
}
```
For more information, visit [Managing dependencies](https://developer.android.com/build/dependencies).

### **4. Building Projects**

Gradle provides tasks for building your Android projects, including compiling code, packaging APKs, and generating app bundles. Using the **Gradle wrapper**, you can ensure build consistency across environments.

#### Example of Building an APK with Kotlin DSL:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest collapsing those for parts to a short overview on this page and references. It can be done with one example in Kotlin and then references to the use-cases

Copy link
Contributor Author

@abhirajadhikary06 abhirajadhikary06 Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gradle provides tasks for building your Android projects, including compiling code, packaging APKs, and generating app bundles. Using the **Gradle wrapper**, you can ensure build consistency across environments.

#### Example of Building an APK with Kotlin DSL:
```kotlin
tasks.register("assembleDebug") {
    doLast {
        println("Building APK in debug mode...")
    }
}

This Kotlin example registers a task to build the APK in debug mode. For more detailed steps on building projects and packaging apps, visit the Building Projects Guide.

```kotlin
tasks.register("customAssembleDebug") {
doLast {
println("Building APK in debug mode...")
}
}
```
This Kotlin example registers a task to build the APK in debug mode. For more detailed steps on building projects and packaging apps, visit the [Building Projects Guide](https://developer.android.com/build/building-cmdline).

### **5. Optimizing Builds**
Gradle provides various options to improve build performance, including **build caching**, **parallel execution**, and **Gradle Daemon tuning**. These methods are especially beneficial for larger projects, helping to reduce build times and enhance productivity.

#### Example of Enabling Parallel Execution:
```properties
org.gradle.parallel=true
```

To explore more optimization techniques and configure your builds for better performance, check out our detailed guide: [Gradle Build Optimization](docs/android/optimization.md).

For further information, visit the **official** [Optimizing Builds Guide](https://developer.android.com/build/optimize-your-build).

### **6. Extending AGP**
You can extend the Android Gradle Plugin (AGP) by writing custom tasks and plugins to integrate new functionality into the existing build system. This allows for advanced customization and flexibility in your build processes.

#### Example of a Custom Task:
```kotlin
tasks.register("printHelloMessage") {
doLast {
println("Hello, Android Developers!")
}
}
```
For more information, visit [Extending AGP](https://developer.android.com/build/extend-agp).

### **7. Migration Guide**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, not a recipe per se. I would keep a reference until there is substantial content

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### **7. Migration Guide**
### **7. Migration Guide**
With the shift from **Groovy** to **Kotlin DSL** in the Android ecosystem, migrating your build scripts is essential. This guide helps you transition your build configurations and annotation processing from **Kapt** to **KSP**.
For detailed steps and best practices, refer to the [official Migration Guide](https://developer.android.com/build/migrate-to-kotlin-dsl).


With the shift from **Groovy** to **Kotlin DSL** in the Android ecosystem, migrating your build scripts is essential. This guide helps you transition your build configurations and annotation processing from **Kapt** to **KSP**.

For detailed steps and best practices, refer to the [official Migration Guide](https://developer.android.com/build/migrate-to-kotlin-dsl).

### **8. Troubleshooting AGP**

Encountering issues with the Android Gradle Plugin (AGP) is common, and this section provides solutions to common problems such as dependency resolution failures, build script errors, and performance bottlenecks.

#### Example of Dependency Conflict Resolution:
```kotlin
configurations.all {
resolutionStrategy {
force("com.google.guava:guava:27.0.1-android")
}
}
```
For more troubleshooting tips, visit the **official** [Troubleshooting AGP](https://developer.android.com/build/troubleshoot) page.

You can also check the [Gradle Troubleshooting](docs/android/troubleshooting.md) page for additional help with Gradle-specific issues.

## References

Expand Down
9 changes: 9 additions & 0 deletions docs/android/optimization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Gradle Build Optimization

This page references key external resources to help you optimize your Android Gradle builds. Below are some techniques and guides:

- [Build Caching](https://developer.android.com/build/optimize-your-build#build-cache) – Reduce unnecessary tasks by reusing outputs from previous builds.
- [Parallel Execution](https://developer.android.com/build/optimize-your-build#parallel-execution) – Execute independent tasks in parallel to speed up the build.
- [Gradle Daemon](https://developer.android.com/build/optimize-your-build#gradle-daemon) – Optimize build performance by using a persistent process for faster execution.

More original content will be added soon to cover performance monitoring, advanced caching strategies, and Gradle tuning specific to large-scale projects.
28 changes: 28 additions & 0 deletions docs/android/troubleshooting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Troubleshooting Android Gradle Plugin (AGP)

This page serves as a resource for resolving common issues encountered while working with the Android Gradle Plugin (AGP). Below are some typical problems and solutions.

## Common Issues

### 1. Dependency Resolution Failures
Dependency conflicts can arise when multiple libraries require different versions of the same dependency. Use the following example to enforce a specific version:

```kotlin
configurations.all {
resolutionStrategy {
force("com.google.guava:guava:27.0.1-android")
}
}
```

### 2. Build Script Errors
Errors in the build script can prevent the project from compiling. Ensure that all plugins and dependencies are correctly defined. Review the logs for specific error messages that can guide you to the problem.

### 3. Performance Bottlenecks
Build performance issues may occur due to improper configurations. To optimize your build time, consider using techniques like enabling build caching and parallel execution.

## Additional Resources
- [Troubleshooting AGP](https://developer.android.com/build/troubleshoot) page.

## Contributing
Feel free to add additional tips or common issues you encounter. Contributions are welcome to improve this troubleshooting guide.
Loading