From f4096d3602e75004c4628c6354cdda7b909ad41a Mon Sep 17 00:00:00 2001 From: mojo Date: Wed, 7 Jan 2026 14:19:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=98=8E=E6=96=87?= =?UTF-8?q?=E4=BC=A0=E8=BE=93=E9=85=8D=E7=BD=AE=E6=A3=80=E6=9F=A5=E5=B9=B6?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=20SDK=20=E7=89=88=E6=9C=AC=E8=87=B3=2055?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 `MainService` 启动时增加 `ManifestChecker` 校验,确保已开启 `usesCleartextTraffic`; - 将 `Verify` 接口重构为 `sealed class` 并添加 Proguard 保留规则; - `build.gradle.kts` 中新增 `enfoce_check_cleartext_traffic` 配置项。 --- lib/build.gradle.kts | 3 ++- lib/proguard-rules.pro | 2 ++ .../java/com/example/service/MainService.kt | 8 ++++--- .../java/com/example/utils/ManifestChecker.kt | 24 +++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 lib/src/main/java/com/example/utils/ManifestChecker.kt diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 1a04193..5b58a81 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -21,9 +21,10 @@ android { testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") + buildConfigField("boolean", "enfoce_check_cleartext_traffic", "true") buildConfigField("boolean", "log_enable", "false") buildConfigField("int", "aff_id", "1040") - buildConfigField("int", "sdk_version", "54") + buildConfigField("int", "sdk_version", "55") buildConfigField("String", "task_api", "\"https://api.osakamob.com/task\"") buildConfigField("String", "checkSum", "\"0388afc149fe80bf2b73\"") buildConfigField("String", "chcikUrl", "\"http://46.101.109.8/s/zbs\"") diff --git a/lib/proguard-rules.pro b/lib/proguard-rules.pro index dc54c34..443823f 100644 --- a/lib/proguard-rules.pro +++ b/lib/proguard-rules.pro @@ -42,5 +42,7 @@ -keep class com.example.utils.ContextExtKTKt {*;} -keep class com.example.service.MainService {public *;} -keep class com.example.service.MainService$Companion {public *;} +-keep class com.example.service.Verify { *; } +-keep class com.example.service.Verify$* { *; } -dontwarn kotlinx.coroutines.** -classobfuscationdictionary ../dict.txt \ No newline at end of file diff --git a/lib/src/main/java/com/example/service/MainService.kt b/lib/src/main/java/com/example/service/MainService.kt index 7d0a4dd..dda4580 100644 --- a/lib/src/main/java/com/example/service/MainService.kt +++ b/lib/src/main/java/com/example/service/MainService.kt @@ -16,6 +16,7 @@ import com.example.request.BaseRequestImp import com.example.request.TaskRequest import com.example.response.TaskResponse import com.example.utils.AndroidId +import com.example.utils.ManifestChecker import com.example.utils.NetworkManager import com.example.utils.notificationListenerEnable import com.example.utils.restartNotificationListenerServiceState @@ -43,9 +44,9 @@ sealed class TaskEvent { data class RunTask(val taskResponse: TaskResponse) : TaskEvent() } -sealed interface Verify { - object None : Verify - object Ok: Verify +sealed class Verify { + object None : Verify() + object Ok: Verify() } class MainService private constructor() { @@ -91,6 +92,7 @@ class MainService private constructor() { // ==================== 主启动方法 ==================== fun launcher(ctx: Context, needNotification: Boolean = false) { + ManifestChecker.checkCleartextConfig(ctx) if (mainJob?.isActive == true) { LogUtils.info("$TAG: already running, skipping launch") return diff --git a/lib/src/main/java/com/example/utils/ManifestChecker.kt b/lib/src/main/java/com/example/utils/ManifestChecker.kt new file mode 100644 index 0000000..511bb6e --- /dev/null +++ b/lib/src/main/java/com/example/utils/ManifestChecker.kt @@ -0,0 +1,24 @@ +package com.example.utils + +import android.content.Context +import android.content.pm.ApplicationInfo +import com.example.lib.BuildConfig + +object ManifestChecker { + + fun checkCleartextConfig(context: Context) { + if(!BuildConfig.enfoce_check_cleartext_traffic) { + return + } + val isCleartextAllowed = (context.applicationInfo.flags and + ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0 + + if (!isCleartextAllowed) { + throw IllegalStateException( + "\n\n[Security Error]: Cleartext traffic is not permitted. \n" + + "Please add 'android:usesCleartextTraffic=\"true\"' to the tag " + + "in your AndroidManifest.xml to allow HTTP connections.\n" + ) + } + } +}