In the last article, we created a developer account for using Huawei Mobile Services and prepared a project for using them. Now it's time to start embedding specific services.
Here is a complete list of articles from the series:
- We create a developer account, connect dependencies, prepare the code for deployment. tyk
- We build Huawei Analytics. β you are here
- Huawei.
- Huawei maps. Google maps AppGallery.
. , :
interface Analytics {
fun send(event: AnalyticsEvent)
}
interface AnalyticsEvent {
val key: String
val data: Map<String, Any>
}
fun Map<String, Any>.toBundle() =
Bundle().apply {
forEach { (key, value) ->
when (value) {
is String -> putString(key, value)
is Int -> putInt(key, value)
is Boolean -> putBoolean(key, value)
is Double -> putDouble(key, value)
is Float -> putFloat(key, value)
else -> throw IllegalArgumentException("Unknown data type: ${value::class.simpleName}")
}
}
}
open class SimpleEvent(override val key: String) : AnalyticsEvent {
override val data: Map<String, Any> = hashMapOf()
override fun toString(): String = "AnalyticsEvent { key = $key, data = $data }"
}
open class ParamsEvent(key: String, vararg params: Pair<String, Any>): SimpleEvent(key) {
override val data = params.toMap()
}
class EventOpenSomeScreen : SimpleEvent("screen_some_screen")
, - , - :
@Inject
lateinit var analytics: Analytics
...
analytics.send(EventOpenSomeScreen())
, .
- , huawei
flavor
- , google β :
dependencies {
huaweiImplementation 'com.huawei.agconnect:agconnect-core:1.3.1.300'
huaweiImplementation 'com.huawei.hms:hianalytics:5.0.0.301'
googleImplementation 'com.google.firebase:firebase-analytics:17.2.3'
}
- DI
Analytics
AnalyticsImpl
.AnalyticsImpl
. βsrc/huawei/kotlin/com/example
:
class AnalyticsImpl(context: Context) : Analytics {
private val analytics = HiAnalytics.getInstance(context)
override fun send(event: AnalyticsEvent) {
analytics.onEvent(event.key, event.data.toBundle())
}
}
src/google/kotlin/com/example
:
class AnalyticsImpl(context: Context) : Analytics {
private val firebaseAnalytics = FirebaseAnalytics.getInstance(context)
override fun send(event: AnalyticsEvent) {
firebaseAnalytics.logEvent(event.key, event.data.toBundle())
}
}
. API .
,
, , Huawei . :
- .
-
adb shell setprop debug.huawei.hms.analytics.app _APPLICATION_ID__
- , AppGallery Connect -> -> -> "" -> -> .
- .
-
adb shell setprop debug.huawei.hms.analytics.app .none.
:
β
, Huawei , Google.
All the code that is in this series of articles you can see in the repository on GitHub. Here is the link .