udacity / andfun-kotlin-android-trivia

Other
197 stars 480 forks source link

The app doesn't complie once used because the old navigation Libaries and need to update target SDK #56

Open ahmedMubarak2024 opened 1 year ago

0x10FF commented 1 year ago

Made following changes:

diff --git a/app/build.gradle b/app/build.gradle
index d19be74..3bea24b 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -23,14 +23,21 @@ apply plugin: 'kotlin-kapt'
 apply plugin: 'androidx.navigation.safeargs'

 android {
-    compileSdkVersion 30
+    compileSdkVersion 34
     buildFeatures {
         dataBinding true
     }
+    compileOptions {
+        sourceCompatibility JavaVersion.VERSION_17
+        targetCompatibility JavaVersion.VERSION_17
+    }
+    kotlinOptions {
+        jvmTarget = '17'
+    }
     defaultConfig {
         applicationId 'com.example.android.navigation'
-        minSdkVersion 19
-        targetSdkVersion 30
+        minSdkVersion 21
+        targetSdkVersion 33
         vectorDrawables.useSupportLibrary = true
         versionCode 1
         versionName "1.0"
@@ -43,16 +50,14 @@ android {
     }
     productFlavors {
     }
+    namespace 'com.example.android.navigation'
 }

 dependencies {
     implementation fileTree(include: ['*.jar'], dir: 'libs')

-    // Kotlin
-    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"
-
     // Constraint Layout
-    implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"
+    implementation "androidx.constraintlayout:constraintlayout:2.1.4"

     // Core
     implementation "androidx.core:core:$version_core"
@@ -60,7 +65,22 @@ dependencies {
     // Material Design
     implementation "com.google.android.material:material:$version_material"

-    // Navigation
-    implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
-    implementation "android.arch.navigation:navigation-ui-ktx:$version_navigation"
+    def nav_version = "2.7.0"
+
+    // Java language implementation
+    implementation "androidx.navigation:navigation-fragment:$nav_version"
+    implementation "androidx.navigation:navigation-ui:$nav_version"
+
+    // Kotlin
+    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
+    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
+
+    // Feature module Support
+    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
+
+    // Testing Navigation
+    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
+
+    // Jetpack Compose Integration
+    implementation "androidx.navigation:navigation-compose:$nav_version"
 }
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index eb917a8..cba0193 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -15,8 +15,7 @@
   ~ limitations under the License.
   -->

-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.example.android.navigation">
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">

     <application
         android:allowBackup="true"
@@ -27,7 +26,8 @@
         android:theme="@style/AppTheme">
         <activity
             android:name=".MainActivity"
-            android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout">
+            android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"
+            android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
diff --git a/app/src/main/java/com/example/android/navigation/MainActivity.kt b/app/src/main/java/com/example/android/navigation/MainActivity.kt
index c21b120..ff07b9e 100644
--- a/app/src/main/java/com/example/android/navigation/MainActivity.kt
+++ b/app/src/main/java/com/example/android/navigation/MainActivity.kt
@@ -39,7 +39,7 @@ class MainActivity : AppCompatActivity() {
         appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
         // prevent nav gesture if not on start destination
         navController.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, bundle: Bundle? ->
-            if (nd.id == nc.graph.startDestination) {
+            if (nd.id == nc.graph.startDestinationId) {
                 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
             } else {
                 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
diff --git a/app/src/main/java/com/example/android/navigation/TitleFragment.kt b/app/src/main/java/com/example/android/navigation/TitleFragment.kt
index 16a7883..7ff90d5 100644
--- a/app/src/main/java/com/example/android/navigation/TitleFragment.kt
+++ b/app/src/main/java/com/example/android/navigation/TitleFragment.kt
@@ -33,10 +33,13 @@ import com.example.android.navigation.databinding.FragmentTitleBinding
 class TitleFragment : Fragment() {
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                               savedInstanceState: Bundle?): View? {
+
         val binding: FragmentTitleBinding = DataBindingUtil.inflate(
                 inflater, R.layout.fragment_title, container, false)
-        binding.playButton.setOnClickListener { v: View ->
-            v.findNavController().navigate(TitleFragmentDirections.actionTitleFragmentToGameFragment())
+
+        binding.playButton.setOnClickListener { view: View ->
+            Navigation.findNavController(view).navigate(R.id.action_titleFragment_to_gameFragment)
+//            view.findNavController().navigate(TitleFragmentDirections.actionTitleFragmentToGameFragment())
         }
         setHasOptionsMenu(true)
         return binding.root

commit d67dd3273bde60d3ac8224afbeb8f7d7c7498ac5

diff --git a/build.gradle b/build.gradle
index 42c636d..45640d8 100644
--- a/build.gradle
+++ b/build.gradle
@@ -17,19 +17,18 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 buildscript {
     ext {
-        version_kotlin = "1.3.72"
-        version_core = "1.3.1"
-        version_constraint_layout = "2.0.0-rc1"
+        version_kotlin = '1.8.0'
+        version_core = '1.10.1'
         version_lifecycle_extensions = "2.2.0"
-        version_material = "1.2.0"
-        version_navigation = "2.3.0"
+        version_material = '1.9.0'
+        version_navigation = '2.7.0'
     }
     repositories {
         google()
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:4.0.1'
+        classpath 'com.android.tools.build:gradle:8.1.1'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$version_kotlin"
         classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$version_navigation"

commit d67dd3273bde60d3ac8224afbeb8f7d7c7498ac5

diff --git a/gradle.properties b/gradle.properties
index 15120a8..e618b54 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -22,7 +22,10 @@
 # http://www.gradle.org/docs/current/userguide/build_environment.html
 # Specifies the JVM arguments used for the daemon process.
 # The setting is particularly useful for tweaking memory settings.
+android.defaults.buildfeatures.buildconfig=true
 android.enableJetifier=true
+android.nonFinalResIds=false
+android.nonTransitiveRClass=false
 android.useAndroidX=true
 org.gradle.jvmargs=-Xmx1536m
 # When configured, Gradle will run in incubating parallel mode.

With above changes, it built and deployed on emulator just fine. Hope above helps!

retrospectmike commented 11 months ago

Made following changes:



With above changes, it built and deployed on emulator just fine. Hope above helps!

Just vouching that as of 9/19/23 the above patches worked on a fresh install of Android Studio, at least on my macOS machine. This should help anyone trying to complete the App Navigation lesson in the Udacity course that links to this repo! Thanks @0x10FF !!!

44thm0820 commented 11 months ago

As of 9/20/2023, I also agree with retrospectmike. Works great as the starter code as it now builds and runs now on all my physical and virtual devices.

retrospectmike commented 11 months ago

For those who are trying the "Adding a Menu" exercise that follows and finding trouble with the now-deprecated onCreateOptionsMenu functions, you can add the following code to the bottom of onCreateView, above return binding.root:

    (requireActivity() as MenuHost).addMenuProvider(object: MenuProvider {
        override fun onPrepareMenu(menu:Menu){
            //handle eg visibility of menu items.. nothing here now
        }
        override fun onCreateMenu(menu: Menu, menuInflater:MenuInflater){
            menuInflater.inflate(R.menu.overflow_menu, menu)
        }

        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
            //validate and handle selected menu item
            return NavigationUI.onNavDestinationSelected(menuItem!!,
                requireView().findNavController())
        }
    },viewLifecycleOwner, Lifecycle.State.RESUMED)

It would be instead of the tutorial's onCreateOptinsMenu(..) and onOptionsItemSelected(..)

You'll need to add the proper imports as indicated by the IDE.
Credit to a medium page I found called How To Migrate The Deprecated onCreateOptionsMenu for the approach.

A7MeDG0L0L commented 6 months ago

+android.defaults.buildfeatures.buildconfig=true android.enableJetifier=true +android.nonFinalResIds=false +android.nonTransitiveRClass=false android.useAndroidX=true org.gradle.jvmargs=-Xmx1536m

When configured, Gradle will run in incubating parallel mode.

@0x10FF after made these changes build failed with : Unsupported Java. Your build is currently configured to use Java 17.0.9 and Gradle 6.1.1.

A7MeDG0L0L commented 6 months ago

+android.defaults.buildfeatures.buildconfig=true android.enableJetifier=true +android.nonFinalResIds=false +android.nonTransitiveRClass=false android.useAndroidX=true org.gradle.jvmargs=-Xmx1536m

When configured, Gradle will run in incubating parallel mode.

@0x10FF after made these changes build failed with : Unsupported Java. Your build is currently configured to use Java 17.0.9 and Gradle 6.1.1.

I changed my jdk to 13 and I worked successfully.

KarthiAru commented 2 months ago

Just confirming that the changes proposed by @0x10FF worked for me on Android Studio Koala on macOS as on 17/06/24

I was also able to upgrade the gradle and dependencies. Made the following changes.

gradle-wrapper.properties distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip

project/build.gradle

version_kotlin = '2.0.0'
version_core = '1.13.1'
version_lifecycle_extensions = "2.2.0"
version_material = '1.12.0'
version_navigation = '2.7.7'
classpath 'com.android.tools.build:gradle:8.5.0'