使用反射调用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("");
        }
相关推荐
闲暇部落2 小时前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
诸神黄昏EX4 小时前
Android 分区相关介绍
android
大白要努力!5 小时前
android 使用SQLiteOpenHelper 如何优化数据库的性能
android·数据库·oracle
Estar.Lee5 小时前
时间操作[取当前北京时间]免费API接口教程
android·网络·后端·网络协议·tcp/ip
Winston Wood5 小时前
Perfetto学习大全
android·性能优化·perfetto
Dnelic-8 小时前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen11 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年18 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿20 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神1 天前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri