Writing an API with Rust to parse and extract info from Apk

Writing an API with Rust to parse and extract info from Apk

Jul 24, 2022

As I said in the last post, let's write an API and extract information from Apk. I'm going to write It with Rust Programming Language and Rocket framework with some packages which are explained further in this post. the code Is uploaded to Github and everybody can fork/clone and use It or add futures to It.

This is the information we want to extract:

Our API has just one endpoint to POST an Apk, so let's write the main function and the upload endpoint:

Now after POST an Apk to the API, we call a function parser::parse(&path) and pass the Path of the uploaded file, and the base of our app going to parse It and return a Option that tells us If the operation succeed or not. let's look at parse the function:

As I earlier said, the Apk files are Archives then we can open them with libraries to work with ZIP files. we use zip crate to access archive files and extract contents of AndroidManifest.xml. after finding it, we have to decode binary XML So I use the axml crate which helps us in this purpose.

As we have the String content, let's explore the tags of the XML and get the information we want from It. for that I'm going to use xml-rs crate. There are three Important tags to extract the values we want, Let's look at the code:

So the tags we are interested in are:

  • manifest that includes:
* PackageName
* VersionCode
* VersionName
* CompileSdkVersion 
* CompileSdkVersionCodename 
  • uses-sdk that includes:
* minSdkVersion
* targetSdkVersion
  • uses-permission That uses for permissions of the application.

After getting this information we return the ApkInfo struct and convert It to JSON using serde-json crate and serve It to the request as a response.

Now let's test our API, I'm going to use curl to request:

And the result Is:

That's It, we're done.

Github: Apk-Parser

Thanks for reading this Article, Good Luck.

Benyamin Eskandari

Curious, Experienced Android Developer Also in love with Rust, DevOps, and System Programming Trying to read a lot of books and learn a lot of things, I will write posts about my path on this journey.