Creating mobile software is a fascinating but complex process that requires precision at every stage. Many novice developers encounter difficulties already at the start, not understanding why the code does not turn into a ready-made installation file. To compile the application successfully, you need to prepare the right environment and strictly follow the algorithm of actions.

The compilation process is the translation of source code written in languages โ€‹โ€‹like Kotlin or Java into bytecode understandable by a virtual machine Dalvik or ART. Errors at this stage can be caused by both syntactic inaccuracies and environment configuration problems. In this article, we will look at all the nuances, from installing tools to obtaining the final .apk or .aab file.

It is important to understand that modern Google Play standards require strict adherence to signing and optimization rules. Starting August 2021, all new applications on Google Play must use the Android App Bundle (.aab) format instead of APK. This change is aimed at reducing the size of downloaded files for users, but adds a step to the build process for developer.

Preparing the environment and installing the SDK

The first step is to install the integrated development environment Android Studio. This is an official tool from Google that contains all the necessary components to work. The package includes Android SDK (Software Development Kit), an emulator and a debugger. Without a correctly installed SDK, compilation is impossible in principle.

After downloading the installer, you must select components to install. You should not agree with the standard โ€œdefaultโ€ set if you plan to develop for specific versions of the system. In the menu SDK Manager you should select the target version of Android (Target SDK) and the minimum supported version (Min SDK).

โš ๏ธ Attention: Make sure that the path to the SDK folder does not contain Cyrillic characters or spaces. The Gradle build system often handles such paths incorrectly, resulting in hard-to-diagnose compilation errors.

A critical component is JDK (Java Development Kit). Modern versions of Android Studio use the built-in version JBR (JetBrains Runtime), but some tasks may require installing a separate JDK 11 or higher. Check your environment variables, in particular JAVA_HOMEif you plan to use console commands.

๐Ÿ’ก

Use the command line inside Android Studio (Terminal) to automatically activate all the necessary environment variables for the current session.

Project structure and configuration files

Before running the build, you need to understand in the project structure. The main configuration file for the build system is build.gradle. In modern projects there are usually two of them: one at the project root (project level) and the second in the module folder app (application level). It is in the second file that the main compilation parameters are written.

In the file build.gradle (Module: app) there is a block androidwhere key parameters are set. This indicates compileSdkVersion โ€”the version of the SDK against which the code is compiled, and defaultConfig, where minSdkVersion and targetSdkVersionare written. Errors in these values โ€‹โ€‹are a common reason why the project fails to compile.

The necessary libraries and dependencies are also included here. The build system Gradle will automatically download the specified packages from the repositories. If the library version is incompatible with the SDK version or other dependencies, the build process will abort with a conflict message.

What is the difference between compileSdk and targetSdk?

compileSdkVersion tells the compiler which Android APIs are available for use in your code. targetSdkVersion tells the Android system that the application has been tested on this version and can use new runtime behaviors, such as changes in permissions.

A file libs.versions.toml in a directory gradleis often used to version control dependencies. This allows you to centrally manage library versions throughout the project, avoiding version conflicts in different modules.

Compilation process through Android Studio

When the code is written and the configuration is checked, you can proceed directly to the build. In the top panel menu, select Build. Before running the full build, it is useful to run the command Make Project (Ctrl+F9 or Cmd+F9 on Mac). This command compiles only modified files, which greatly speeds up the debugging process.

If you want to create a distribution-ready file, select Build Bundle(s) / APK(s). Here the system will offer two options: to create a debug or release APK file, and to create an archive. To publish on Google Play, select the second option. Build APK(s) to create a debug or release APK file, and Build Bundle(s) to create .aab archive. To publish on Google Play, choose the second option.

During the process, the execution log is displayed in the bottom panel. Build . You will see the tasks that Gradle performs: :app:preBuild, :app:compileDebugKotlin, :app:mergeDebugResources and others. If a task fails with an error, the log will highlight it in red and indicate the reason.

๐Ÿ“Š Which build tool do you work with most often?
Android Studio (Gradle)
Command Line (Gradlew)
Firebase App Distribution
Another CI/CD system

After successful completion of the build, the system will offer to locate (find) the created file or run it on the connected device. Files are usually saved to the path app/build/outputs/apk/debug/ or app/build/outputs/bundle/release/.

Building a project via the command line

Professional developers and continuous integration systems (CI/CD) often use the command line to build. This allows you to automate the process and run it on servers without a GUI. For this, a wrapper script is used in the root of the project: gradlew (for Linux/macOS) or gradlew.bat (for Windows).

The basic command for building a debug version looks like this:

./gradlew assembleDebug

To create a release version that requires a signature, use the command:

./gradlew assembleRelease

