Isn't there an official format specification of .apk? - android

I have seen some applications that reverse .apk files into elementary files. I would like to know where can I find the official specification of .apk files, but I reached nowhere! Could someone enlighten us about where to look, and what are the best resources about the specification if there is not a formal specification?

I don't know if there's an official specification, but from browsing my app's file structure, I have deduced the following:
-The .apk is simply a zip file and can be opened as such in any suitable application (7zip, WinZip, WinRAR, any OS's default .zip tool).
-It must contain an AndroidManifest.xml file in the root directory (though this is a compiled .xml file and not human-readable).
-It must contain a classes.dex file containing the actual binary code compiled as Dalvik bytecode.
-It must contain a resources.arsc file. I believe it's an index of everything in the /res folder, but I'm not positive.
-It must contain a META-INF folder in the root containing CERT.RSA, CERT.SF, and MANIFEST.MF. These are the app's digital signature files.
-The app may contain the folders /assets, /res, and /raw that contain resource files like images and databases.
-The app may contain a /lib folder that contains folders for all the native code targets (like armeabi), which contain .so files (native code).
I probably missed a ton of things, but this should give you a good start.

Related

Error The file name must end with .xml, when placing binary files in asset folder

From what I have read, binary files should be put in assets folder in order to be able to access them correctly from the native part of my application. Yet when I put a binary, android studio complains that the file should should be an XML folder. From what I read, putting the binary in raw folder is not a good idea, Since the process of reading raw files in native code is not straight forward nor forward compatible since open opens a file stream and seeks forward some offset. If android system designs later on decide to unpack raw files outside the APK the code will not work any more. I there a way I can suppress the XML error that results when I put the binary in the assets folder.
The assets/ directory should be a peer of res/ and java/, not inside of res/. Otherwise, the build tools will think that it is a resource directory and may expect its contents to be limited to XML files.

Using resource files in NDK

I have an NDK library that I am creating that needs to contain and access a binary data file (.dat extension). I am having trouble getting the compiled library to see this file. To make things a little more difficult, I am doing this within a library package.
I think it would work if, during my Android.mk file, I copy this .dat file to my app's resources folder, and then access that from within the app, but I feel like there must be a better way.
Any suggestions?
Instead of resources, put it in the assets folder; NDK provides API to access assets from native code.
Often, we unpack some "files" from the resources or assets to the file system (e.g. /sdcard) on the first run of the app after install. This approach works best when the files must be used by external apps and libs (e.g. to play sounds), or when these files will be changing.
Finally, you can link the data into your .so during ndk-build. This will resolve the question how the .dat file will be copied into the app folder, but reading it may be tricky, and modifying - impossible. You don't need to create a huge library. You can create a mock-up library that contains the data. If I understand correctly, you can ignore the file structure, headers, etc. You only need a file named lib something .so in your libs/armeabi (or other) folder.

Is .apk an executable or a package?

I want to know that a .apk file of android is an executable or it is just a package that contains the compiled code, resources, manifest.xml etc.
It's like Zip format and it's just a Package :
Wikipedia : Android application package file (APK) is the file format used to distribute and install application software and middleware onto Google's Android operating system; very similar to an MSI package in Windows or a Deb package in Debian-based operating systems like Ubuntu. To make an APK file, a program for Android is first compiled, and then all of its parts are packaged into one file. An APK file contains 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, provided that the file name ends in ".apk".1[2][3][4]
APK files are ZIP file formatted packages based on the JAR file format, with .apk file extensions. The MIME type associated with APK files is application/vnd.android.package-archive.
Another Useful Link
It's a bit of a hybrid between the two. Code isn't executed directly out of it (the classes are copied into a separate file and optimized for your system prior to execution), but stuff like the application manifest and resources are loaded directly from it when they're needed.
A general explanation can be found here. i also found a similar thread in SO (can be found here). On AndroidBook, there's an interesting thread too: In brief, it's a package :)
APK files are Android Package and a variant of the JAR file format, which are built on the
ZIP file format, with a .apk file extension.

Is there a difference between typical a ZIP file and an APK file?

I know a typical APK file would have AndroidManifest.xml but that's not the aspect I ask for. I'm asking in terms of ZIP structure and headers, i.e. at a lower level perspective.
Any APK file is a valid ZIP file. There's more to it than that - files that must be present, the fact that zipalign is normally used to align data structures within the file - but it's all valid ZIP.
AFAIK, it is a completely standard ZIP file. I have had no problems working with an APK as a ZIP file using any tool I have tried.
Note that, as with regular ZIP files, not all entries will be compressed (varies by file type).

Role of classes.dex file in an apk file [duplicate]

This question already has answers here:
What are .dex files in Android?
(3 answers)
Closed 7 months ago.
When opening an APK file with WinRar (software to open compressed files). I got a bunch of files packed inside the APK. Among them classes.dex is one. My question is what is the role of this file and if we modify/delete the same file will it affect the APK?
.dex file
Compiled Android application code file.
From Android API GUIDES
Android programs are compiled into .dex (Dalvik Executable) files, which are in turn zipped into a single .apk file on the device. .dex files can be created by automatically translating compiled applications written in the Java programming language.
And yes if you will delete those files it will effect APK.
classes.dex is essentially all of the application logic. Code of the given application is written in java and then compiled to class files, then these class files are cross compiled (with many optimisations) to dalvik VM format. Note that there also might be some .so files which are also application code but these are generated when NDK is used.
You can not delete this file. You could however change it by first running this utility https://github.com/JesusFreke/smali which will generate smali code from this compiled dex which is somewhat similar to java and could be understood. You could also use tools ApkOneClick or ApkMultiTool to get Java source from the smali files but these would probably not be perfect and will require further fixing. When you change the code you want you should build the classes.dex again and put them into existing zip/apk file you have. Note that then existing certificate files (META-INF) will not be valid anymore and you will need to delete this folder and resign the apk package in order to instal it on the phone or emulator.
For more info you could check this question too What are .dex files in Android?
Also this is a great tutorial on disassembling dex files using existing tools http://blog.vogella.com/2011/02/14/disassemble-android-dex
What is the role of this file?
The role of classes.dex in Android is similar to that of JAR files in plain Java. It's a file containing bytecodes. In Android case, the bytecode is Dalvik bytecode, which is different from Java bytecode.
If we modify/delete the same file will it effect the apk?
If you modify classes.dex, you are modifying the programs behavior, which may or may not work after a repackage. If you delete classes.dex, then your application doesn't have code and you shouldn't expect it to work.
.dex file in the apk is the compress file which is made up of all the java classes in the application code. Its different than jar file. A jar file is a collection of .class files which are isolated. If we unzip .jar, we get all the classes separately. On the other side, .dex file is a single file made up with all .class file from application code.
Code compilation flow :
multiple .java files --> multiple .classes files --> a single .dex file
.dex files are the executables which are executed by the DVM...Dalvik Virtual Machine, which is a Runtime for Android.
.dex will never include resources. Resources are separately maintained in the /res folder in .apk

Categories

Resources