Hands, Soul, and Gradle: The Minimal Android App Setup

Introduction

Welcome to "Hands, Soul, and Gradle," a step-by-step guide to setting up a minimal Android project using IntelliJ IDEA and Gradle. This tutorial will walk you through creating a basic project structure, writing the necessary code, and running your first Android application. This tutorial is perfect for beginners who want to get started with Android development with the least amount of code and effort.

Prerequisites

Step-by-Step Guide

1. Create a New Project in IntelliJ IDEA

  1. Open IntelliJ IDEA and select New Project.
  2. Choose Android under Templates, then select Phone and Tablet.
  3. Select No Activity and click Next.
  4. Fill in the project details:
    • Name: Running
    • Package name: com.example.running
    • Save location: Choose your desired location
    • Language: Kotlin
    • Minimum SDK: API 24 (Nougat)
    • Build Configuration Lang: Kotlin DSL
  5. Click Create to generate the project.

2. Project Structure

Your project should have the following structure:

/ProjectRoot
├── build.gradle.kts
├── settings.gradle.kts
└── app
    ├── build.gradle.kts
    └── src
        └── main
            ├── AndroidManifest.xml
            ├── java
            │   └── com
            │       └── example
            │           └── running
            │               └── MainActivity.kt
            └── res
                └── layout
                    └── activity_main.xml

3. Generate Necessary Files Using IntelliJ IDEA

3.1. Generate MainActivity.kt

  1. Right-click on the com.example.running package directory.
  2. Select New -> Kotlin Class/File.
  3. Name the file MainActivity and select Class.
  4. IntelliJ IDEA will generate the MainActivity.kt file.

Add the following code to MainActivity.kt:

// package com.example.{"$YOUR PROJECT NAME"} 
package com.example.running

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

3.2. Generate activity_main.xml

  1. Right-click on the res directory.
  2. Select New -> Android Resource Directory.
  3. Name the directory layout.
  4. Right-click on the new layout directory and select New -> Layout Resource File.
  5. Name the file activity_main.

IntelliJ IDEA will generate the activity_main.xml file. Replace its content with:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
</LinearLayout>

4. AndroidManifest.xml

Edit the AndroidManifest.xml file to include the theme and the MainActivity declaration:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <activity
                android:name=".MainActivity"
                android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

5. Gradle Configuration

Note that the Gradle configuration files are generated automatically by IntelliJ IDEA during the project setup. These files typically do not require manual edits. Here are the contents for reference:
5.1. Root-level build.gradle.kts
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.2.0" apply false
    id("org.jetbrains.kotlin.android") version "1.9.24" apply false
}
5.2. settings.gradle.kts
pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "running"
include(":app")
5.3. app/build.gradle.kts
plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.example.running"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.running"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation("androidx.core:core-ktx:1.13.1")
    implementation("androidx.appcompat:appcompat:1.7.0")
    implementation("com.google.android.material:material:1.12.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.2.1")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
}

6. Build and Run the Project

6.1. Clean and Rebuild the Project

In IntelliJ IDEA, go to Build -> Clean Project to clean the project.
Then go to Build -> Rebuild Project to rebuild the project.

6.2. Run the Project

Press Shift + F10 to run the project.

Conclusion

Congratulations! You've successfully set up a minimal Android project using IntelliJ IDEA and Gradle. This basic setup provides a foundation upon which you can build more complex Android applications. Happy coding!
By following this guide, users can quickly set up a minimal Android project, letting IntelliJ IDEA handle the boilerplate code generation, and focus on the essential parts of the application. Feel free to follow it and let me know if you encounter any issues!