This package provides a Swift interface to various Android NDK APIs.
- Swift 5.9
- Swift Android SDK
Add the package to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/skiptools/swift-android-native.git", from: "1.0.0")
]
This module provides a Logger API for native Swift on Android compatible with the OSLog Logger for Darwin platforms.
Add the AndroidLogging
module as a conditional dependency for any targets that need it:
.target(name: "MyTarget", dependencies: [
.product(name: "AndroidLogging", package: "swift-android-native", condition: .when(platforms: [.android]))
])
This example will use the system OSLog
on Darwin platforms and AndroidLogging
on Android
to provide common logging functionality across operating systems:
#if canImport(Darwin)
import OSLog
#elseif os(Android)
import AndroidLogging
#endif
let logger = Logger(subsystem: "Subsystem", category: "Category")
logger.info("Hello Android logcat!")
Android log messages for connected devices and emulators
can be viewed from the Terminal using the
adb logcat
command.
For example, to view only the log message in the example above, you can run:
$ adb logcat '*:S' 'Subsystem/Category:I'
10-27 15:53:12.768 22599 22664 I Subsystem/Category: Hello Android logcat!
Android Studio provides the ability to graphically view and filter log messages, as do most other Android IDEs.
The Logger
functions will forward messages to the NDK
__android_log_write
function.
OSLogMessage
is simply a typealias toSwift.String
, and does not implement any of the redaction features of the Darwin version.
Licensed under the Apache 2.0 license with a runtime library exception, meaning you do not need to attribute the project in your application. See the LICENSE file for details.