I thought I would have a quick run through some of the tutorial info on getting a basic Android application built and then run it on the Android x86 port I have running in a VM.
I downloaded the adt bundle for linux which includes the sdk, a version of eclipse and the adt plugin. First time through I forgot that I did not have a jdk environment installed so had to grab that as well.
I also added the platform-tools and tools directories to my PATH.
Following the instructions I tested out a basic layout on the emulator that comes as part of the bundle and then exported my project as an Android application.
Here is where I believed the tricky part would be, to get the apk file onto my android instance running in a VM. I could have placed it on a webserver and then downloaded it (but where is the fun in that). If this was runnign on a real device/handset I could connect it up via usb and use adb.
Actually I can still use adb, as android is running in a VM and has networkig configured I just need to configure adb to connect to the VM.
I do this by the command
adb connect <ip address of VM>:5555
Then I can load my custom application using
adb install <apk file>
When I did this I needed to dismiss a warning that popped up on android, but the application installed fine.
Once the application has been installed, disconnect adb using
adb disconnect
Both before connecting adb and after it is worth checking that an existing adb server instance is not already running, it can be killed with
adb kill-server
Now to figure out a useful app to develop.
Showing posts with label android. Show all posts
Showing posts with label android. Show all posts
Wednesday, 21 August 2013
Wednesday, 7 August 2013
Internals of Android applications .apk files
Ever since I saw an article on creating a basic android application I have been curious about how some android applications have been created and since I have a working android installation it is time to take a look at how they work.
My android install is on a qcow2 disk image so I can mount it on my system via ndb, just like I did when building my LFS system.
I copied off an apk file to test.
The apk file is effectively just a zip file, so I could just unzip it to get the files, however when I do this most of the files are in a binary format (serialized objects?) and unreadable. Since the discussion a few months back on android bug 8219321 I was aware of the apktool utility to unpack an apk file.
So used apktool to unpack the apk file, like so
apktool d <apkfile> <dir to unpack to>
I now have manifest xml files which are readable and code. I was expecting to get java sourcecode files from this process, but all the files were smali (Dalvik byte code). This does not look too hard to understand but I wanted something I was a little more familiar with, i.e. java.
Therefore I needed to extract files from within the classes.dex file (Dalvik executables) and convert them to java classes.
So I just extracted the classes.dex file from the original apk file using unzip (jar would work as well if a jdk was installed).
I now need to convert the Dalvik executables to java class files, to do this I used the utility dex2jar e.g.
d2j-dex2jar.sh classes.dex
This has converted the dex file into a jar file, again I could use the jar utility to extract the class files but as it is basically a zip file I will use unzip.
I now have a bunch of java class files which I need to disassemble and look at. In the past I have not had much luck with java dissassemblers, however, I have seen one recommended called jd-gui.
This is a 32bit application and as I am running a 64bit OS I needed to install the 32bit libXxf86vm runtime library.
My android install is on a qcow2 disk image so I can mount it on my system via ndb, just like I did when building my LFS system.
I copied off an apk file to test.
The apk file is effectively just a zip file, so I could just unzip it to get the files, however when I do this most of the files are in a binary format (serialized objects?) and unreadable. Since the discussion a few months back on android bug 8219321 I was aware of the apktool utility to unpack an apk file.
So used apktool to unpack the apk file, like so
apktool d <apkfile> <dir to unpack to>
I now have manifest xml files which are readable and code. I was expecting to get java sourcecode files from this process, but all the files were smali (Dalvik byte code). This does not look too hard to understand but I wanted something I was a little more familiar with, i.e. java.
Therefore I needed to extract files from within the classes.dex file (Dalvik executables) and convert them to java classes.
So I just extracted the classes.dex file from the original apk file using unzip (jar would work as well if a jdk was installed).
I now need to convert the Dalvik executables to java class files, to do this I used the utility dex2jar e.g.
d2j-dex2jar.sh classes.dex
This has converted the dex file into a jar file, again I could use the jar utility to extract the class files but as it is basically a zip file I will use unzip.
I now have a bunch of java class files which I need to disassemble and look at. In the past I have not had much luck with java dissassemblers, however, I have seen one recommended called jd-gui.
This is a 32bit application and as I am running a 64bit OS I needed to install the 32bit libXxf86vm runtime library.
Wednesday, 31 July 2013
Running Android in KVM... because I can
I have been toying with the idea of playing around with android and took a quick look at downloading the sourcecode. I decided to hold off as it seemed like a lot of time would need to be spent on this and I had a few other projects I wanted to look at first.
However I found the android x86 port and so I decided to get this running in KVM.
Why?
Because I can.
I downloaded the iso for version 4.2 (I had a few problems with 4.3, however this might be solved in the same way I solved booting 4.2).
I created a new qcow2 qemu image (the original instructions I was looking at suggested using this just to store data and emulate an sd card to save data as the iso is also a live image), however I decided to install it to this disk image.
Using qemu-kvm I booted the iso image and attached the qemu disk I had created. The live image did not seem to boot up at all. If I selected the debug option, this just dropped me into a command prompt. Therefore I decided to install the os to the qemu disk image using the option on the boot menu of the live image.
I also used virt-install to import this disk image which would make it easier to work with.
It still failed to boot correctly, so I checked the VM settings and checked what hardware had been assigned to the VM. I gave it a 32bit cpu as it had defaulted to the same cpu as my hypervisor and because it seemed to be complaining about some graphic functions I switched the video card type from Cirrus to VMVGA (VMWare). With these settings it booted up fine and I got the familiar android home screen.
Sound also seemed not to work so I switched over to using the SPICE protocol instead of VNC, which gave me sound but was very choppy (probably due to running in an emulated environment on my laptop which is not too powerful).
I installed a few apps to prove that this was functioning and challenged my wife to a game of Word Fued.
I have installed version 4.3-test over the top and it seems to work, although Google Play Services periodically crashed during setup.
As this is not on a phone/tablet rotation can be a bit of a headache (I noticed this with WordFued it wanted to start in landscape), therefore I downloaded a rotation locker app to keep the display in potrait and I also turned of auto rotation in android.
However I found the android x86 port and so I decided to get this running in KVM.
Why?
Because I can.
I downloaded the iso for version 4.2 (I had a few problems with 4.3, however this might be solved in the same way I solved booting 4.2).
I created a new qcow2 qemu image (the original instructions I was looking at suggested using this just to store data and emulate an sd card to save data as the iso is also a live image), however I decided to install it to this disk image.
Using qemu-kvm I booted the iso image and attached the qemu disk I had created. The live image did not seem to boot up at all. If I selected the debug option, this just dropped me into a command prompt. Therefore I decided to install the os to the qemu disk image using the option on the boot menu of the live image.
I also used virt-install to import this disk image which would make it easier to work with.
It still failed to boot correctly, so I checked the VM settings and checked what hardware had been assigned to the VM. I gave it a 32bit cpu as it had defaulted to the same cpu as my hypervisor and because it seemed to be complaining about some graphic functions I switched the video card type from Cirrus to VMVGA (VMWare). With these settings it booted up fine and I got the familiar android home screen.
Sound also seemed not to work so I switched over to using the SPICE protocol instead of VNC, which gave me sound but was very choppy (probably due to running in an emulated environment on my laptop which is not too powerful).
I installed a few apps to prove that this was functioning and challenged my wife to a game of Word Fued.
![]() |
| grub boot screen |
![]() |
| lock screen |
![]() |
| home screen |
I have installed version 4.3-test over the top and it seems to work, although Google Play Services periodically crashed during setup.
As this is not on a phone/tablet rotation can be a bit of a headache (I noticed this with WordFued it wanted to start in landscape), therefore I downloaded a rotation locker app to keep the display in potrait and I also turned of auto rotation in android.


