---
title: "Build Android Artifact from Command-line"
date: 2021-10-27T09:26:48.000Z
author: Z.SHINCHVEN
tags: [Android, Gradle, apk, aab, Commandline]
canonical: https://atlassc.net/2021/10/27/build-android-artifact-from-command-line
---
## Theory

Everything you do that builds Android artifacts from Android Studio is based on Android command-line tools, so you can just build them from command-line.

## Why Build Android Artifact from Command-line?

So that you can do it in an automated pipeline, I have a [Dockerfile](https://github.com/ShinChven/android-build) for that.

In my use case, I keep the release signing key from the team for property protection, when a release artifact should be build, it will be built in a pipeline that can retrieve the release signing key.

## How?

The [official document](https://developer.android.com/studio/build/building-cmdline) explains it well.

## Use `gradlew` command instead of `gradle`

Don't use `gradle` command which you installed separately to execute Android project's gradle tasks, 
it will fail due to configurations and throws exception like:

```log
> Task :app:kaptDebugKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)
```

Android project needs its own touch.
Just use the `gradlew` bash command included in project to execute Android project's gradle tasks like `build` or `clean`.
