Tuesday, August 26, 2014

Use Android hidden and internal methods

Its possible to use android hidden and internal methods by using the device android.jar instead the ADT's
It will work because the device have a version of android.jar (framework.zip) that have access to this methods.

Steps are (version 4.4.4):

1. copy: C:\Program Files\Android\android-sdk\platforms\android-19 to C:\Program Files\Android\android-sdk\platforms\android-19-internals

2. Download open android.jar and replace the android.jar in the android-19-internals folder. I created this jar from nexus 5 device framework.zip with 4.4.4 OS using a utility that converts dex file to regular classes.

3. Edit build.prop, change ro.build.version.sdk=19 to ro.build.version.sdk=-19 and ro.build.version.release=4.4 to ro.build.version.release=4.4.extended

4. restart eclipse, you will now see the 4.4.extended which you can select for your projects.

* Note that its highly recommended to verify the hidden/internal methods exists before you make such a call, you can do a simple verification using reflaction, here is an example:

 private boolean isRequestPestitentInfoMethodExists() {
        java.lang.reflect.Method method;
        try {
            Class cls = Class.forName("android.net.wifi.p2p.WifiP2pManager");
            method = cls.getMethod("requestPersistentGroupInfo",  Class.forName("android.net.wifi.p2p.WifiP2pManager$Channel"), Class.forName("android.net.wifi.p2p.WifiP2pManager$PersistentGroupInfoListener"));
            Log.i(P.TAG, "isRequestPestitentInfo Method Exists");
            return true;
        } catch (ClassNotFoundException e) {
            Log.e(P.TAG, "ClassNotFoundException: " + e.getMessage());
            return false;
        } catch (NoSuchMethodException e) {
            Log.e(P.TAG, "NoSuchMethodException: " + e.getMessage());
            return false;