Search code examples
androidstringsuppress-warningshardcoded

How can I ignore all hard coded strings inside Log in Android Studio?


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?


Solution

  • 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.