How to Build a CRUD App With Kotlin and Android Studio

Chapter 2 Creating the Tutorial App

Start up Android Studio and create a new project using ’New Project’. Select ’Empty Activity’:

Then fill in the input fields as follows, whereby the storage location should of course be set according to your environment:

Then create the following packages in the ’cruddiary’ folder by right-clicking ’New’ and ’Package’:

  • adapter

  • dao

  • data

  • database

  • fragment

  • notification

  • ui

  • viewmodel

Your new project should now look like this:

2.1 Project-build.gradle

In the file build.gradle (Project) some lines have to be added that are necessary for the use of Room, Google’s AdMob and the navigation:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.lifecycle_version = ’2.6.1’
    ext.room_version = ’2.5.1’
    ext.nav_version = "2.5.3"
    dependencies {
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
    // for using AdMob
    repositories {
        google()
        mavenCentral()
    }
}
plugins {
    id com.android.application version ’7.4.2’ apply false
    id com.android.library version ’7.4.2’ apply false
    id org.jetbrains.kotlin.android version ’1.8.0’ apply false
}

2.2 Module-build.gradle

In build.gradle (modules), some additional lines are required for the use of Room and the navigation:

plugins {
    id com.android.application
    id org.jetbrains.kotlin.android
    // the following line is necessary for using a room database (see HFAD page 574)
    id kotlin-kapt
    id androidx.navigation.safeargs.kotlin
}
android {
    namespace app.gedama.tutorial.cruddiary
    compileSdk 33
    defaultConfig {
        applicationId "app.gedama.tutorial.cruddiary"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled 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.10.0’
    implementation androidx.appcompat:appcompat:1.6.1’
    implementation com.google.android.material:material:1.8.0’
    implementation androidx.constraintlayout:constraintlayout:2.1.4’
    // the following 5 lines are necessary for using a room database (see HFAD page 574)
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    testImplementation junit:junit:4.13.2’
    androidTestImplementation androidx.test.ext:junit:1.1.5’
    androidTestImplementation androidx.test.espresso:espresso-core:3.5.1’
}