前情提要
每一次Android大版本的升级,往往会有大量的APP出现兼容性问题,导致这个情况的主要原因是由于APP的热修复SDKs以及依赖Android internal API(内部API),也就是非SDK API。这些API是指标记@hide的类、方法以及字段,它们不属于官方Android SDK的字段与函数
Google希望未来Android大版本升级,APP都能正常运行,而很多APP对内部API的调用通过反射或JNI间接调用的方法来调用,破坏兼容性。 为此Google从Android P开始限制对内部API的使用
主要是用到以下两个库:
- https://github.com/RikkaApps/HiddenApiRefinePlugin
- https://github.com/RikkaW/HiddenApi
新建库
首先需要新建一个Android Library,名字叫做 hidden-api
修补 gradle
首先需要修改 gradle/libs.versions.toml
在[versions]
中补充
hidden-api-refine = "4.4.0"
devrikka-hidden = "4.3.3"
devrikka-anno = "4.3.0"
activity = "1.9.0"
hiddenapibypass = "4.3"
在[libraries]
中补充
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]
中补充
hiddenApiRefine = { id = "dev.rikka.tools.refine", version.ref = "hidden-api-refine" }
完整的文件如下:
[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
annotationProcessor libs.hiddenapirefineannotationprocessor
compileOnly libs.hiddenapirefineannotation
compileOnly libs.hiddenapistub
implementation libs.hiddenapicompat
完整的文件如下:
plugins {alias(libs.plugins.android.library)
}android {namespace 'com.anonymous.hidden_api'compileSdk 34defaultConfig {minSdk 28testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"consumerProguardFiles "consumer-rules.pro"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}
}dependencies {implementation libs.appcompatimplementation libs.materialtestImplementation libs.junitandroidTestImplementation libs.ext.junitandroidTestImplementation libs.espresso.coreannotationProcessor libs.hiddenapirefineannotationprocessorcompileOnly libs.hiddenapirefineannotationcompileOnly libs.hiddenapistubimplementation libs.hiddenapicompat
}
修补 app
主要是修补其中的 build.gradle
,主要是补充 dependencies
compileOnly project(":hidden-api")
compileOnly libs.hiddenapistub
implementation libs.hiddenapicompat
implementation libs.hiddenapibypass
implementation libs.appcompat
implementation libs.material
完整的文件例子如下:
plugins {alias(libs.plugins.android.application)
}android {namespace 'com.anonymous.strategytest'compileSdk 34defaultConfig {applicationId "com.anonymous.strategytest"minSdk 28targetSdk 34versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}
}dependencies {compileOnly project(":hidden-api")implementation(libs.runtime)compileOnly libs.hiddenapistubimplementation libs.hiddenapicompatimplementation libs.hiddenapibypassimplementation libs.appcompatimplementation libs.materialimplementation libs.activityimplementation libs.constraintlayouttestImplementation libs.junitandroidTestImplementation libs.ext.junitandroidTestImplementation libs.espresso.core
}
使用
在代码前加入如下的代码:
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("");}