Android Studio Kotlin: Extract as Constant - android-studio

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 =)

Related

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

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.

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.

What is the use of com.diffplug.spotless plugin in Android Studio?

I am working with Jetpack Compose and I came across "com.diffplug.spotless" plugin in the app's build.gradle file of many examples, but I am not sure if I need it in my project. Can anyone explain the purpose of using it in the Jetpack compose projects?
''' apply plugin: "com.diffplug.spotless" '''
Spotless: Keep your code spotless with Gradle
While working, Many a times you will get formatting issues at the stage of every commit like removing empty lines, cutting white spaces correct, indentation and other minor formatting mistakes.
Using tool/plugin called “Spotless” will reduce time in addressing code review comments.
Spotless provides support for a variety of languages.
Spotless consists of a list of format and each format has:-
1.a target (the files to format), which you set with target.
2.a list of FormatterStep, which are just String -> String functions, such as replace, replaceRegex, trimTrailingWhitespace, custom, prettier, eclipseWtp, licenseHeader etc.
To start integration with Gradle:-
1.Add the following dependency to your build.gradle file
classpath(“com.diffplug.spotless:spotless-plugin-gradle:$spotlessVersion”)
2.Apply the following plugin
apply plugin: ‘com.diffplug.gradle.spotless’
3.Applying spotless to your gradle file in Android Java source
spotless {
java {
// ...
target '**/*.java'
// ...
}
}
Note:- Be sure to add target '**/*.java' otherwise spotless will not detect Java code
inside Android modules.
For more detail you can refer this link : 1

Where is the setting for the suggestion to wrap incompatible argument for TextView's setText() method using the static method String.valueOf()?

Recently I found out that my Android Studio no longer gives the suggestion to wrap an incompatible argument for the TextView's setText() method using the String.valueOf() method.
I sometimes forget to convert the value/variable that I'm going to pass as argument to String, but Android Studio usually gives me a warning AND suggestions to wrap the value/variable, as long as it is possible. Now, for example, when I do this:
double x = 10.567;
textView.setText(double);
Android Studio will still give me a red error warning, but the only suggestion in there is "Cast parameter to 'int'".
It used to give me the option to automatically insert the String.valueOf() method call, which is very helpful.
Is it just me or this is how it is now? There was an update a few days ago.
Did I mess up some settings in my Android Studio? Can someone tell me how to fix it?
I've looked through the Inspections settings and I haven't found it, if there's even a setting for it.
Maybe the update changes the mechanism, or you can click on the "More actions" in the prompt to see if other options available.
setText() can accept String or int, if you pass in int value, compiler will expect it is String resource ("R.string.xxx"), which is int type. Just memorize this, you don't need to rely on the prompt any more.

Finding the default project settings file

I'm trying to write a plugin for 3ds max, I went through the entire sdk installation process to the letter as described in the help files.
The problem I'm facing though is intellisence complaining about an invalid macro definition
"IntelliSense: command-line error: invalid macro definition:_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT =1"
I found the definition in project settigs -> c/c++ -> preprocessor definitions as inherited from parent or project default.
I tried disabling the inherited definitions and re-entered them, this time without the space between the name and the = and all works fine so I'm guessing its a typo on their part?
Anyway, I want to change the default project or whatever to not repeat it every time i start a new project. The project is created with a wizard which required me to copy over some files to appear and after which I had to enter the sdk path.
The files I copied are plain text with some fancy extensions and not much in them so I'm guessing the defaults are described in the sdk directory.. somewhere. Does anybody know what kind of a file I'm looking for?
EDIT: I found a file called root.vcxproj_template and it has a section for preprocessor definitions but all it contains is
<PreprocessorDefinitions>_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
and no mention of the broken one
EDIT2: in another part of the file there was a path to a property sheet (maxsdk\ProjectSettings\propertySheets\3dsmax.common.tools.settings) which included the faulty definition. I fixed it an no more complaints from VS.
_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT = 1 means that compiler should replace all old C run-time routines such as sprintf, strcpy, strtok with new versions such as strprintf_s, strcpy_s, strtok_s and similar. It goes in pair with following definition _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES = 1.
More you can find here: (MSDN) https://msdn.microsoft.com/en-us/library/ms175759.aspx. However I tried to use this but without success. It says that you can use this only for statically allocated buffers like char buffer[32], but compilers was still complaining bout unsecure strcpy.

Resources