Android Guide
barK integrates seamlessly into any Android project โ Kotlin Multiplatform or Android-only โ with zero boilerplate and automatic tag detection.
Installation
Add barK to your project via Maven Central:
Initialization
Train barK once at app startup โ typically in your Application class:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Bark.train(AndroidLogTrainer()) // (1)!
} else {
Bark.train(AndroidLogTrainer(minLevel = Level.WARNING)) // (2)!
}
}
}
- Routes all logs to Android Logcat. Uses the
SYSTEMpack โ only one system trainer can be active at a time. - In production, only
WARNINGand above reach Logcat. Keeps logs meaningful and reduces noise.
Logging
Use barK from anywhere in your app. Tags are detected automatically from the calling class:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Tag: [MainActivity]
Bark.v("Verbose detail")
Bark.d("Debug info")
Bark.i("User logged in: ${user.name}")
Bark.w("Token expiring soon")
Bark.e("Network request failed", exception)
Bark.wtf("Unrecoverable state reached")
}
}
Available Trainers
| Trainer | Pack | Best for |
|---|---|---|
AndroidLogTrainer |
SYSTEM | Logcat output in app and instrumented tests |
AndroidTestLogTrainer |
SYSTEM | Logcat output scoped to test runs |
UnitTestTrainer |
CONSOLE | Plain console output in unit tests |
ColoredUnitTestTrainer |
CONSOLE | ANSI-colored console output in unit tests |
Note
Both UnitTestTrainer classes automatically detects whether it's running in a test environment and activates accordingly โ no configuration needed.
Auto-Tag Detection
barK reads the calling class name from the stack trace at runtime. No manual TAG constant required:
class UserRepository {
fun saveUser(user: User) {
Bark.i("Saving user: ${user.name}") // Tag: [UserRepository]
}
}
class PaymentService {
fun processPayment() {
Bark.d("Processing payment") // Tag: [PaymentService]
}
}
Note
Android enforces a 23-character tag limit. barK truncates automatically when needed.
Global Tags
Override auto-detection with a consistent tag across all classes โ useful for SDK development:
Bark.tag("MySDK")
// All calls now use [MySDK] regardless of calling class
Bark.d("Initializing")
Bark.i("Ready")
Bark.untag() // Return to auto-detection
Runtime Control
Bark.muzzle() // Silence all output
Bark.unmuzzle() // Resume output
println(Bark.getStatus()) // Inspect current configuration
Tip
Useful for app flows that deal with sensitive data.
Sample App
See barK in action with the included Android sample app:
The sample demonstrates realistic usage with the repository pattern, multiple trainers, and auto-tag detection across multiple classes.