Velocity is Like OpenTable on Steroids

We all enjoy a night out with friends or our significant other from time to time. However, there is … more

Android Lollipop Lands for the Sony Xperia Z Ultra

The undisputed king of the beasts–at least in Sony’s current stable,is the … more

Android 5.0 Lollipop in 3D–EVO 3D, That Is!

It is that time of the year once again. Flowers bloom (or snow falls, depending on which … more

Gaming Console with Lollipop? Ouya Gets an Android TV Port

Android is a very flexible platform, and it can be used on a large variety of … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[APP][DEV][GUIDE] Using the Android Java Internal/Hidden API classes

OP E:V:A

15th June 2012, 11:35 AM   |  #1  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
Using Java Reflection with Eclipse ADT to Access Internal/Hidden API classes.

Purpose

We present a way to access all the Internal and Hidden Java packages/classes
in the AOS. To do this we need to both repackage the Android.jar and hack the
Eclipse ADT plugin, to allow using these internal packages.

Posting

==================================================
Do NOT post general questions/requests on how to
do this or that, they will not be answered here.
DO post if you have additional tricks, hacks or
information that can help/benefit this tutorial.

==================================================

Background

There are two reasons one cannot use internal packages. One reason is that, if
you're using Eclipse as your development platform, those packages are
internally blocked in the Eclipse ADT plugin. Second reason is that the normal
development android.jar runtime does not contain those *.class files that
belong to the internal packages.

"There is no easy way to use com.android.internal package (internal API) or
anything marked with @hide attribute (hidden API) without using reflection.
That’s because android.jar file does not contain classes from internal and
hidden API and because of this nobody can reference those classes in compile
time."

Thus we need to first restore the "original" android.jar which will allow us
to use internal and hidden APIs. But the runtime equivalent of Android SDK’s
android.jar file is framework.jar. This file is located in the
/system/framework/ directory of your device. We will extract and use this for
our pleasure.

The general procedure:
A) Grab the "full" framwork.jar from your device
B) extract the class files
C) add them to "full" android.jar ??
D) Hack the Eclipse ADT plugin jar.

Finally, NOTHING would have been possible without the excellent step-by-step
instructions on the devmaze-blog by senior Android developer Ievgenii Nazaruk
(aka. "inazaruk"). THANK YOU Ievgenii!
References

http://stackoverflow.com/questions/7...apis-available
http://stackoverflow.com/questions/4...on-gingerbread
http://code.google.com/p/smali/wiki/DeodexInstructions
http://code.google.com/p/adt-addons/



Last edited by E:V:A; 15th June 2012 at 01:07 PM.
The Following 11 Users Say Thank You to E:V:A For This Useful Post: [ View ]
15th June 2012, 11:35 AM   |  #2  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
The General Procedure
NOTE: All this was performed on Windows Vista with Cygwin.
(1) Grab BOOTCLASSPATH from init.rc

Find the line in your init.rc file that reads something like:
Code:
export BOOTCLASSPATH /system/framework/core.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/core-junit.jar
Extract and reformat the path to:
Code:
core.jar:bouncycastle.jar:ext.jar:framework.jar:android.policy.jar:services.jar:core-junit.jar
(2) Grab the "framework" from your device

Create a working directory somewhere, let's call it "_framework":
Code:
mkdir ./_framework
cd _framework
Grab all the framework files from your device:
Code:
adb pull /system/framework .
Rename directory if needed.

NOTE-1: From now on I'll assume you know where you are!
NOTE-2: Most GB 2.3.4+ devices uses .odex'ed files,
with name pairs like: <package>.jar and <package>.odex.
These need to be converted.

(3) Use baksmali with (1)

You can also use baksmali with the switch: -d <framwork-dir>.
The general command is something like below, but in windows there may be "wrappers"
that allow you to just type "baksmali" without the "java -jar" prefix and without
the ".jar" post fix. Anyway here is the command I used:
Code:
java -Xmx1024m -jar ./../../baksmali.jar -a 10 -c :core.jar:bouncycastle.jar:ext.jar:framework.jar:android.policy.jar:services.jar:core-junit.jar -x framework.odex
==> This results in all files put in the "out" sub-directory.
This directory contain 3 sub-directories (for GB 2.3.4):
Code:
android        (Hidden APIs)
com        (Internal APIs)
javax        (Hidden APIs)
NOTE: If you are using Google's own API's, you will probably also need to add
those packages to the path above. (Eg. Email.jar, etc etc ?)

