Importing Android SDK with hidden APIs

How to import ActivityManagerNative?

My first motivation was to use AndroidManagerNative class, even you can find this class in Android open source project, you can’t use it in our app when you build with usual Android SDK.

Yes, some of the classes are hidden in SDK. More specifically, $SDK_PATH/platforms/android-xx/android.jar contains limited APIs for Android app developers.

I put memo how to use these hidden classes/methods in your Android app development from Android studio (which works with build.gradle).

Building Android SDK including all APIs

First step is to prepare library which includes hidden Android APIs. Below link answers how to do this.

How do I build the Android SDK with hidden and internal APIs available?

  1. Build the repo or download jars from https://sites.google.com/site/hippunosource/home/android/androidnohide-apiwo-shi-yongsuru-rifurekushonha-wei-shi-yong
  2. copy out out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar (better to rename it as something like framework_all.jar)
  3. config your project build path–>libraries –> add this external jars. In Order and Export, move it up and before android.jar

So after fullbuilding AOSP source code, you can get desired jar file at out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
I will note it as framework_all.jar in the following.

Another way to build only SDK is to use below command. (Reference: How to build my own Android SDK)

make -j8 PRODUCT-sdk-sdk showcommands dist

See also Eclipse上からAndroidのHide APIを使用する(リフレクションは未使用) (maybe only for Eclipse user).

Adding library in Android studio

Copy this framework_all.jar file at app/libs folder in your Android project, right click and choose “Add as library”

Now you can import hidden API, e.g. ActivityManagerNative!

However building failed. Because framework_all.jar is big library, including many methods, which causes some error while building.

build.gradle settings: compile error fix for including framework_all.jar 

java.lang.OutOfMemoryError: GC overhead limit exceeded

Add following options to increase java heap size,

android {
    ...
    dexOptions {
        javaMaxHeapSize "4g"
    }
}

Reference: Android Studio Google jar causing GC overhead limit exceeded error

trouble writing output: Too many method references: 83845; max is 65536.

There is a limitation dx compiler, up to 65536 method references is allowed per one dex file, while framework_all includes more than 65536 methods. I got following error

trouble writing output: Too many method references: 83845; max is 65536.
You may try using --multi-dex option.
References by package:
 27 android
 147 android.accessibilityservice
 598 android.accounts
 ...

To cooperate this, add multiDexEnabled options to support multidex inside defaultConfig closure,

android {
    ...
    defaultConfig {
        applicationId "..."
        minSdkVersion 22
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        // Enabling multidex support.
        multiDexEnabled true
    }
}

Reference

Leave a Comment

Your email address will not be published. Required fields are marked *