From aab70c8aa09f30bbb809005a8db3e4d148ed229b Mon Sep 17 00:00:00 2001 From: Vishnuv2311 Date: Sat, 24 Dec 2022 13:07:48 +0530 Subject: [PATCH] Fixed build issue - update dependencies - SDK 33 1. Fixed build issue with the latest android studio 2. Updated the version of dependencies to the latest versions. 3. Made the code compatible with the latest version of dependencies. 4. sdk -> 33 5. use view binding --- app/build.gradle | 27 ++++++++------ app/src/main/AndroidManifest.xml | 2 + .../android/fitactions/FitMainActivity.kt | 9 ++--- .../fitactions/home/FitStatsAdapter.kt | 15 ++++---- .../fitactions/home/FitStatsFragment.kt | 37 ++++++++++++------- .../android/fitactions/model/FitDatabase.kt | 2 +- .../android/fitactions/model/FitRepository.kt | 3 +- .../tracking/FitTrackingFragment.kt | 33 ++++++++++------- .../fitactions/tracking/FitTrackingService.kt | 4 +- build.gradle | 6 +-- gradle/wrapper/gradle-wrapper.properties | 21 ++--------- 11 files changed, 83 insertions(+), 76 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6412510..36a0912 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -21,7 +21,6 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' -apply plugin: 'kotlin-android-extensions' def firebaseEnabled = false @@ -30,12 +29,12 @@ if (firebaseEnabled) { } android { - compileSdkVersion 31 + compileSdkVersion 33 defaultConfig { applicationId "PUT_YOUR_APPLICATION_ID_HERE" minSdkVersion 21 - targetSdkVersion 31 + targetSdkVersion 33 versionCode 2 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -55,6 +54,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + + buildFeatures { + viewBinding true + } } dependencies { @@ -64,26 +67,26 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // Jetpack - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' - implementation 'androidx.core:core-ktx:1.6.0' + implementation 'androidx.core:core-ktx:1.9.0' // Material components - implementation 'com.google.android.material:material:1.4.0' + implementation 'com.google.android.material:material:1.6.0' // Firebase - implementation 'com.google.firebase:firebase-core:19.0.1' + implementation 'com.google.firebase:firebase-core:21.1.1' implementation 'com.google.firebase:firebase-appindexing:20.0.0' // Room DB - def room_version = "2.3.0" + def room_version = "2.4.3" implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-ktx:$room_version" kapt "androidx.room:room-compiler:$room_version" // Test dependencies - testImplementation 'junit:junit:4.13.1' - androidTestImplementation 'androidx.test:runner:1.4.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test:runner:1.5.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1e518aa..c08b090 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,6 +22,8 @@ + + (DIF } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.fit_stats_row, parent, false)) + return ViewHolder(FitStatsRowBinding.inflate(LayoutInflater.from(parent.context),parent,false)) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { @@ -62,7 +61,7 @@ class FitStatsAdapter : ListAdapter(DIF } } - class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + class ViewHolder(private val binding: FitStatsRowBinding) : RecyclerView.ViewHolder(binding.root) { fun bind(activity: FitActivity, max: Int) { val context = itemView.context @@ -73,7 +72,7 @@ class FitStatsAdapter : ListAdapter(DIF Locale.getDefault() ) val monthFormatter = SimpleDateFormat("MM") - itemView.statsRowTitle.text = context.getString( + binding.statsRowTitle.text = context.getString( R.string.stat_date, day, calendar.get(Calendar.DAY_OF_MONTH), @@ -84,14 +83,14 @@ class FitStatsAdapter : ListAdapter(DIF val km = String.format("%.2f", activity.distanceMeters / 1000) val duration = context.getString(R.string.stat_duration, minutes) val distance = context.getString(R.string.stat_distance, km) - itemView.statsRowContent.apply { + binding.statsRowContent.apply { text = duration append("\n") append(distance) } - itemView.statsRowProgress.max = max - itemView.statsRowProgress.progress = activity.distanceMeters.toInt() + binding.statsRowProgress.max = max + binding.statsRowProgress.progress = activity.distanceMeters.toInt() } } } \ No newline at end of file diff --git a/app/src/main/java/com/devrel/android/fitactions/home/FitStatsFragment.kt b/app/src/main/java/com/devrel/android/fitactions/home/FitStatsFragment.kt index 6c34275..ce17342 100644 --- a/app/src/main/java/com/devrel/android/fitactions/home/FitStatsFragment.kt +++ b/app/src/main/java/com/devrel/android/fitactions/home/FitStatsFragment.kt @@ -24,8 +24,8 @@ import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import com.devrel.android.fitactions.R +import com.devrel.android.fitactions.databinding.FitStatsFragmentBinding import com.devrel.android.fitactions.model.FitRepository -import kotlinx.android.synthetic.main.fit_stats_fragment.* import java.util.concurrent.TimeUnit /** @@ -37,43 +37,54 @@ class FitStatsFragment : Fragment() { lateinit var actionsCallback: FitStatsActions + private var _binding: FitStatsFragmentBinding? =null + + private val binding get() = _binding!! + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - return inflater.inflate(R.layout.fit_stats_fragment, container, false) + ): View { + _binding = FitStatsFragmentBinding.inflate(inflater, container, false) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val adapter = FitStatsAdapter() - statsList.adapter = adapter + binding.statsList.adapter = adapter val repository = FitRepository.getInstance(requireContext()) - repository.getStats().observe(viewLifecycleOwner, Observer { fitStats -> - statsActivityCount.text = getString( + repository.getStats().observe(viewLifecycleOwner) { fitStats -> + binding.statsActivityCount.text = getString( R.string.stats_total_count, fitStats.totalCount ) - statsDistanceCount.text = getString( + binding.statsDistanceCount.text = getString( R.string.stats_total_distance, fitStats.totalDistanceMeters.toInt() ) val durationInMin = TimeUnit.MILLISECONDS.toMinutes(fitStats.totalDurationMs) - statsDurationCount.text = getString(R.string.stats_total_duration, durationInMin) - }) + binding.statsDurationCount.text = + getString(R.string.stats_total_duration, durationInMin) + } - repository.getLastActivities(10).observe(viewLifecycleOwner, Observer { + repository.getLastActivities(10).observe(viewLifecycleOwner) { adapter.submitList(it) - statsList.smoothScrollToPosition(0) - }) + binding.statsList.smoothScrollToPosition(0) + } - statsStartButton.setOnClickListener { + binding.statsStartButton.setOnClickListener { actionsCallback.onStartActivity() } } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + interface FitStatsActions { fun onStartActivity() } diff --git a/app/src/main/java/com/devrel/android/fitactions/model/FitDatabase.kt b/app/src/main/java/com/devrel/android/fitactions/model/FitDatabase.kt index 6c99898..adbadc0 100644 --- a/app/src/main/java/com/devrel/android/fitactions/model/FitDatabase.kt +++ b/app/src/main/java/com/devrel/android/fitactions/model/FitDatabase.kt @@ -52,7 +52,7 @@ abstract class FitDatabase : RoomDatabase() { private fun buildDatabase(context: Context): FitDatabase { return Room.databaseBuilder(context, FitDatabase::class.java, "FitActionsDB") - .addCallback(object : RoomDatabase.Callback() { + .addCallback(object : Callback() { override fun onCreate(db: SupportSQLiteDatabase) { super.onCreate(db) prepopulate(context) diff --git a/app/src/main/java/com/devrel/android/fitactions/model/FitRepository.kt b/app/src/main/java/com/devrel/android/fitactions/model/FitRepository.kt index e063609..f0a6c81 100644 --- a/app/src/main/java/com/devrel/android/fitactions/model/FitRepository.kt +++ b/app/src/main/java/com/devrel/android/fitactions/model/FitRepository.kt @@ -19,6 +19,7 @@ package com.devrel.android.fitactions.model import android.content.Context import android.os.Handler +import android.os.Looper import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Transformations @@ -130,7 +131,7 @@ class FitRepository(private val fitDb: FitDatabase, private val ioExecutor: Exec private class Tracker : MutableLiveData() { private var isRunning = true - private val handler = Handler() + private val handler = Handler(Looper.getMainLooper()) private val runnable = object : Runnable { /** diff --git a/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingFragment.kt b/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingFragment.kt index 3a6e8ac..c4095a6 100644 --- a/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingFragment.kt +++ b/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingFragment.kt @@ -25,11 +25,10 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import androidx.lifecycle.Observer import com.devrel.android.fitactions.R +import com.devrel.android.fitactions.databinding.FitTrackingFragmentBinding import com.devrel.android.fitactions.model.FitActivity import com.devrel.android.fitactions.model.FitRepository -import kotlinx.android.synthetic.main.fit_tracking_fragment.* import java.util.concurrent.TimeUnit /** @@ -51,6 +50,10 @@ class FitTrackingFragment : Fragment() { lateinit var actionsCallback: FitTrackingActions + private var _binding: FitTrackingFragmentBinding? = null + + private val binding get() = _binding!! + private val fitRepository: FitRepository by lazy { FitRepository.getInstance(requireContext()) } @@ -73,21 +76,22 @@ class FitTrackingFragment : Fragment() { countDownMs = millisUntilFinished val secondsLeft = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished).toString() - startActivityCountDown.text = secondsLeft + binding.startActivityCountDown.text = secondsLeft } } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - return inflater.inflate(R.layout.fit_tracking_fragment, container, false) + ): View { + _binding = FitTrackingFragmentBinding.inflate(inflater,container,false) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - startActivityButton.setOnClickListener { + binding.startActivityButton.setOnClickListener { // Handle the two states of the activity button // If activity started, stop it and notify activity. // Otherwise start the tracking service. @@ -101,7 +105,7 @@ class FitTrackingFragment : Fragment() { } // Observe the ongoing activity and update the view accordingly. - fitRepository.getOnGoingActivity().observe(this, Observer { + fitRepository.getOnGoingActivity().observe(viewLifecycleOwner) { if (it == null) { // When no ongoing activity only start the countdown if we haven't already. if (countDownMs > 0) { @@ -111,12 +115,13 @@ class FitTrackingFragment : Fragment() { } else { onTracking(it) } - }) + } } override fun onDestroyView() { countDownTimer.cancel() super.onDestroyView() + _binding = null } /** @@ -139,21 +144,21 @@ class FitTrackingFragment : Fragment() { private fun onCountDown() { val type = arguments?.getSerializable(PARAM_TYPE) as? FitActivity.Type ?: FitActivity.Type.UNKNOWN - startActivityTitle.text = getString(R.string.start_activity_title, type.name.toLowerCase()) - startActivityCountDown.text = TimeUnit.MILLISECONDS.toSeconds(countDownMs).toString() - startActivityButton.isSelected = false + binding.startActivityTitle.text = getString(R.string.start_activity_title, type.name.lowercase()) + binding.startActivityCountDown.text = TimeUnit.MILLISECONDS.toSeconds(countDownMs).toString() + binding.startActivityButton.isSelected = false } /** * Update the tracking view */ private fun onTracking(activity: FitActivity) { - startActivityTitle.setText(R.string.tracking_notification_title) - startActivityCountDown.text = getString( + binding.startActivityTitle.setText(R.string.tracking_notification_title) + binding.startActivityCountDown.text = getString( R.string.stats_tracking_distance, activity.distanceMeters.toInt() ) - startActivityButton.isSelected = true + binding.startActivityButton.isSelected = true } interface FitTrackingActions { diff --git a/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingService.kt b/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingService.kt index b8bb803..583a615 100644 --- a/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingService.kt +++ b/app/src/main/java/com/devrel/android/fitactions/tracking/FitTrackingService.kt @@ -54,7 +54,9 @@ class FitTrackingService : Service() { */ private val notificationBuilder: NotificationCompat.Builder by lazy { val pendingIntent = Intent(this, FitMainActivity::class.java).let { notificationIntent -> - PendingIntent.getActivity(this, 0, notificationIntent, 0) + val flags = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0 + PendingIntent.getActivity(this, 0, notificationIntent, flags) } NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle(getText(R.string.tracking_notification_title)) diff --git a/build.gradle b/build.gradle index 3387e84..8de0973 100644 --- a/build.gradle +++ b/build.gradle @@ -17,16 +17,16 @@ buildscript { - ext.kotlin_version = '1.5.30' + ext.kotlin_version = '1.7.21' repositories { google() mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.2' + classpath 'com.android.tools.build:gradle:7.3.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath 'com.google.gms:google-services:4.3.10' + classpath 'com.google.gms:google-services:4.3.14' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f141732..0612091 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,21 +1,6 @@ -# -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# +#Sat Dec 24 12:49:33 IST 2022 distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip +zipStoreBase=GRADLE_USER_HOME