Learn Android as an iOS Developer

As an experienced iOS developer, one of the main points I have had to understand when starting to develop applications for Android has been the build scripts.

First of all, it is important to understand that the development of applications on iOS and Android has its particularities. While they share the common goal of creating high-quality mobile apps, there are significant differences in development environments, tools used, and key concepts.

One of the areas where we find these differences is in the build tools. While in the iOS ecosystem we are familiar with Xcode and its integrated build system, in Android we are familiar with Gradle and its build scripts.

Build scripts are configuration files that play an essential role in the process of building Android applications. These are step-by-step instructions that tell Gradle how to compile the source code, package the resources, and produce the final compiled file.

Compared to iOS, where much of the build process is integrated into Xcode, Android build scripts provide an additional level of flexibility and customization. By allowing us to define build options, manage dependencies, and configure build flavors, build scripts become a powerful tool for tailoring our applications to different configurations and specific requirements.

Throughout this article, we will explore in detail how build scripts work in Android. We will discover their structure, the elements that compose them and how to customize them for our projects. In addition, we will learn how to leverage our skills as iOS developers to understand and work efficiently with build scripts on Android.

Build tools on Android

Now that we’ve laid the foundation, let’s explore build tools in the world of Android, with a primary focus on Gradle. Gradle is the widely used build system in Android application development, and is known for its flexibility and power.

Gradle is responsible for automating the process of building an application, managing the dependencies, compiling the source code, packaging the resources and generating the final compilation. It is based on a plugin framework and uses a specific programming language, such as Groovy or Kotlin, to write the build scripts.

The importance of build scripts in the Android application building process cannot be underestimated. These scripts allow you to define how the build tasks will be performed and to configure various aspects of the application. We can set build options, manage the external dependencies our application requires, and even customize different build variants to suit different environments, such as test or production.

For example, let’s take a look at a typical module-level build script in Android:

android {
    compileSdkVersion 31
    defaultConfig {
        applicationId = "com.example.myapp"
        minSdkVersion = 21
        targetSdkVersion = 31
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        release {
            minifyEnabled = false
        }
    }
    dependencies {
        implementation("com.example:library:1.0.0")
    }
}

Let’s see what each of these parameters means:

  • android. This section defines the general configuration related to the Android platform. Things like build SDK version, default app settings, and build variants are set here.

  • defaultConfig. This section sets the default configurations of the application, such as the application identifier (applicationId), the minimum version of the SDK (minSdkVersion), the target version of the SDK (targetSdkVersion), the version code (versionCode) and the version name (versionName).

  • buildTypes. Different build types are defined here, such as debug mode and release mode. The example shows the configuration for release mode, where optimization is disabled (minifyEnabled false).

  • dependencies. This section allows you to declare the external dependencies necessary for the application. In the example, a dependency is added with the library com.example:library:1.0.0, which will be used by the application.

Structure of an Android project

To fully understand how build scripts work in Android, it’s important to become familiar with the basic structure of an Android project. This will allow us to locate the build.gradle files and understand their role in the build process.

The structure of a typical Android project is made up of several folders and files, each with a specific purpose. Let’s see the main elements:

  • “app” folder. This folder contains the files related to the application module. Here you will find the Java/Kotlin files where the application’s classes and logic reside, as well as resources such as images, layout XML files, and other application-specific resources.

  • “gradle” folder. This folder contains the project-level Gradle configuration files. The gradle-wrapper.properties file specifies the version of Gradle to use in the project. The settings.gradle file defines the general settings of the project, such as the modules included.

  • “build.gradle” folder (project level). This file is essential and defines the global configuration of the project. Here you set the versions of Gradle and the Android Gradle Plugin, define the dependency repositories, and configure general aspects of the project.

  • “build.gradle” folder (module level). This folder contains the application module-specific build.gradle file. This file defines application-specific settings, such as build versions, dependencies, and build configurations.

Introduction to build.gradle files

The build.gradle files are configuration files written in the Groovy or Kotlin programming language. These files define how an Android application will be compiled and built, and are essential to how Gradle works.

The project-level build.gradle file is used to configure global aspects of the project, such as Gradle and Android Gradle Plugin versions, dependency repositories, and other required plugins.

Here is a simplified example of a project-level build.gradle file:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.0")
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

In the buildscript section, the dependencies and repositories needed for the project build are configured.

Within repositories, the dependency repositories that will be used to download the necessary libraries and tools are specified. In this case, two repositories are used: google() and jcenter().

The dependencies section is used to declare the dependencies necessary for the compilation of the project. In this case, a dependency on the com.android.tools.build:gradle plugin is declared in version 7.0.0. This plugin is essential for building Android applications and provides key functionality during the build process.

The allProjects section is used to configure the dependency repositories for all projects within the main project. In this case, the same google() and jcenter() repositories are re-declared.

In short, this code sets up the repositories and dependencies needed for the build of the Android project. The google() and jcenter() repositories are used to download the required libraries and tools, and the dependency com.android.tools.build:gradle:7.0.0 specifies the version of the Gradle plugin to use in the build of the project. These configurations are essential to ensure that the project can compile and build successfully.

Advanced options in build.gradle

Product flavors

