使用 Android 隐藏 API 和内部 API

  • https://github.com/anggrayudi/android-hidden-api

这个仓库包含每个版本的android.jar ,如果是自己编译的可以在

  • out\target\common\obj\JAVA_LIBRARIES\framework_intermediates

这个目录下拷贝class.jar 文件,就包含了自己改的方法和API

我们把完整的 android.jar 放在工程 libs 目录下,也就是平时依赖 jar 的地方,然后在工程 build.gradle 配置的 dependencies 里,以 provided 的方式引用 android.jar。因为每个工程模块依赖 android.jar 的类型就是 provided,这样不会把 android.jar 打包到应用中,运行环境中存在 framework.jar,
直接就可以使用。

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.example.mtkgnss"
        minSdkVersion 16
        targetSdkVersion 28
        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
    }

    signingConfigs {
        myConfig {
            
        }
    }

    buildTypes {
        debug  {
            signingConfig signingConfigs.myConfig
        }
    }

    project(":app") { // app是你工程的名字,配置只对当前工程有效
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                // 注意修改 jar 包的路径,替换 app/libs/hidden_api_23.jar,其他部分不要改
                // Xbootclasspath/p:是 Java 编译的寻址优先设置,先找缺省路径还是全路径
                options.compilerArgs.add('-Xbootclasspath/p:app/libs/android.jar')
            }
        }
    }

}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    provided files('libs/android.jar')

}

按照上面的配置就可以使用了,虽然编辑器会爆红,不影响编译直接打包即可。

你可能感兴趣的:(使用 Android 隐藏 API 和内部 API)