google-developer-training / android-kotlin-fundamentals-starter-apps

android-kotlin-fundamentals-starter-apps
Other
1.1k stars 3.32k forks source link

Code don't works in newer Android Studio Versions #85

Closed pjuppes closed 9 months ago

pjuppes commented 2 years ago

None of the code examples works on Android Studie 2020.3.1 and jdk 16, a lot of mistakes, warnings. The gradle builts works, but the app don't start. Why don*t you build the programms from Beginning a new projekt

spamshot commented 2 years ago

Hello. I'm not sure if the buttons are working in the starting code but I'll help you getting the app running.

Here's the steps I did and got the app working

  1. update all dependencies by hand, don't add any extras
  2. set compileSdkVersion 31 also set targetSdkVersion 31
  3. replace jcenter() with mavenCentral()
  4. run android auto migration steps and update steps
  5. open Manifest and set activity android:exported="true"
  6. in SleepTrackerViewModelFaactory.kt Make class SleepTrackerViewModelFactory abstract
  7. in SleepTrackerViewModelFaactory.kt Change fun signature to 'fun ...... Screenshot link for SleepTrackerViewModelFaactory.kt code for SleepTrackerViewModelFaactory.kt abstract class SleepTrackerViewModelFactory( private val dataSource: SleepDatabaseDao, private val application: Application) : ViewModelProvider.Factory { @Suppress("unchecked_cast") override fun <T : ViewModel> create(modelClass: Class<T>): T { if (modelClass.isAssignableFrom(SleepTrackerViewModel::class.java)) { return SleepTrackerViewModel(dataSource, application) as T } throw IllegalArgumentException("Unknown ViewModel class") } }

build.gradle(:app) `apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' apply plugin: 'androidx.navigation.safeargs'

android { compileSdkVersion 31 defaultConfig { applicationId "com.example.android.trackmysleepquality" minSdkVersion 19 targetSdkVersion 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }

// Enables data binding.
buildFeatures {
    dataBinding true
}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// Support libraries
implementation "androidx.appcompat:appcompat:1.4.1"
implementation "androidx.fragment:fragment:1.4.1"
implementation "androidx.constraintlayout:constraintlayout:2.1.3"

// Android KTX
implementation 'androidx.core:core-ktx:1.7.0'

// Room and Lifecycle dependencies
implementation "androidx.room:room-runtime:$room_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"

// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"

// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$navigationVersion"
implementation "android.arch.navigation:navigation-ui-ktx:$navigationVersion"

// Testing
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

} build.grade(TrackMySleep....) buildscript {

ext {
    kotlin_version = '1.6.21'
    archLifecycleVersion = '1.1.1'
    room_version = '2.4.2'
    coroutine_version = '1.5.2'
    gradleVersion = '7.1.3'
    navigationVersion = '1.0.0-alpha08'
    dataBindingCompilerVersion = gradleVersion // Always need to be the same.
}

repositories {
    google()
    mavenCentral()
}

dependencies {
    classpath "com.android.tools.build:gradle:$gradleVersion"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}

allprojects { repositories { google() mavenCentral() } }

task clean(type: Delete) { delete rootProject.buildDir }`

spamshot commented 2 years ago

after updating to chipmunk you'll get an error I haven't found a fix yet I'll keep you posted


error: illegal start of expression
      return .R.id.action_sleepQualityFragment_to_sleepTrackerFragment;
lexicalninja commented 1 year ago

I spend like an hour on this every time.

you also need to upgrade the navigation version, change to the androidx implementation of the navigation libs, and use the androidx plugin for safe args.

it still has nav on 1.0.0 and is using the android.arch libraries instead of the androidx libraries

https://developer.android.com/jetpack/androidx/releases/navigation