Product flavors in Android are a feature that allows you to create variants of an app with different settings, features, and resources. You can think of them as different versions or variants of the same app. With product flavors, you can customize an app to suit different requirements, such as creating a free version and a paid version, or tailoring it for different markets or regions.

Each product flavor can have its own set of resources, configuration files, dependencies, and specific source code. This allows you to adjust aspects such as the name of the application, the icons, the colors, the functionalities and any other detail according to the needs of each product flavor. Additionally, each product flavor can have its own build configurations, such as different minimum versions or SDK targets, build options, and other specific configurations.

When you build the app, you can select which product flavor you want to build, which will generate a version of the app based on that specific configuration. This simplifies the management of multiple variants of an application in a single project and eases the build and maintenance process.

For example, let’s say you’re developing a music player app and you want to create two variants of the app: a free version with ads and a paid version without ads. Using the product flavors, you can easily configure these two variants.

In the app module’s build.gradle file, you can add the following:

android {
     // Other settings...

     flavorDimensions = "version"
     productFlavors {
         free {
             dimension = "version"
             // Settings specific to the free version
             buildConfigField("boolean", "HAS_ADS", "true")
         }
         paid {
             dimension = "version"
             // Settings specific to the paid version
             buildConfigField("boolean", "HAS_ADS", "false")
         }
     }
}

In this example, two we have defined two product flavors: “free” and “paid”. Both belong to the “version” dimension. Within each product flavor, you can add specific settings. In this case, buildConfigField is used to define a constant called HAS_ADS that indicates whether the build has ads or not.

Once you’ve defined the product flavors, you can select which variant to build from your IDE’s build menu. Each variant will generate a unique APK with the corresponding settings and features. This will allow you to distribute the different versions of the application according to your needs.

This is just a basic example of how to use product flavors in Android. You can further customize flavors with specific resources, additional configuration files, and different dependencies for each product flavor. This gives you the flexibility to tailor your application to different scenarios and requirements.

Custom tasks

Custom tasks in Android’s build.gradle files are specific actions that you can define and execute during the process of building your app. These tasks allow you to automate additional actions and customize the build flow to your specific needs.

You can think of custom tasks as additional steps that run before, during, or after the build process. These tasks are defined using Gradle syntax and can be written in the build.gradle file of the application module.

Here is an example of how a custom task could be defined:

task myCustomTask {
     doLast {
         // Custom actions you want to execute
         println "This is a custom task!"
         // Other actions you want to perform
     }
}

In this example, we have defined a custom task called myCustomTask. Within the doLast block, you can specify the actions you want to perform. You can use any Gradle action, run system commands, interact with files, or perform any custom task you need. In this case, a message is printed to the console as a custom action.

Custom tasks are useful in various situations. For example, you can create a custom task to copy additional files to the output folder, run specific tests, generate additional documentation, or perform post-build tasks.

To run a custom task, you can use the Gradle command line or your IDE interface. For example, if you want to run the custom task myCustomTask, you can run the following command:

./gradlew myCustomTask

Custom tasks give you great flexibility to automate additional actions during the process of building your Android app. You can use them to customize the build flow, add specific functionality, or integrate external tools into your development process.

Key points to keep in mind when working with build scripts in Android

Here are some tips for iOS developers getting into Android and the key points to keep in mind when working with build scripts on Android:

  • Get familiar with Gradle. Gradle is a fundamental build tool in the Android ecosystem. Make sure you understand how it works, the syntax of build scripts, and how dependencies are managed.

  • Understand the project structure. Familiarize yourself with the basic structure of an Android project, which includes directories like “app”, “res”, and “manifests”. This will help you locate and modify the build.gradle files efficiently.

  • Read the official documentation. The official Android documentation is an excellent source of information. Explore available resources, such as Gradle documentation and developer guides, to better understand build scripts and how to configure them correctly.

  • Use proper versions and dependencies. As in iOS, it is important to correctly manage versions and dependencies in Android. Make sure to use the most up-to-date versions of libraries and tools, and declare dependencies correctly in your build.gradle files.

  • Set specific build options. Build scripts allow you to set various build options in Android. Appropriately adjust the minimum SDK version, target SDK version, and other build options to ensure proper application compatibility and performance.

  • Take advantage of build flavors. Android offers the ability to configure different build flavors to tailor an app to different needs, such as free and paid versions, or variants for different markets. Learn how to use build flavors to maximize development efficiency and variant management.

  • Consider custom tasks. Custom tasks in Gradle allow you to automate specific actions during the build process. Use custom tasks to perform additional actions, such as copying files, running tests, or setting up specific environments.

  • Keep your code clean and modular: Just like on iOS, it’s important to keep your code clean and modular on Android. Break your code into modules and use Gradle features like plugins and per-module configurations to efficiently organize your project.

  • Don’t be afraid to ask for help. Whenever you’re facing Android-specific challenges or build script-related issues, don’t hesitate to seek help from the Android developer community or specialized forums. There is a large community willing to provide guidance and support.

With these tips and key points in mind, you’ll be well on your way to delving into the world of Android and working with build scripts effectively. Remember that practice and experience will be your best allies to master this area and become a successful Android developer.