A few years ago I wrote a short article on how you can access, and use, Android internal API’s in Eclipse. This is still a popular article which still generate hits every day.

Sadly it is perhaps a bit dated – especially since Eclipse is no more and Android Studio is the new toolbox, so I decided to see if I could do the same with Android Studio and Android 5.1 aka Lollipop…

I must admit that I initially tried to fix an important “feature” which after many hours turned out to be void, but such is life I guess… What I was trying to find was the previously hard-coded string “com/android/internal/**” which used to stop the ADT plugin from allowing us to compile with android internals. It turned out that this limitation has not (yet?) been implemented in Android Studio. I now have ~4GB source code for Android Studio sitting on my build server to no current use… 😉

Anyhow, here is how you enable Android Internals in Android Studio, and a small example application as a proof of concept!

First you must create a android.jar which contains the necessary classes:

Connect the phone to the computer and run the following commands.

adb pull /system/framework/framework.jar unzip framework.jar d2j-dex2jar.bat classes2.dex

dex2jar can be downloaded from SourceForge. https://sourceforge.net/projects/dex2jar/

Select and open android.jar (…/sdk/platforms/android-22/android.jar in this example, and make sure you pick the same version as you’ve pulled from your phone!) in the SDK and add all classes/files that you’ve extracted from classes2.dex

Jar-files are actually zip-files so you can open them in your favourite zip-handling tool, or you can simply unpack them, copy the files and then pack a new android.jar, whatever method you prefer.

Start Android Studio and create a new project without an activity.

Edit build.gradle (Module:app) and change targetSdkVersion to the same as compileSdkVersion in order to make sure the correct android.jar is used when compiling.

I also had to comment out the dependencies in order for gradle to build correctly. I’m using targetSdkVersion 22 in my example below but you must use whatever sdk version you have on your phone.

dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') //testCompile 'junit:junit:4.12' //compile 'com.android.support:appcompat-v7:23.3.0' //compile 'com.android.support:design:23.3.0' }

Create an activity (MainActivity.java)

package com.politisktinkorrektpappa.hack.hackinternals; import android.app.Activity; import android.os.Bundle; import com.android.internal.widget.LockPatternView; /** * Created by PolitisktInkorrektPappa on 2016-04-21. * HackInternals */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.unlock); LockPatternView lpv = (LockPatternView) findViewById(R.id.lockPattern); } }

Create the layout (unlock.xml)

<?xml version="1.0" encoding="utf-8"?> <com.android.internal.widget.LinearLayoutWithDefaultTouchRecepient xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/patternView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00000000" android:orientation="horizontal" > <LinearLayout android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_marginLeft="0.0dip" android:layout_weight="1.0" android:gravity="left" android:orientation="vertical" android:weightSum="1"> <com.android.internal.widget.LockPatternView android:id="@+id/lockPattern" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> </com.android.internal.widget.LinearLayoutWithDefaultTouchRecepient>

I was unable to get visual rendering working in Android Studio for the layout of the LockPatternView but I’ve only just started using Android Studio so perhaps this is something that can be fixed?

You will now be able to build and deploy your application to your phone and verify that it works.

BUILD SUCCESSFUL Total time: 3.075 secs

Sometimes it’s nice to have access to Android Internals when creating an app, but remember – it’s not something you can deploy on Google Play because you’d be in breach of EULA and the fine-print you have to sign when setting up a developer account, but for home tinkering etc it’s perfectly fine!

Happy hacking!

Share this: Twitter

Facebook

Reddit

Tumblr

Email

