Solution 1 :
Okay, maybe if someone needs it it might be useful. I found an elegant solution in KOTLIN:
Here Snippet from my repo.
And here my solution:
val Any.TAG: String
get() {
return if (!javaClass.isAnonymousClass) {
val name = javaClass.simpleName
// first 23 chars
if (name.length <= 23) name else name.substring(0, 23)
} else {
val name = javaClass.name
// last 23 chars
if (name.length <= 23) name else name.substring(name.length - 23, name.length)
}
}
Problem :
I would like to create a class that provide a class tag everywhere in my kotlin project. I don’t want to use any library. A simple class/snippet could be fine to my personal project.
I want to use it to prevente everytime to create a String value TAG = “ClassName” and than use it in a log like:
Log.d(TAG,””)
Comments
Comment posted by Ryan M
This will break if you use Proguard or R8 (which is a large part of why most apps define a
Comment posted by DoctorWho
do you know better case?
Comment posted by Ryan M
Unfortunately, you’d need some sort of code-generation based solution. I’m not aware of any.