(4) Then use smali to create a dex file from "out" directory

Code:
java -jar smali.jar out
==> creates out.dex from "out" directory.

(5) Run dex2jar on out.dex

Code:
./dex2jar.bat out.dex
==> creates out_dex2jar.jar
(This .jar contain close to 4900 files at 12 MB!)

(6) Rename "out_dex2jar.jar" to "framework-classes.zip"

Code:
mv out_dex2jar.jar framework-classes.zip
unzip framework-classes.zip


(7) Find and copy your Android SDK's android.jar file


Go to: /path/to/android-sdk-windows/platforms/android-X/android.jar
where X is the API level of interest. This obviously have to match the
API level of the files you extracted from your device in: /system/framework .

This .jar file contain more than 5300 files when expanded, but missing all
the internal packages. Our job is to add them back in.

Let's first make a copy that we can use to expand and add files from (6):
Code:
cp android.jar custom-android.zip 
unzip custom-android.zip


(8) Add all *.class files from (6) in to (7)


Copy and replace all existing *.class files from framework-classes.zip into
custom-android.zip:
Code:
cp -R /path/to/framework-classes/* /path/to/custom-android/.
The root content of that directory should then look something like this:
Code:
android
assets
com
dalvik
java
javax
junit
META-INF
org
res
AndroidManifest.xml
resources.arsc
(9) Rename directory and create your new "classy-android.jar"

We rename it so not confuse with the original:
Code:
mv custom-android classy-android
zip classy-android
mv classy-android.zip classy-android.jar
IMPORTANT:
Make absolutely sure that the folder structure of your zip archive
is exactly the same as what you intended. To check, unzip the file
and see if it is what you (and eventually Eclipse) would expect.
(For example, if you use 7zip to zip a directory file called "test",
into "test.zip", you may end-up extracting it to ./test/test/... )


(10) Enabling & Restricting Access to classy-android.jar

Instead of just replacing the android.jar with classy-android.jar, we choose
to create a customized Android platform. This way you can enable the Internal
and Hidden API's for those projects requiring them, while other standard
projects doesn't have access to those.

(a) Go to: /path/to/android-sdk-windows/platforms/
and copy the relevant directory (for example):
Code:
cp -R android-10 android-10-internals
(b) Replace android.jar with your classy-android.jar:
Code:
cp classy-android.jar android.jar
("cp" overwrites!)

(c) Edit the build.prop file:
Edit/replace the following lines:
Code:
ro.build.version.sdk=10         ==>     ro.build.version.sdk=-10
ro.build.version.release=2.3.3  ==>     ro.build.version.release=2.3.internal
(11) Customizing the Eclipse ADT

In order to be able to use com.android.internal packages in the Eclipse ADT,
you have to disable the internal protection mechanism of the plugin, that
prevent you to use these libraries. You can see this by right-clicking on your
project package and navigate to:
Code:
==>  Properties  ==>  Java Build Path  ==>  Libraries (tab) 
-->  Android 2.x.x  --> android.jar 
--> "Access rules: 1 rule defined": (X) Forbidden: com/android/internal/**
This can not be removed (bug?), even though the interface allows changing, it
never persists after closing the Properties window. So we have to hack it!

The way to do it, is to hexedit the correct java class file and change the
name from "internal" to "internax". First let's find the correct file. The
plugin file is located in the ./eclipse/plugins/ directory, and its name is
something like:
Code:
com.android.ide.eclipse.adt_18.0.0.v201203301601-306762.jar
(a) make a backup copy of this (with the exact name preserved) in another directory.
(b) make a another copy of this in another directory.
(c) unzip (b) in that directory

Code:
cp com.android.ide.eclipse.adt_18.0.0.v201203301601-306762.jar hacked_adt.zip
unzip hacked_adt.zip
cd hacked_adt
This is a huge directory system, so forget poking around in it,
just go to the correct sub-directory:
Code:
cd ./com/android/ide/eclipse/adt/internal/project/
Then find the correct file and the approximate string location within that file:
Code:
strings.exe -f -4 -t x ./*.class |grep "android\/internal"
It happens to be in "AndroidClasspathContainerInitializer.class". Now, use a
hexeditor to find and change the string "com/android/internal/**"
to "com/android/internax/**". That will do it!

Now zip-up your hacked jar directory and copy it over the old one.
(Remember that "cp" overwrites without warning!)
Code:
zip hacked_adt
cp hacked_adt.zip /path/to/eclipse/plugins/com.android.ide.eclipse.adt_18.0.0.v201203301601-306762.jar
You Are Done!
Enjoy your newly hacked Eclipse!
Errors

If you get any errors;
1. make sure you have zipped up everything properly as warned before.
2. make sure you have included Google API packages in your BOOTCLASSPATH in step (3).
3. Try to "clean-up" the Java by: "Right-Click" ==> Source ==> "Clean Up...".
4. Google them
5. Ignore them
6. Give up. Not! But I can't help you!

If it still doesn't work, try to download inazaruk's pre-compiled set of internal android.jar's from here.
(For android 7,8,10,15.)

Last edited by E:V:A; 7th August 2012 at 11:06 AM.
The Following 2 Users Say Thank You to E:V:A For This Useful Post: [ View ]
15th June 2012, 11:36 AM   |  #3  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
WIP! <here be dragons2>
Last edited by E:V:A; 15th June 2012 at 12:13 PM.
15th June 2012, 02:21 PM   |  #4  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
For a project using internal package imports, see my thread:
"[TOOL][APP][WIP] Native AT Command Injector"

<here be more dragons>
Last edited by E:V:A; 15th June 2012 at 06:48 PM.
7th August 2012, 10:56 AM   |  #5  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
Following the instructions in posts 1-2 above, may not always work. It is not known to me at this time, why it shouldn't. One theory is that it can have something to do with how Eclipse and Android.jar is packaging their files and the resulting sizes.

This was mentioned in this Stackoverflow post:
"Jar files: why does extracting then compression a jar file create a file of a different size to the original?"

Then reading the man pages for "jar" we can inform ourselves with:
Code:
       c    Creates a new archive file named jarfile (if f is specified) or to
            standard  output  (if  f  and  jarfile are omitted). Add to it the
            files and directories specified by inputfiles.

       u    Updates an existing file jarfile (when f is specified)  by  adding
            to it files and directories specified by inputfiles. 

       x    Extracts files and directories from jarfile (if f is specified) or
            standard  input  (if  f and jarfile are omitted). If inputfiles is
            specified,  only  those  specified  files  and   directories   are
            extracted. Otherwise, all files and directories are extracted.

       t    Lists  the  table  of contents from jarfile (if f is specified) or
            standard input (if f and jarfile are omitted).  If  inputfiles  is
            specified,  only those specified files and directories are listed.
            Otherwise, all files and directories are listed.

       i    Generate index information  for  the  specified  jarfile  and  its
            dependent jar files. 
More info is provided here:

The JAR Overview @
http://java.sun.com/javase/6/docs/te.../jarGuide.html

The JAR File Specification @
http://java.sun.com/javase/6/docs/te...s/jar/jar.html

The JARIndex Spec @
http://java.sun.com/javase/6/docs/te...s/jar/jar.html

JAR Tutorial @
http://java.sun.com/docs/books/tutorial/jar/

pack200 Reference Page @
http://java.sun.com/javase/6/docs/te...e/pack200.html


Another theory is that it may have something to do with what seem to be, that Google have revoked the the use of MODIFY_PHONE_STATE since Android 2.3, and that this influences the Eclipse behavior, when using and modifying older android.jar's. This was mentioned here and here.

Any help would be very much appreciated!
26th February 2013, 04:05 PM   |  #6  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
< bump >
7th August 2013, 12:05 AM   |  #7  
Junior Member
Thanks Meter: 7
 
14 posts
Join Date:Joined: Oct 2012
Hi, thanks for the info, I made it using linux and worked really nice, these were the lines that I used, hope be useful.

This is my Android folder at home
Code:
Android/
├── eclipse
├── ndk
├── platforms-internals
├── sdk
└── tools
Start an avd running the desired API to modify in this case API-17
Code:
$ emulator -avd avd_api_17 -no-window &
Get the framework
Code:
$ cd ~/Android/
$ mkdir _framework
$ cd _framework
$ adb -s emulator-5554 pull /system/framework .
Grab BOOTCLASSPATH
Code:
$ adb -s emulator-5554 shell cat init.rc | grep BOOTCLASSPATH > bootclasspath
I didn't have the tools used in this tutorial, so I included the steps for getting them

Decompile with baskmali
Code:
$ cd ~/Android/tools
$ wget https://smali.googlecode.com/files/baksmali-1.4.2.jar
$ cd ~/Android/_framework
$ java -Xmx1024m -jar ../tools/baksmali-1.4.2.jar -a 17 -c core.jar:core-junit.jar:bouncycastle.jar:ext.jar:framework.jar:telephony-common.jar:mms-common.jar:android.policy.jar:services.jar:apache-xml.jar -x framework.odex
The parameter -a for baksmali refers to the API we are working with.

Generate out.dex with smali
Code:
$ cd ~/Android/tools
$ wget https://smali.googlecode.com/files/smali-1.4.2.jar
$ cd ~/Android/_framework
$ java -jar ../tools/smali-1.4.2.jar out
Get internal and hidden classes using dex2jar
Code:
$ cd ~/Android
$ wget https://dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip
$ unzip dex2jar-0.0.9.15.zip
$ rm dex2jar-0.0.9.15.zip
$ cd _framework/
$ ../tools/dex2jar-0.0.9.15/d2j-dex2jar.sh out.dex
$ unzip out-dex2jar.jar -d framework-classes
Add these classes to plataform's default android.jar
Code:
$ cd ~/Android
$ unzip  sdk/platforms/android-17/android.jar -d custom-android
$ cp -r _framework/framework-classes/* custom-android/
$ rm -r _framework
$ cd custom-android
$ zip -r ../custom-android.jar *
$ cd ..
$ rm -r custom-android
Create new extended platform
Code:
$ cd ~/Android
$ cp -r sdk/platforms/android-17 platforms-internals/android-17-internals
$ mv custom-android.jar platforms-internals/android-17-internals/android.jar
$ vi platforms-internals/android-17-internals/build.prop

 ro.build.version.release=4.2.2
 ro.build.version.release=4.2.2.internal

$ ln -s ~/Android/platforms-internals/android-17-internals ~/Android/sdk/platforms/android-17-internals
I use a symlink for keep it a little organized

Hack ADT
Code:
$ cd ~/Android
$ unzip eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar -d hacked_adt
Go to right folder
Code:
$ cd hacked_adt/com/android/ide/eclipse/adt/internal/project/
Find file where is our desired string
Code:
$ strings -f -a -t x * | grep "android\/internal"
Edit with an hex editor
Code:
$ bless AndroidClasspathContainerInitializer.class &
Here we change the l for the x.

Replace original file making a backup
Code:
$ cd ~/Android
$ cp eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar.original
$ cd hacked_adt/
$ zip -r ../eclipse/plugins/com.android.ide.eclipse.adt_22.0.4.v201307151829--741630.jar *
$ cd ..
$ rm -r hacked_adt
This worked for me... thanks E:V:A

I got just one error related to a dropbox class, but i think this is not important... hope that
Last edited by lenieto3; 9th August 2013 at 01:13 AM. Reason: get it better
The Following 2 Users Say Thank You to lenieto3 For This Useful Post: [ View ]
5th January 2014, 10:45 PM   |  #8  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
Quote:
Originally Posted by lenieto3

...Start an avd running the desired API to modify in this case API-17... This worked for me... I got just one error related to a dropbox class, but i think this is not important...

Thanks and sorry for late reply. I'm very happy to hear these instructions still works with API-17! Could you also upload your hacked JAR somewhere so that people can save some time when experimenting?

I was just here to check-in and try to bump this thread to see if it is still useful to anyone.
27th January 2014, 06:28 AM   |  #9  
Mohammad_Adib's Avatar
Senior Member
Flag Bellevue, WA
Thanks Meter: 3,270
 
1,287 posts
Join Date:Joined: Sep 2011
More
Quote:
Originally Posted by E:V:A

Thanks and sorry for late reply. I'm very happy to hear these instructions still works with API-17! Could you also upload your hacked JAR somewhere so that people can save some time when experimenting?

I was just here to check-in and try to bump this thread to see if it is still useful to anyone.

ive got access to ActivityManager's hidden methods.

I want to use the removeTask method, but it keeps saying that I dont have the REMOVE_TASKS permissions even though I added it to the manifest (and turned off lint).

Permission Denial: removeTask() from pid=9963, uid=10179 requires android.permission.REMOVE_TASKS
4th April 2014, 12:40 PM   |  #10  
E:V:A's Avatar
OP Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,870
 
1,378 posts
Join Date:Joined: Dec 2011
Does someone know if there are any automated tools to do/performs steps 1-9?

I'd like to see a tool to automatically pull (from phone), extract and create a compatible android.jar.

@Mohammad_Adib: Sorry, this is the wrong thread for those type of questions.

Post Reply Subscribe to Thread

Tags
api, eclipse, internal, java, reflection
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes