4

I have been reading about ignoring hardcoded string from Log:

Log.d(TAG, "onBindViewHolder: ");

Even inside a Toast: Toast.makeText(context,"Hardcoded text",LENGTH_SHORT).show();

Sources: How can I find all RELEVANT hard coded strings in Android Studio?

Android find all hardcoded strings in code using Android Studio

In the first source there is the best approach but somthing is not working and that is:

"adding @SuppressLint("HardcodedNonLog") to the top of the class definition"

Is there a way to ignoring these kind of hardcoded strings?

UPDATE
Create a Gradle task to add "//NON-NLS" to every Log, could be a good way? If the answer is yes, how could I do that?

Community
  • 1
  • 1
Deneb Chorny
  • 391
  • 3
  • 19

2 Answers2

2

Yes, you can!

At least with Android Studio 3.0 canary 9 (not checked with previous version).

There is option like "Annotate class 'Log' as @Non_Nls" enter image description here

After that will be generated file at path "$modulename/android/util/annotations.xml" with this content:

<root>
   <item name='android.util.Log'>
       <annotation name='org.jetbrains.annotations.NonNls'/>
   </item>
</root>

With this file all strings mentioned in Log will be ignored for this module.

ITurchenko
  • 1,828
  • 2
  • 17
  • 24
0

Hardcoded text is not an error it is a warning which can be dealt with by pressing left alt+ Enter. When you press alt+Enter you are just defining that hardcoded string to string.xml in values folder.