Monotype
Monotype

Reputation: 153

How to find hardcoded strings in kotlin

Android studio lint don't work for a kotlin project. I can't find hardcoded strings. What can be solution for this?

Upvotes: 12

Views: 2020

Answers (4)

Jaspalsinh Gohil
Jaspalsinh Gohil

Reputation: 694

Ah, well actually there's an update. Kotlin IDE does understand org.jetbrains.annotations.PropertyKey 11, but it does not always work. I have had problems if the bundle contains more than one language.

Example:

fun message(@PropertyKey(resourceBundle = "Messages") id: String) =
        ResourceBundle.getBundle("Messages").getString(sid) ?: "###$id###"

For more visit:https://developer.android.com/studio/write/lint#src

Upvotes: 0

cmaynard
cmaynard

Reputation: 2860

I suggest using Detekt for static analysis in Kotlin, specifically the string literal duplication.

Upvotes: 0

Abhishek
Abhishek

Reputation: 2305

Do these steps it worked for me

  • Download Android Studio 3.2 (in preview right now)
  • Open find in project window (cmd + shift + F for Mac)
  • Check Regex checkbox(Top right corner).
  • Check file mask(next to Regex) and type *.kt or choose kt from the drop down.
  • Type this regex = \"\w+\" you can see the results now.
  • Search with this regex =\"\w+\" in case code is not formatted properly.

Upvotes: 2

InvisibleGorilla
InvisibleGorilla

Reputation: 366

Use Edit/Find/"Find in Path" (shortcut is Cmd+Shift+F on Mac).

Then select the check on "Regex?" at the top of the "Find in Path" window and type \"*\" in the search input.

Then choose to search in Directory and then select the directory with kotlin files. Search separately in /activities and in /fragments (and any other directory where you might have kotlin code).

(To search for raw strings use \"\"\"*\".)

Screenshot of "Find in Path" search for strings example

I'm not sure if this is a 100% reliable solution but it worked for me. "Analyse -> Run inspection By Name" then type "Hardcoded strings" does not work for me.

Upvotes: 0

Related Questions