I\'m creating my first Kotlin classes in my Android application. Usually for logging purposes I have a constant with name TAG
. What I would do in Java is:
Here is my extension function in kotlin, just add it in your extensions file.
val Any.TAG: String
get() {
return if (!javaClass.isAnonymousClass) {
val name = javaClass.simpleName
if (name.length <= 23 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) name else
name.substring(0, 23)// first 23 chars
} else {
val name = javaClass.name
if (name.length <= 23 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
name else name.substring(name.length - 23, name.length)// last 23 chars
}
}
Then you can use TAG in any class like below:
Log.d(TAG, "country list")