class does not implement ActivityResultLauncher AS ArcticFox - android-studio

I just installed the last version of Android Studio Arctic Fox | 2020.3.1 Canary 3.
When I open my projects, studio shows error for all of the fragments :
Class 'MapsFragment' is not abstract and does not implement abstract base class member public abstract fun <I : Any!, O : Any!> prepareCall(contract: ActivityResultContract<I!, O!>, callback: ActivityResultCallback<O!>): ActivityResultLauncher<I!> defined in androidx.fragment.app.Fragment
is it AS bug?

For some reason this problem happens after core-ktx version 1.3.0-rc01.
You can manage to solve it either by:
downgrading your core-ktx.
implementation 'androidx.core:core-ktx:1.3.0-rc01'
or just add the suitable dependency for the fragment and leave your core-ktx as is
implementation 'androidx.fragment:fragment-ktx:1.3.0-rc02'

Related

Android studio doest not show error in kotlin class after update gradle plugin

I work in the Android 4.1.2 studio. I updated Gradle plugin
- classpath 'com.android.tools.build:gradle:3.5.3'
+ classpath 'com.android.tools.build:gradle:4.1.2'
and
-distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
After that, I tried to build the apk, but the build failed due to errors in kotlin class, for example: "'handleMessage' overrides nothing " for Handler class, and "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type" for intent.getParcelableExtra. The Android studio itself does not show me such errors in kotlin class. Can you tell me what this is related to and how I can enable the display of errors in android studio
I have tried rebuild, clean , invalidate & restart.
Click the "Handler" to open the Decompiled Handler.class, search "handleMessage" in it, the new definition looks as below:
public void handleMessage(#NonNull Message msg) {
throw new RuntimeException("Stub!");
}
So the argument msg has been changed to #NonNull.
Therefore, to fix this issue, the original override statement
override fun handleMessage(message: Message?)
should be updated to remove the ? to match the function definition.
override fun handleMessage(message: Message)

Secured Android SharedPreferences Error: 'Caused by: java.lang.RuntimeException: Field keySize_ for...'

In an Android Kotlin project, I implemented EncryptedSharedPreference feature based on this link using the androidx.security library and it worked well in debug mode. But in release mode, I keep getting this error
java.lang.ExceptionInInitializerError
at com.package_name.i.a.f(:46)
at com.package_name.i.a.j(:52)
at com.package_name.i.a.e(:82)
at com.package_name.MyApplication.onCreate(:37)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Field keySize_ for k.a.d.a.h0.u not found. Known fields are [private int k.a.d.a.h0.u.i, private static final k.a.d.a.h0.u k.a.d.a.h0.u.j, private static volatile k.a.d.a.i0.a.a1 k.a.d.a.h0.u.k]
at k.a.d.a.i0.a.v0.n0(:608)
Please kindly share your ideas on how to solve this error.
This bug is related to Minify Enabled, which most likely causes some values to be removed.
A bug is reported already, you can track it here:
https://issuetracker.google.com/issues/157983099
-keep class com.google.crypto.** { *; }
Something is wrong with the rc2 version of android crypto of may 20. So it is better to use 1.0.0-alpha02 in your gradle file.
Remember, takes care with dependencies which are not in rc. You can also fix the issue with a proguard rules (see comment of Sebas LG).
dependencies {
implementation "androidx.security:security-crypto:1.0.0-alpha02"
}
Official documentation : https://developer.android.com/jetpack/androidx/releases/security
Linked commits : https://android.googlesource.com/platform/frameworks/support/+log/f66cdf1658639bd74ae850dfe3c1f5bb72eaebe6..6be101c2241593fee3ef9ab4e1fb337b485f2f9a/security/crypto
I solved using
dependencies {
..
implementation "androidx.security:security-crypto:1.0.0-rc03"
This is due to the fact that AndroidX security library uses the Google Tink library(This is already fixed in Tink 1.4.0), where there is a flaw in working with Proguard.
Add this code to proguard-rules.pro and this should help:
-keepclassmembers class * extends com.google.crypto.tink.shaded.protobuf.GeneratedMessageLite {
<fields>;
}
Links:
https://github.com/google/tink/issues/361
https://github.com/google/tink/blob/master/java_src/src/main/resources/META-INF/proguard/protobuf.pro

Android Studio nullability annoying warning

The problem:
#Nullable
private View view;
public doSomethingWithView() {
checkNotNull(view); //<- this throws NPE if parameter is null
view.showWarningAboutIssue(); //<- IDE reports about possible null here
}
Is there a way to configure the IDE, so that it doesn't report for a possible NPE on the second line?
UPDATE:
I'm using Dagger checkNotNull method which is identical to Guava ones. If I change import from Dagger to Guava my IDE removes the warning.
UPDATE #2
With latest update of Android studio I can not reproduce it anymore
You need to add the following comment before view.showWarningAboutIssue() statement:
//noinspection ConstantConditions
This can also be performed by the GUI : Alt+Enter (or "bubblelight" menu), then choose Assert view!=null and then Suppress for statement:

Android studio bad folding

Ok, I have some fictional class
public class TEMP {
String data;
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (data != null) {
}
}
};
}
And it looks like this in Android Studio:
If I collapse all - it looks ugly:
If I remove that if block inside onClick() - it looks good:
Why is this happening and how can I solve this issue?
It's been a while, so I'm not sure how helpful this is.
This is folding the function into a lambda-like visual structure with Java 8 lambda-notation. Single method interfaces are (almost) equivalent to lambdas in Java 8. Android Studio currently does NOT support Java 8 lambda notation (details found at the bottom of this answer: Is it possible to use Java 8 for Android development?).
A workaround IS available for Java 8, based on the Jack toolchain. Here is the the workaround, and here is the deprecation of the Jack toolchain.
Another note is that according to here and here, Android Studio 2.4 preview 4 and later appears to support actual lambda notation. Note that these are previews, and the latest official version is 2.3.2.
As for how to fix it, I don't believe it is possible. You may be able to use the region feature to duplicate what you are trying to achieve. The link above is a how-to sort of thing.

Kotlin unresolved reference linkedListOf

Following this post http://obviam.net/index.php/libgdx-and-kotlin/ I
created a project, and edited using Atom. It compiles, and runs on an android device. I want to convert to AndroidStudio for better tooling.
I'm using AndroidStudio 1.5.1, and it says I have the latest version of the kotlin plugin. I created a new project using the LibGDX setup program, imported into AdroidStudio, converted the main class to kotlin, everything works. Then I pasted in my existing my code, and when I build, this line:
val bullets:MutableList<NewBullet> = linkedListOf()
gets this error:
Error:(19, 42) Unresolved reference: linkedListOf
When I select Tools -> Kotlin -> Configure it says 'All modules with Kotlin files are configured'.
I've also tried importing the existing project into AndroidStudio, and the result is the same issue.
As said in the change log of Kotlin 1.0 RC, linkedListOf has been deprecated and is not available now. The article you referenced uses Kotlin 1.0 Beta, which is older.
To create a LinkedList<T> from varargs, you can pass a listOf(...) to the constructor:
val bullets: MutableList<SomeType> = LinkedList(listOf(item1, item2))
or write your own linkedListOf:
fun <T> linkedListOf(vararg items: T) = LinkedList<T>().apply {
for (i in items) { add(i) }
}

Resources