Android网络请求监控与数据上报

Monitor-Network

基于 ServiceLoader,监控 Okhttp 网络请求,实现拦截、解析、转发、存储、上报等功能。采用组件化开发方式,各业务组件均可单独拆分使用,同时提供export对外调用组件,外部调用无需关心内部实现,组件之间集成完全解耦

Github 地址 > github.com/MannaYang/M...

集成组件介绍

Components-stone

  • 提供基础依赖管理build.gradle.kts(可修改依赖版本)
  • 提供DataStore/Gson/RetrofitProvider对外调用管理类(可替换)
  • 提供NetworkInterceptor全局拦截器(Okhttp Interceptor)(如需替换,需全局处理接口注解)
  • 提供Application生命周期分发管理,基于@AutoService注解生成并解耦( 如需替换,需全局处理接口注解)

Components-http-export

该组件为对外集成组件,可直接依赖

  • HttpDataResult.kt 提供对外获取Interceptor拦截器解析实体model
  • HttpInterceptorListener.kt 提供对外转发实体model数据接口,可供上层业务获取网络请求数据并自定义处理,参考app组件模块[HttpInterceptor.kt](https://link.juejin.cn?target=app%252Fsrc%252Fmain%252Fjava%252Fcom%252Fmanna%252Fmonitor%252Fnetwork%252Finterceptor%252FHttpInterceptor.kt "app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fmanna%2Fmonitor%2Fnetwork%2Finterceptor%2FHttpInterceptor.kt")

Components-http

该组件是http-export内部业务实现组件,处理网络请求白名单、解析、转发等业务操作,不可直接依赖,建议上层business业务集成

Components-room-export

该组件为对外集成组件,可直接依赖

Components-room

该组件是room-export内部业务实现组件,提供room database、table、dao实现

Components-report-export

该组件为对外集成组件,可直接依赖

  • WorkReport.kt 对外开放业务查询过滤实体、业务上报平台区分,关联WorkManager-InputData参数

Components-report

该组件是report-export内部业务实现组件,提供WorkManager后台上报功能

  • dingtalk 钉钉工作空间及鉴权服务与数据上报(仅作参考),具体实现可换为实际上报业务平台
  • markdown 格式化上报报文(仅作参考),具体按实际业务要求处理
  • work 处理数据上报及冗余数据处理(仅作参考),具体按实际业务要求处理

Components-app

上层业务集成组件壳,包含所有export及内部实现组件,组装额外公共参数(用户信息、设备信息、应用信息)

scss 复制代码
kapt("com.google.auto.service:auto-service:1.1.1")
api("com.google.auto.service:auto-service-annotations:1.1.1")
implementation(project(":stone"))
implementation(project(":http"))
implementation(project(":http-export"))
implementation(project(":room"))
implementation(project(":room-export"))
implementation(project(":report"))
implementation(project(":report-export"))

具体调用方法在MainActivity.ktHttpInterceptor.kt,应用内BaseApplication应替换为项目实际使用类

使用说明

项目中为了演示效果采用钉钉上报,因此需要申请企业应用AppKey/SecretKey/AgentId/进行鉴权以,通过以上key信息获取UserIds完成消息推送通知, 实际应用中推荐上报到日志中台或相关日志平台,作为全链路监控的一环

钉钉开放平台 > open.dingtalk.com/
gradle.properties 需配置以下内容,关联影响鉴权方法位置 : DingRepository.kt

ini 复制代码
# Ding key config
dingAppKey=
dingSecretKey=
dingAgentId=
dingUsersId=

实际调用代码示例

DingProvider.kt

kotlin 复制代码
suspend fun reportWorkSpace(title: String, content: String, success: (Boolean) -> Unit) {
  ...
  ...
  ...
}

可自定义DataServerProvider.kt ,处理实际数据上报平台

kotlin 复制代码
suspend fun reportDataServer(title: String, content: String, success: (Boolean) -> Unit) {
   //do something
}

suspend fun reportDataOther(title: String, content: String, success: (Boolean) -> Unit) {
   //do something
}

演示效果截图 screenshot

ServiceLoader

基于@AutoService注解生成的metadata可在各自组件build目录查看,例如:

  • report/build/intermediates/java_res/debug/out/META_INF/services/*
  • report/build/intermediates/runtime_library_classes_dir/debug/META_INF/services/*
  • report/build/tmp/kapt3/classes/debug/META_INF.services

使用方式可参考各业务组件中含有@AutoService class,例如:

auto-service官方文档地址 > github.com/google/auto...
Declaration Interface

kotlin 复制代码
/**
 * Define lifecycle methods what you want to use
 */
interface ApplicationLifecycle {

    /** Lifecycle onAttachBaseContext */
    fun onAttachBaseContext(context: Context)

    /** Lifecycle onCreate */
    fun onCreate(application: Application)

    /** marking priority,0 is highest priority,next is 1,2,3...100 ,you can custom it */
    fun priority(): Int
}

Annotation Class

kotlin 复制代码
/**
 * Add google [@AutoService] annotation,to implementation it
 */
@AutoService(ApplicationLifecycle::class)
class ApplicationLifecycleProxy : ApplicationLifecycle {

    override fun onAttachBaseContext(context: Context) {
        //do somethings
    }

    override fun onCreate(application: Application) {
        //do somethings
        DataStoreProvider.initDataStore(application)
    }

    override fun priority(): Int {
        //in base application context,do somethings highest priority,so you should return 0
        return 0
    }
}

ServiceLoader.load

kotlin 复制代码
/**
 * With google auto-service,it will collect all @AutoService annotation class
 */
class ServiceLoaderProxy {

    private val loader: ServiceLoader<ApplicationLifecycle> by lazy {
        ServiceLoader.load(ApplicationLifecycle::class.java)
    }

    val lifecycleQueue by lazy { loader.sortedWith(compareBy { it.priority() }) }
}

引用三方库

  1. Google JetPacks > developer.android.com/jetpack
  2. Kotlin Coroutines > github.com/Kotlin/kotl...
  3. Retrofit & Okhttp > github.com/square/retr... & github.com/square/okht...
  4. Gson > github.com/google/gson
  5. auto-service > github.com/google/auto...
相关推荐
anxiao_m14 分钟前
2026大文件传输软件推荐,从速度安全多维度客观测评
数据库·文件传输·传输
IT大白鼠27 分钟前
MySQL 分布式集群系列 · 第七篇——生产调优实战:NDB 性能、内存、高可用全方位优化
数据库·分布式·mysql
IvorySQL28 分钟前
打造下一代 AI Agent 的统一多模智能数据底座——PostgreSQL 与 AI 的融合演进
数据库·人工智能·postgresql
ShineWinsu29 分钟前
对于MySQL:内置函数的解析
linux·数据库·c++·mysql·面试·函数·查询
isNotNullX36 分钟前
智能问数为什么答不准?数据、语义、模型、SQL四层拆解
数据库
烂蜻蜓2 小时前
Django入门教程(三):django-admin与manage.py命令完全指南
数据库·django·sqlite
yunlaodacom2 小时前
腾讯云国际版代理商:COS标准、低频、归档和深度归档怎么选?存储成本与数据取回区别
数据库·云计算·腾讯云
Nturmoils2 小时前
SQL Server数据库迁移:V9R4C019 如何接住存量 T-SQL 批处理
数据库
NineData2 小时前
NineData智能数据管理平台新功能发布|2026年8月
数据库·人工智能·oracle·中间件·agent·数据库开发·ninedata
Elastic 中国社区官方博客2 小时前
Elasticsearch 向量数据库:几分钟内完成部署,以经济高效的方式扩展至数千亿规模
大数据·运维·数据库·elasticsearch·搜索引擎·ai·全文检索