What is an apk?
Android application package file (APK) is the file format used to distribute and install application software and middleware onto Google's Android operating system. To make an APK file, a program for Android is first compiled, and then all of its parts are packaged into one file. This holds all of that program's code (such as .dex files), resources, assets, certificates, and manifest file. As is the case with many file formats, APK files can have any name needed, but must end with the four character, three letter extension, .apk.
Building and Running
During the build process, your Android projects are compiled and packaged into an .apk file, the container for your application binary. It contains all of the information necessary to run your application on a device or emulator, such as compiled
.dex files (.class files converted to Dalvik byte code), a binary version of the AndroidManifest.xml file, compiled resources (resources.arsc) and uncompiled resource files for your application.
If you are developing in Eclipse, the ADT plugin incrementally builds your project as you make changes to the source code. Eclipse outputs an
.apk file automatically to the bin folder of the project, so you do not have to do anything extra to generate the .apk.
If you are developing in a non-Eclipse environment, you can build your project with the generated
build.xml Ant file that is in the project directory. The Ant file calls targets that automatically call the build tools for you.
To run an application on an emulator or device, the application must be signed using debug or release mode. You typically want to sign your application in debug mode when you develop and test your application, because the build tools use a debug key with a known password so you do not have to enter it every time you build. When you are ready to release the application to Google Play, you must sign the application in release mode, using your own private key.
The following diagram depicts the components involved in building and running an application:
A Detailed Look at the Build Process
The build process involves many tools and processes that generate intermediate files on the way to producing an
.apk. If you are developing in Eclipse, the complete build process is automatically done periodically as you develop and save your code changes. If you are using other IDEs, this build process is done every time you run the generated Ant build script for your project. It is useful, however, to understand what is happening under the hood since much of the tools and processes are masked from you. The following diagram depicts the different tools and processes that are involved in a build:
The general process for a typical build is outlined below:
- The Android Asset Packaging Tool (aapt) takes your application resource files, such as the AndroidManifest.xml file and the XML files for your Activities, and compiles them. An R.java is also produced so you can reference your resources from your Java code.
- The aidl tool converts any .aidl interfaces that you have into Java interfaces.
- All of your Java code, including the R.java and .aidl files, are compiled by the Java compiler and .class files are output.
- The dex tool converts the .class files to Dalvik byte code. Any 3rd party libraries and .class files that you have included in your project are also converted into .dex files so that they can be packaged into the final .apk file.
- All non-compiled resources (such as images), compiled resources, and the .dex files are sent to the apkbuilder tool to be packaged into an .apk file.
- Once the .apk is built, it must be signed with either a debug or release key before it can be installed to a device.
- Finally, the application is being signed in release mode.
Basic structure
Since an APK file is an archive that once unzipped usually contains the following folders structure:
Here’s a breakdown on what’s what:
META-INF directory:
- MANIFEST.MF: the Manifest file
- CERT.RSA: The certificate of the application.
- Contains the certificate of the public key for you to verify the signature.
- CERT.SF: It is the signature file it contains the list of resources and SHA-1 digest;
Resource folder
Contains following category of resources:
- /drawable: Here it stores all the graphic files as well as android xml based styles. By default, there are several versions of folders within this such as: drawable-mdpi, drawable-hdpi, drawable-ldpi. This is because there are many Android devices with different screen resolutions, hence in order to adapt to different screen resolutions these kind of folder structure is used.
- /layout: This folder contains the UI layouts that is used in the project. These UI layouts are stored as XML files.
resources.arsc :
It contains the compressed resources file.
AndroidManifest.xml:
Describes the name, version, access rights, referenced library files for the application.
classes.dex
It is a compressed file which contains the java and classes compiled.
How to build Android application package (.apk) from the command line using the SDK tools
Step 1: Generate Resource java code and packaged Resources
aapt package -f -M ${manifest.file} -F ${packaged.resource.file} -I ${path.to.android-jar.library} -S ${android-resource-directory} [-m -J ${folder.to.output.the.R.java}]
Step 2: Compile java source codes + R.java
use javac
Step 3: Convert classes to Dalvik bytecodes
use dx.bat
dx.bat –dex –output=${output.dex.file} ${compiled.classes.directory} ${jar files..}
Step 4: Create unsigned APK
use apkbuilder
apkbuilder ${output.apk.file} -u -z ${packagedresource.file} -f ${dex.file}
or
apkbuilder ${output.apk.file} -u -z ${packagedresource.file} -f ${dex.file} -rf ${source.dir} -rj ${libraries.dir}
-rf = resources required for compiled source files?
-rj = resources required for jar files
Step 5: Generate a key
use keytool
Step 6: Sign APK
use jarsigner
jarsigner -keystore ${keystore} -storepass ${keystore.password} -keypass ${keypass} -signedjar ${signed.apkfile} ${unsigned.apkfile} ${keyalias}
Step 7: Publish
use adb
adb -d install -r ${signed.apk}
Inspecting your APK file:
aapt list -v latest.apk
Case Study on Native Android SDK
Following is the package detail for an Android native based project, where the files used at the development stage is placed mostly stored in src for java files and layouts are designed and stored in res/layout.
Once the apk build is generated which can be easily located in bin folder of the project. We can get the apk extracted which will have the folder structure as mentioned in below diagram. All the source class files are zipped separately and kept in classes.dex file
Case Study on Convertigo MEAP
Using Convertigo MEAP following is the folder structure followed at the time of development, the files used at the development stage is placed mostly in DisplayObjects
Once the build is generated and apk is generated we get the below as its folder structure, where the
All the code used at time of development can be seen located in assets->www folder
Interaction between HTML, Javascript to Android native code.
This is done with help of WebView.
A WebView that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.
In case for building app natively we need to place these html files and its contents inside /assets/www folder
1 comment:
Any way to create android App for blog without Knowledge of coding ?
Post a Comment