此软件包为各种 Android NDK API 提供了一个 Swift 接口。
将软件包添加到您的 Package.swift
文件
dependencies: [
.package(url: "https://github.com/skiptools/swift-android-native.git", from: "1.0.0")
]
此模块为 Android 上的原生 Swift 提供了一个 Logger API,它与 Darwin 平台的 OSLog Logger 兼容。
将 AndroidLogging
模块添加为需要它的任何目标的条件依赖项
.target(name: "MyTarget", dependencies: [
.product(name: "AndroidLogging", package: "swift-android-native", condition: .when(platforms: [.android]))
])
此示例将在 Darwin 平台上使用系统 OSLog
,在 Android 上使用 AndroidLogging
,以在操作系统之间提供通用的日志记录功能
#if canImport(Darwin)
import OSLog
#elseif os(Android)
import AndroidLogging
#endif
let logger = Logger(subsystem: "Subsystem", category: "Category")
logger.info("Hello Android logcat!")
连接的设备和模拟器的 Android 日志消息可以使用终端通过 adb logcat
命令查看。例如,要仅查看上面示例中的日志消息,您可以运行
$ adb logcat '*:S' 'Subsystem/Category:I'
10-27 15:53:12.768 22599 22664 I Subsystem/Category: Hello Android logcat!
Android Studio 提供了图形化查看和过滤日志消息的功能,大多数其他 Android IDE 也是如此。
Logger
函数会将消息转发到 NDK __android_log_write 函数。
OSLogMessage
只是 Swift.String
的类型别名,并且未实现 Darwin 版本的任何 编辑功能。在 Apache 2.0 许可证下获得许可,并带有运行时库例外,这意味着您无需在应用程序中署名该项目。有关详细信息,请参见 LICENSE 文件。