使用反射调用Android隐藏API

前情提要

每一次Android大版本的升级,往往会有大量的APP出现兼容性问题,导致这个情况的主要原因是由于APP的热修复SDKs以及依赖Android internal API(内部API),也就是非SDK API。这些API是指标记@hide的类、方法以及字段,它们不属于官方Android SDK的字段与函数

Google希望未来Android大版本升级,APP都能正常运行,而很多APP对内部API的调用通过反射或JNI间接调用的方法来调用,破坏兼容性。 为此Google从Android P开始限制对内部API的使用

主要是用到以下两个库:

新建库

首先需要新建一个Android Library,名字叫做 hidden-api

修补 gradle

首先需要修改 gradle/libs.versions.toml

[versions]中补充

bash 复制代码
hidden-api-refine = "4.4.0"
devrikka-hidden = "4.3.3"
devrikka-anno = "4.3.0"
activity = "1.9.0"
hiddenapibypass = "4.3"

[libraries]中补充

bash 复制代码
hiddenapirefineannotationprocessor = { group = "dev.rikka.tools.refine", name = "annotation-processor", version.ref = "hidden-api-refine"}
hiddenapirefineannotation = { group = "dev.rikka.tools.refine", name = "annotation", version.ref = "devrikka-anno"}
hiddenapicompat = { group = "dev.rikka.hidden", name = "compat", version.ref = "devrikka-hidden"}
hiddenapistub = { group = "dev.rikka.hidden", name = "stub", version.ref = "devrikka-hidden"}
runtime = { module = "dev.rikka.tools.refine:runtime", version.ref = "hidden-api-refine" }
hiddenapibypass = { module = "org.lsposed.hiddenapibypass:hiddenapibypass", version.ref = "hiddenapibypass" }

[plugins]中补充

bash 复制代码
hiddenApiRefine = { id = "dev.rikka.tools.refine", version.ref = "hidden-api-refine" }

完整的文件如下:

bash 复制代码
[versions]
agp = "8.4.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
appcompat = "1.6.1"
material = "1.12.0"
constraintlayout = "2.1.4"
hidden-api-refine = "4.4.0"
devrikka-hidden = "4.3.3"
devrikka-anno = "4.3.0"
activity = "1.9.0"
hiddenapibypass = "4.3"


[libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
hiddenapirefineannotationprocessor = { group = "dev.rikka.tools.refine", name = "annotation-processor", version.ref = "hidden-api-refine"}
hiddenapirefineannotation = { group = "dev.rikka.tools.refine", name = "annotation", version.ref = "devrikka-anno"}
hiddenapicompat = { group = "dev.rikka.hidden", name = "compat", version.ref = "devrikka-hidden"}
hiddenapistub = { group = "dev.rikka.hidden", name = "stub", version.ref = "devrikka-hidden"}
runtime = { module = "dev.rikka.tools.refine:runtime", version.ref = "hidden-api-refine" }
hiddenapibypass = { module = "org.lsposed.hiddenapibypass:hiddenapibypass", version.ref = "hiddenapibypass" }


[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
hiddenApiRefine = { id = "dev.rikka.tools.refine", version.ref = "hidden-api-refine" }

修补 hidden-api

主要是修补其中的 build.gradle,主要是补充 dependencies

bash 复制代码
annotationProcessor libs.hiddenapirefineannotationprocessor
compileOnly libs.hiddenapirefineannotation
compileOnly libs.hiddenapistub
implementation libs.hiddenapicompat

完整的文件如下:

bash 复制代码
plugins {
    alias(libs.plugins.android.library)
}

android {
    namespace 'com.anonymous.hidden_api'
    compileSdk 34

    defaultConfig {
        minSdk 28

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation libs.appcompat
    implementation libs.material
    testImplementation libs.junit
    androidTestImplementation libs.ext.junit
    androidTestImplementation libs.espresso.core

    annotationProcessor libs.hiddenapirefineannotationprocessor
    compileOnly libs.hiddenapirefineannotation
    compileOnly libs.hiddenapistub
    implementation libs.hiddenapicompat
}

修补 app

主要是修补其中的 build.gradle,主要是补充 dependencies

bash 复制代码
compileOnly project(":hidden-api")
compileOnly libs.hiddenapistub
implementation libs.hiddenapicompat
implementation libs.hiddenapibypass
implementation libs.appcompat
implementation libs.material

完整的文件例子如下:

bash 复制代码
plugins {
    alias(libs.plugins.android.application)
}

android {
    namespace 'com.anonymous.strategytest'
    compileSdk 34

    defaultConfig {
        applicationId "com.anonymous.strategytest"
        minSdk 28
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    compileOnly project(":hidden-api")

    implementation(libs.runtime)
    compileOnly libs.hiddenapistub
    implementation libs.hiddenapicompat
    implementation libs.hiddenapibypass
    implementation libs.appcompat
    implementation libs.material
    implementation libs.activity
    implementation libs.constraintlayout
    testImplementation libs.junit
    androidTestImplementation libs.ext.junit
    androidTestImplementation libs.espresso.core
}

使用

在代码前加入如下的代码:

bash 复制代码
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
//            try {
//                HiddenApiBypass.newInstance(Class.forName("android.content.IIntentSender$Stub")/*, args*/);
//            } catch (ClassNotFoundException e) {
//                throw new RuntimeException(e);
//            }
            HiddenApiBypass.addHiddenApiExemptions("");
        }
相关推荐
拭心10 小时前
Google 提供的 Android 端上大模型组件:MediaPipe LLM 介绍
android
带电的小王12 小时前
WhisperKit: Android 端测试 Whisper -- Android手机(Qualcomm GPU)部署音频大模型
android·智能手机·whisper·qualcomm
梦想平凡13 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
元争栈道13 小时前
webview和H5来实现的android短视频(短剧)音视频播放依赖控件
android·音视频
阿甘知识库14 小时前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站
元争栈道14 小时前
webview+H5来实现的android短视频(短剧)音视频播放依赖控件资源
android·音视频
MuYe15 小时前
Android Hook - 动态加载so库
android
居居飒15 小时前
Android学习(四)-Kotlin编程语言-for循环
android·学习·kotlin
Henry_He18 小时前
桌面列表小部件不能点击的问题分析
android
工程师老罗18 小时前
Android笔试面试题AI答之Android基础(1)
android