KDeviceInfo is Kotlin Multiplatform Library - allows to access the device details of android and ios devices without writing expect actual boilerplate code.
Android | Ios |
---|---|
IMPORTANT:
The project is in the experimental phase. All APIs can change incompatibly or be dropped without the deprecation cycle!
This library is available on mavenCentral. To use it in your project add following block of code
in settings.gradle.kts
file if not available.
repositories {
...
mavenCentral()
}
Add the library dependency in shared
module (build.gradle.kts
)
commonMain.dependencies {
...
api or implementation("io.github.swapnil-musale:KDeviceInfo:0.0.7")
}
Add the library dependency in composeApp
module (build.gradle.kts
)
commonMain.dependencies {
...
implementation("io.github.swapnil-musale:KDeviceInfo:0.0.7")
}
Implementation of KDeviceInfo is pretty straightforward. Simply import the DeviceInfoXState and start accessing device information in your Compose Multiplatform project.
@Composable
fun DeviceInfoExample() {
val deviceInfoXState: DeviceInfoXState = rememberDeviceInfoXState()
Column(modifier = Modifier.fillMaxSize()) {
if (deviceInfoXState.isAndroid) {
val androidInfo = deviceInfoXState.androidInfo
Text(text = "App Name : ${androidInfo.appName}")
Text(text = "SdkInt : ${androidInfo.version.sdkInt}")
Text(text = "PackageName : ${androidInfo.packageName}")
} else if(deviceInfoXState.isIos) {
val iosInfo = deviceInfoXState.iosInfo
Text(text = "SystemName : ${iosInfo.systemName}")
Text(text = "SystemVersion : ${iosInfo.systemVersion}")
Text(text = "IsPhysicalDevice : ${iosInfo.isPhysicalDevice}")
} else if(deviceInfoXState.isDesktop) {
Text(text = "Welcome to Desktop App")
}
}
}
OR Use helper function to access device information
@Composable
fun DeviceInfoExample() {
Column(modifier = Modifier.fillMaxSize()) {
OnPlatform(
onAndroid = { androidInfo ->
Text(text = "App Name : ${androidInfo.appName}")
Text(text = "SdkInt : ${androidInfo.version.sdkInt}")
Text(text = "PackageName : ${androidInfo.packageName}")
},
onIos = { iosInfo ->
Text(text = "SystemName : ${iosInfo.systemName}")
Text(text = "SystemVersion : ${iosInfo.systemVersion}")
Text(text = "IsPhysicalDevice : ${iosInfo.isPhysicalDevice}")
}
)
}
}
To access the device details outside the Composable function you can make use of DeviceInfoState as below
class AppViewModel {
private val deviceInfoXState: DeviceInfoXState = DeviceInfoXState()
init {
if (deviceInfoXState.isAndroid) {
val androidInfo: AndroidInfo = deviceInfoXState.androidInfo
println("DeviceInfoX - App Name : ${androidInfo.appName}")
println("DeviceInfoX - Version Name : ${androidInfo.versionName}")
} else else if (deviceInfoXState.isIos) {
val iosInfo: IosInfo = deviceInfoXState.iosInfo
println("DeviceInfoX - App Name : ${iosInfo.appName}")
println("DeviceInfoX - App Version : ${iosInfo.appVersion}")
}
}
}
OR Use helper function to access device information
class AppViewModel {
init {
onPlatform(
onAndroid = { androidInfo ->
println("DeviceInfoX - App Name : ${androidInfo.appName}")
println("DeviceInfoX - Version Name : ${androidInfo.versionName}")
},
onIos = { iosInfo ->
println("DeviceInfoX - App Name : ${iosInfo.appName}")
println("DeviceInfoX - App Version : ${iosInfo.appVersion}")
}
)
}
}
You can access more info using this State API, for more information about API usage in your Compose Multiplatform project check out Sample App.
Contributions to KDeviceInfo are super welcome checkout Contributions Guidelines. If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on GitHub.
MIT License
Copyright (c) 2024 Swapnil Musale
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.