What are the new 'p' variables in Android Studio debugger variables pane? - android-studio

Recently I've installed the Bumblebee version of Android Studio and I've remarked strange new fields marked with p icon and ...get() in my debugger variables pane.
I haven't found any info about these fields, so maybe you can answer? They are so annoying, just duplicating info about val fields in the class.
See the screenshots and the sample class:
data class MyOwnClass2(
val fieldOne: String,
val fieldTwo: Int,
)
I'm using coroutines in the app, and have some Java code mixed with Kotlin one.
I can only imagine that these fields are 'properties' of the class, but I didn't explicitly override custom get/set.

TL;TR: #JvmField or migrate from Java!
So after all, these are consequences of using both Kotlin and Java code in the same module. When I put #JvmField, it doesn't generate these get/set in Kotlin, so no 'p' fields in the debugger are seen any longer.

Related

Kotlin Compose Preview reference unresolved

I am working on a Kotlin Multiplatform project for Android and Desktop using Compose and have the Compose Multiplatform IDE Support plugin installed.
What I am trying to do is add the #Preview annotation so I can get a preview of the composables that I'm working on. I tried following this guide to implement that:
https://developer.android.com/jetpack/compose/tooling/studio
I want to have my shared Compose classes in the module shared-ui, so I added the following dependencies to the build.gradle.kts in that module:
dependencies {
debugImplementation("androidx.compose.ui:ui-tooling:1.3.2")
implementation("androidx.compose.ui:ui-tooling-preview:1.3.2")
}
However, even after a synchronization, when I try to use or import the #Preview annotation in my class (shared-ui/src/commonMain/kotlin/.../MyScreen.kt), I get this error:
Unresolved reference: preview
That happens twice: Once in the import statement, where the final word Preview is in red, and once in the annotation, where it is also the word Preview that is in red:
[...]
import androidx.compose.ui.tooling.preview.Preview
[...]
#Preview
#Composable
[...]
Incidentally, I also tried the quick fix option that Android Studio offered me there: "Add dependency on androidx.compose.ui.ui-tooling-preview and import". What that does is add the dependencies exactly where I added them (provided I remove them first, otherwise it does nothing), which apparently means that Android Studio agrees with me that this is where the dependencies should go.
Which brings me to my ultimate question: Why does this not work? What am I doing wrong? Based on my understanding, this should work. But it doesn't. Am I missing something?

Why is my android studio kotlin plugin version in different format than my co-workers?

I stumbled into an issue where all my unit test classes fail to run, providing the following error:
java.lang.IllegalStateException: Failed to transform class with name <my class name>. Reason: toPrettyPrint (Ljava/lang/String;)Ljava/lang/String; in <my class name>: inconsistent stack height -1
One user commented on the following github thread for powermock, that the same error first happened to them after updating their kotlin plugin:
https://github.com/powermock/powermock/issues/779
After reading this, I have checked with my colleagues to see maybe they have a different kotlin version, and saw that their kotlin plugin version is
203.1.6.0-release-798-AS7717.8, whereas mine is 1.4.32-release-Studio4.1-1.
What I find peculiar abbout this, is that these versions dont follow the same conventions, they look more like two completely different sofware. Is there any way for me to get the same version for my kotlin plugin ?
Any help or clarification is much appreciated, thanks!
They probably just changed the naming scheme for the plugin. Looks like you have one that is about one and a half years out of date.
Anyway, go to File -> Settings -> Languages & Frameworks -> Kotlin and click the Install button.

Xzing barcode : I'm getting type mismatch error in fragment using androidx.fragment.app.Fragment

I'm using Android studio 4.0, coding in Kotlin and I'm still really new at Android development.I need to integrate the Xzing barcode libray. I've copied some code from the tutorial at [https://tutorialwing.com/implement-android-qr-code-scanner-using-zxing-library-in-kotlin/][1]. The tutorial uses the following import statement:
import android.app.Fragment
I've modified a fragment that was generated with the Navigation Drawer Activity template. It uses the androidx fragment import statment:
import androidx.fragment.app.Fragment
The errors it occurs on the following line with the "this" reference:
qrScanIntegrator = IntentIntegrator.forFragment(this)
The error is:
Type mismatch: Required Fragment! found HomeFragment
Changing my import to the older version to the older version is not recommended and if I do I get a lot of other errors.
What can I do to get zxing to work with the androidx fragment?
Thanks
The ZXing library is no longer maintained, and that particular class (IntentIntegrator) only supports android.app.Fragment which is the deprecated Android framework version of the AndroidX Fragment class.
You have a few possible options here:
Fork the ZXing library, and change the android.app.Fragment import to use the androidx.fragment.app.Fragment version instead.
Delegate the IntentIntegrator calls to your Activity instead, and have the results forwarded to your Fragment.
Simply duplicate the Intent initialization code from IntentIntegrator to a helper class of your own. The IntentIntegrator methods are just convenience methods to build an Intent and call startActivityForResult(). You'll still be able to use the IntentIntegrator.parseActivityResult method to interpret the result that comes back.
I would recommend going with option #3.
As a longer term alternative, you may also want to consider looking into Firebase ML Kit since ZXing is no longer maintained.

Run a single .kt file in Android Studio

I created a Scratch Kotlin file in Android Studio. I simply want to run this scratch.kt file and get output.
I fiddled with Run Cofigurations but cannot understand what will go into Main Class.
If you create a top-level function called main in any kotlin file, a green run button will appear next to it that allows you to run it as a program:
Note that this works in Android Studio as well as in IntelliJ IDEA.
You can create a scratch kotlin file with File -> New -> Scratch File -> Kotlin.
With this you should be able to create and run kotlin script files to try features, code and so on.
If your intention is to create a full standlone kotlin project. My recomendation is to use IntelliJ IDEA. Doing so with Android Studio may be possible but you won't stop facing issues and having to search for workarounds all the time.
Android Studio allow to run Kotlin code, but the green triangle (to Run) appears just if you don't use a class file or if you add a fun main(args: Array<String>){} out of the class's brackets. So, the way I am doing this (in AS 4.0) is:
Creating a Kotlin FILE (not choose a CLASS) and putting inside just:
fun main(args: Array) {}
Then, inside this method you are allowed to use Kotlin classes. You can do for example:
fun main(args: Array){
var oneMan: Man = Man()
oneMan.method()
}
being Man a regular Kotlin class.
Use this code, You will see a run icon near function.
class Test {
companion object {
#JvmStatic
fun main(args: Array<String>) {
println("Hello test!")
}
}
}

Android Studio Kotlin: Extract as Constant

In IntelliJ-based IDEs like Android Studio, in Java source codes, there is an option to extract things as constants when possible (final static). It is in Refactor -> Extract -> Constant and is accessible via Ctrl+Alt+C.
But I can't find it for Kotlin source codes!
Note 1: I can do it manually as you can see about NUMBER in the above screenshot (by defining it as a const val in companion object).
Note 2: The reverse-action is accomplishable by IDE; it means you can inline NUMBER with Ctrl+Alt+N.
Unfortunately, this is a known missing feature for now. It's being tracked on the official issue tracker. You can vote for it there =)

Resources