Friday, May 2, 2014

Android Package Name From APK Code

apk-icon
Android apps are supplied in .apk files(even if you download from Play store) which contain all information needed to install an app to an Android smart phone.
Once an app is installed on a phone it is identified by a unique package name, the package name format is:
com.usra.ca
com.honestitconsultancy.patris
isymphony2_1.pminternational.com

This value is taken from the Manifest.xml file which is already present inside the .apk file.
If we search "get package name from .apk" on Google we won't be able to find the source code to do this on the first page, almost everyone tries to tell us that this will be done through command line.

Package Archive files

There is a little twist behind these search results, is there another name for .apk files?
Yes sir, the full name of .apk files is "Package Archive". You can say that's the legal name .apk is only a nick name used by the Linux OS (Android is a Linux) and developer community.



It is possible to retrieve various nuggets of information Android package archive or .apk files using Android PackageManager class.
The android.content.pm.PackageManager will return the information about your package (or .apk) in a android.content.pm.PackageInfo object.










Java Source Code
The Java language source code to get the package name from an .apk file is given below.

public String getPackageNameByAPK(String _strAPKPath, Context _activityORservice){
 String strRetVal = "";
 PackageManager packMan = null;
 PackageInfo packInfo = null;

  try{

  if(_strAPKPath == null) return "";

  Log.d(K.ATAG, "getPackageNameByAPK(): " + _strAPKPath);
  if(_activityORservice == null) {
   Log.e(K.ATAG, "Context is null");
    return "";   
  }

 packMan = _activityORservice.getPackageManager();  
 packInfo = packMan.getPackageArchiveInfo(_strAPKPath, 0);
 strRetVal = packInfo.packageName;
   
 }catch(Exception e){
  Log.e(K.ATAG, e.toString() + "" );
 }

  Log.d(K.ATAG, "RetVal: " + strRetVal);
 return strRetVal;
}//Beautify code with tohtml.com/java


Note, this is actually WORKING CODE, copied straight from one of my already deployed apps.
All you need is some common sense and a mug of coffee before performing the magical copy/paste operation.

An instance of the Package Manager can be acquired by calling getPackageManager() method of android.content.Context class, which actually represents the activity or service from which you are making the method call.

The details about PackageManager and the method getPackageArchiveInfo can be found on the URL given below:
http://developer.android.com/reference/android/content/pm/PackageManager.html

I'm working as a freelance programmer through ODesk.com, my profile clearly reflects what I am made of. Feel free to ping me if you need an Android app for yourself or your employer.
https://www.odesk.com/users/~012d73aa92fad47188

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app.

https://www.odesk.com/users/~012d73aa92fad47188


5 comments:

  1. Did you ever think is it possible to edit an APK file or change the way it works?
    http://goo.gl/rv6JRT

    ReplyDelete
  2. Abdul Sattar, yes it is possible to "unpack" an APK and extract its code, modify it and re-package it again.

    Check this link:
    https://code.google.com/p/smali/

    The changes are done in smali language which is really complicated, it is made so on purpose to make reverse engineering difficult. I tried to do it on behalf of a freelance client once, but we didn't make much progress so the project was ended after a week or so.

    ReplyDelete
  3. Hy author,
    I am founder of game killer
    I like your article THANKS FOR SHARING

    ReplyDelete

Feel free to talk back...