Upgrade to androidx.appcompat:appcompat:1.2.0 issues - android

I just updated our Android project to
implementation 'androidx.appcompat:appcompat:1.2.0'
from
implementation 'androidx.appcompat:appcompat:1.1.0'
And suddenly, the app is very slow.
Information about the project:
MVVM architecture
Using dev.b3nedikt.restring:restring:4.0.3
In the Android documentation it says that:
Deprecated AppCompatDelegate.attachBaseContext(). If you are calling or overriding this method, use AppCompatDelegate.attachBaseContext2() instead.
In all activities, I call:
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(Restring.wrapContext(newBase ?: this)))
}
However, removing it does not seems to solve the problem.
Does anyone have similar issues?

Related

Room database.withTransaction { } block not Resolved

I am following this tutorial about RemoteMediator and everything is fine until I run into a weird error when using database.withTransaction{} to allow database operations.
Unfortunately, the IDE doesn't seem (or is refusing) to recognize this very genuine and
legit block. I have checked that I defined ROOM Database abstract class correctly and declared these ROOM and Paging 3 libs in build.gradle(app) file.
// Room
implementation "androidx.room:room-runtime:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
// Room-paging artifact
implementation 'androidx.room:room-paging:2.4.3'
// Paging 3.0
implementation 'androidx.paging:paging-compose:1.0.0-alpha16'
So I decided to ignore the error and went ahead to call a dao function inside the withTransaction{} block but I get Suspension functions can be called only within a coroutine body.
This is a bit confusing since RemoteMediator's load() override function is already a suspend function.
Anyone with insights on this issue please help as I don't seem to be getting anywhere around this.
Thanks to #ianhanniballake day-saving comment above, if you run into this situation just change the room-runtime flavor from:
implementation "androidx.room:room-runtime:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
to room-ktx flavor:
implementation "androidx.room:room-ktx:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
As per Developers Docs ktx-libs provide Kotlin Extensions and Coroutines support which is exactly what database.withTransaction{} block is - it is an extension function on RoomDatabase as shown on this line from the source code.
public suspend fun <R> RoomDatabase.withTransaction(block: suspend () -> R): R {
Android KTX is a set of Kotlin extensions that are included with
Android Jetpack and other Android libraries. KTX extensions provide
concise, idiomatic Kotlin to Jetpack, Android platform, and other
APIs. To do so, these extensions leverage several Kotlin language
features, including the following:
Extension functions
Extension properties
Lambdas
Named parameters
Parameter default values
Coroutines

Android - SafetyNet Safe Browsing API

Just trying to implement SafetyNet Safe Browsing API in an Android app and also read the documentation on : https://developer.android.com/training/safetynet/safebrowsing.html but i am unable to reference SafetyNet class
E.g : They say on documentation :
To use the Safe Browsing API, you must initialize the API by calling
initSafeBrowsing() and waiting for it to complete. The following code
snippet provides an example:
Tasks.await(SafetyNet.getClient(this).initSafeBrowsing())
I have searched for this API lib to reference SafetyNet class object but i am unable to reference it.
I have tried to add in my build.gradle :
implementation 'com.google.apis:google-api-services-safebrowsing:v4-rev20190923-1.30.3'
and
apply plugin: 'com.google.gms.google-services'
But still unable to find that class.
Anyone know how to implement this lib helper?
Ok, such a amateur mistake :D .....
Instead of
implementation 'com.google.apis:google-api-services-safebrowsing:v4-rev20190923-1.30.3'
I should use
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
But they didn't mention that on the official documentations :D, hope to be useful for anyone when being confuse sometimes
There is a problem with the dependency package of safetynet. You can refer to the example code of safetynet on GitHub:
https://github.com/googlesamples/android-play-safetynet/blob/master/client/java/SafetyNetSample/Application/build.gradle
dependencies {
compile "com.android.support:support-v4:$support_version"
compile "com.android.support:support-v13:$support_version"
compile "com.android.support:cardview-v7:$support_version"
compile "com.android.support:appcompat-v7:$support_version"
compile "com.google.android.gms:play-services-safetynet:$google_play_services_version"
}
Definition of google_play_services_version:
ext.google_play_services_version = '11.0.1'

Can't use liveData or viewModelScope.launch

I'm trying to use these two builders for coroutines in my app, but in my ViewModel I can't import them or they do not pop up.
These are my dependencies:
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
and in my ViewModel
class MainViewModel(): ViewModel() {
init{
viewModelScope ----> does not work , marked in red
val data = liveData {} ----> does not work, marked in red
}
}
I rebuilt, cleaned, and restarted with invalidating cache, but I can't use them
Add the ViewModel ktx lib:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx$lifecycle_version"
Available after AndroidX lifecycle v2.1.0
for liveData:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$livedata_version"
for viewModelScope:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
The above additions to build.gradle with cache invalidation and rebuild still were not sufficient to resolve the issue for me (Android Studio Bumblebee | 2021.1.1 Patch 2). Only when I wrote this. I could choose viewModelScope from the pulldown, with automatic import insertion.

Unresolved reference: viewModelScope - Android KTX

I'm trying to use the new viewModelScope() function provided by the new android ktx library.
In the gradle file, I've added:
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
implementation 'androidx.core:core-ktx:1.0.2'
but when I tied to access the viewModelScope(), I got Unresolved reference: viewModelScope error:
class MainViewModel(application: Application): AndroidViewModel(application) {
fun fetchData(){
viewModelScope.launch{
}
}
}
I don't understand what the problem is. Any idea? Thanks.
Check the release notes https://developer.android.com/jetpack/androidx/releases/lifecycle#declaring_dependencies
viewModelScope is available from v2.1.x
So you need to bump your version in your gradle file.
I have use in my project 2.1.0-beta01:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01"
This is less version then released on official site.
However, I use it because new version 2.2.0-alpha01 still has the Unresolved reference: viewModelScope - Android KTX error.

How to get lifecycle.coroutineScope with new androidx.lifecycle:*:2.2.0-alpha01

On 7th May, 2019 androidx.lifecycle:*:2.2.0-alpha01 was released announcing:
This release adds new features that adds support for Kotlin coroutines for Lifecycle and LiveData. Detailed documentation on them can be found here.
On documentation it's mentioned that I can get the LifecycleScope:
either via lifecycle.coroutineScope or lifecycleOwner.lifecycleScope properties
But it seems that I'm not able to find none of them. My current dependencises are:
def lifecycle_ver = "2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_ver"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'
What may be the cause and how do get these apis?
I actually spent a couple hours trying to figure this out myself and it turns out it is in a new package that only exists as of the alpha. Add this and you should be good to go.
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_ver"
The accepted answers is working, but I'm misused for the first time, so I'm trying to make it clear, the current version of lifecycle is "2.1.0" and lifecycleScope, and ViewModelScope is not available in this version, to get them use
For ViewModelScope,
use androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01 or higher.
For LifecycleScope,
use androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01 or higher.
at this time 2.4.1 is available.
I've just added this two lines of code to my build.gradle (App) and that worked like a charm. I can import lifecycleScope without any problems.
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'

Categories

Resources