Open menu with table of contents Android Assignment 04
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Android Development

Android Assignment 04

Introduction to Database with Room

Stuttgart Media University

Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Android developer website. Please do not hesitate to ask me or the tutor if you have any questions.

1 Tasks

  • Reuse your assignment3 code and copy it to assignment4 folder.
  • In your app gradle, increase compileSdk to 34 (should be 30 since Room dependency gives us errors with compileSdk 30)
  • Start using MVVM-architecture pattern so introduce ViewModels.
  • Implement Room library to permanently save selected movies in database across app starts. So use Entities, Daos and Repositories.
  • Verify that selected movies are saved and shown on next app start.

2 Movie Data

Use this data to fill out your rows:

title year actor
Dr. No 1962 Sean Connery
From Russia with Love 1963 Sean Connery
Goldfinger 1964 Sean Connery
Thunderball 1965 Sean Connery
You Only Live Twice 1967 Sean Connery

3 Espresso Test

package de.hdmstuttgart.movietracker

import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import org.junit.Rule
import org.junit.Test

class Assignment4Test {

    @get:Rule
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    @Test
    fun myTest() {
        composeTestRule.onNodeWithTag("openSearchButton").performClick()
        composeTestRule.onNodeWithTag("textField").performTextInput("Gold")
        composeTestRule.onNodeWithTag("searchButton").performClick()
        composeTestRule.onAllNodesWithTag("movieItem")[0].assertTextContains("Goldfinger")
        composeTestRule.onAllNodesWithTag("movieItem")[0].performClick()
        composeTestRule.onAllNodesWithTag("movieItem")[0].assertTextContains("Goldfinger")
        composeTestRule.onNodeWithTag("openSearchButton").performClick()
        composeTestRule.onNodeWithTag("textField").performTextInput("From Russia with Love")
        composeTestRule.onNodeWithTag("searchButton").performClick()
        composeTestRule.onAllNodesWithTag("movieItem")[0].performClick()
        composeTestRule.onAllNodesWithTag("movieItem")[1].assertTextContains("From Russia with Love")
    }
}

4 Gradle Dependencies

Module build.gradle.kts:

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    id("com.google.devtools.ksp") version "1.9.0-1.0.13"
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.navigation.compose)
    implementation(libs.androidx.room.gradle.plugin)
    implementation(libs.androidx.room.runtime)
    annotationProcessor(libs.androidx.room.compiler)
    ksp(libs.androidx.room.compiler)
    implementation(libs.androidx.room.ktx)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}