使用反射调用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("");
        }
相关推荐
烬奇小云3 小时前
认识一下Unicorn
android·python·安全·系统安全
顾北川_野15 小时前
Android 进入浏览器下载应用,下载的是bin文件无法安装,应为apk文件
android
CYRUS STUDIO15 小时前
Android 下内联汇编,Android Studio 汇编开发
android·汇编·arm开发·android studio·arm
右手吉他15 小时前
Android ANR分析总结
android
PenguinLetsGo17 小时前
关于 Android15 GKI2407R40 导致梆梆加固软件崩溃
android·linux
杨武博19 小时前
音频格式转换
android·音视频
音视频牛哥21 小时前
Android音视频直播低延迟探究之:WLAN低延迟模式
android·音视频·实时音视频·大牛直播sdk·rtsp播放器·rtmp播放器·android rtmp
ChangYan.1 天前
CondaError: Run ‘conda init‘ before ‘conda activate‘解决办法
android·conda
二流小码农1 天前
鸿蒙开发:ForEach中为什么键值生成函数很重要
android·ios·harmonyos
夏非夏1 天前
Android 生成并加载PDF文件
android