The advantage of the console build is that you can pass parameters, for example, disable tests to speed up the process or set specific flags. You can also run a complete cleanup of the project before building by adding a task clean.

  • ๐Ÿš€ assembleDebug โ€”builds a debug version of the application without code optimization (ProGuard/R8).
  • ๐Ÿ”’ assembleRelease โ€”builds an optimized release version, requires a customized signature.
  • ๐Ÿงน clean โ€”deletes all previously compiled files, useful for strange compilation errors.

โš ๏ธ Attention: When building the release version via the command line, make sure that the file gradle.properties or build.gradle has the correct paths to the key storage (keystore) and passwords. Otherwise, the build will be interrupted at the signing stage.

Managing dependencies and Gradle versions

A frequent problem is the incompatibility of the version of the Android Gradle Plugin (AGP) and the version of Gradle itself. These components are updated independently of each other, but require compliance with the compatibility matrix. If you update Android Studio, the AGP plugin may update automatically, but the Gradle wrapper version will remain old, causing an error.

The Gradle version is specified in the file gradle/wrapper/gradle-wrapper.properties. There you will find a line distributionUrlthat links to a ZIP archive with the desired version. The version of the AGP plugin is set in the file build.gradle in the root of the project in the block dependencies.

If the error "Minimum supported Gradle version is..." occurs, you need to update the Wrapper. In Android Studio, this can be done through the menu File โ†’ Project Structure โ†’ Projectby selecting the desired version in the drop-down list Gradle Version.

AGP version Min. Gradle version JDK version Status
8.0.0+ 8.0+ 17 Current
7.0.0 - 7.4 7.2+ 11 Supported
4.0.0 - 4.2 6.1.1+ 8 Obsolete
3.0.0 - 3.6 4.1+ 8 Archive
๐Ÿ’ก

Always check the official documentation for compatibility between Gradle and Android Gradle Plugin versions before updating any of the components.

Application signature and release build

To compile the application for publication in the store or installation on others device, it must be signed with a digital key. The signature confirms authorship and guarantees that the code has not been changed since assembly. Without a signature, the Android system will not install the application (except for debug builds).

To create a key, use the utility keytoolincluded in the JDK, or the built-in wizard in Android Studio (Build โ†’ Generate Signed Bundle / APK). You will need to create a keystore (.jks or .keystore), set a password and owner data.

After creating the key, its data must be added to the build configuration. In the file build.gradle (Module: app) a block is created signingConfigs, where the path to the file, passwords and key alias are indicated. Then this config is linked to the assembly release in the block buildTypes.

  • ๐Ÿ”‘ Store File โ€”path to the key storage file on your computer.
  • ๐Ÿ” Store Password โ€”password for accessing the storage (not to be confused with the key password).
  • ๐Ÿ‘ค Key Alias โ€”name of the key inside the storage.

Never upload a file keystore or write passwords for it to a public repository on GitHub. Use environment variables or local files local.propertiesadded to .gitignoreto safely store sensitive data.

Typical compilation errors and their solutions

Even experienced developers encounter build errors. One of the most common is Execution failed for task':app:mergeDebugResources'. It often occurs due to duplicate resources with the same names in different folders or errors in XML files.

Another common problem is Unresolved reference. This means that the compiler does not see the class or method. Reasons could be a missing import, an error in the package name, or a dependency not being enabled due to network issues. Check your internet connection and the availability of the repositories.

If Gradle hangs while downloading dependencies, try clearing the cache. Android Studio has a button File โ†’ Invalidate Caches / Restart. You can also delete the folder in the user's home directory and the folder in the project, then restart the build. APIs and publishing rules in the Google Play Console are updated regularly. Always check the requirements for .gradle in the user's home directory and folder build in the project, then restart the build.

โš ๏ธ Attention: APIs and publishing rules in Google Play Console are updated regularly. Always check the requirements targetSdkVersion and permissions in the official documentation before the final assembly of the release version.

โ˜‘๏ธ Checklist before the final assembly

Done: 0 / 4

FAQ: Often asked questions

Is it possible to compile an Android application without Android Studio?

Yes, it is possible. You can use a text editor (such as VS Code or IntelliJ IDEA Community) and run the build via the command line using Gradle. There are also online compilers, but they are not suitable for creating full-fledged commercial applications.

Why does the build take so long?

The first build is always long, since Gradle downloads all the dependencies and plugins. Subsequent builds should be faster thanks to caching. You can speed up the process by enabling the "Offline work" mode in the Gradle settings if you do not change the dependencies.

What is the difference between Debug and Release builds?

The Debug version is not optimized, contains debugging information and can only run on devices allowed in the debugger. The release version goes through obfuscation (R8/ProGuard), removes unused code and must be signed with the developer's key.

What to do if the error is "SDK location not found"?

You must specify the path to the SDK. Create or edit a file local.properties in the project root and add the line: sdk.dir=C\:\\Users\\YourUser\\AppData\\Local\\Android\\Sdk (path depends on the OS).