Friday, May 2, 2014

Android Package Name From APK Java Code Sample

apk-icon
public String getPackageNameByAPK(String _strAPKPath, Context _activityORservice)

Java Source Code
The Java language source code with android.content.pm.PackageManager to get the package name from a .apk file is shown in this post for online learning purposes.

Android apps are supplied in .apk files(even if you download from the Play store) which contain all information needed to install an app on an Android smartphone.
Once an app is installed on the phone it is identified by a unique package name; the package name format is:

  1. com.usrax.ca
  2. com.honestitconsultancy.patric
  3. mysymphony.pmi.com


This value is taken from the Manifest.xml file which is already present inside the .apk file of an Android application(app).

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 the command line.

This post will answer the following question:
Android, get the package name from apk file in your program

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 nickname 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 the Android PackageManager class programmatically in Java.

The class android.content.pm.PackageManager will return the information about your package (or .apk) in a android.content.pm.PackageInfo object.










Java Source Code Example

The Java programming language source code to get the package name from a .apk file is given below.
I have created a convenient method for you to easily utilize in your Java app code.


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 that this is actually WORKING CODE, copied straight from one of my 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




87 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
  4. This is really a great Post. Thank you for sharing this very nice post awesome keep sharing.
    Z4Root Rooting Software

    ReplyDelete
  5. Great Sharing I Like your article.Its too much good.
    I Suggest you Happy Fathers Day Wishes/Greedings 2018 Please Review and Give FeedBack

    ReplyDelete
  6. The out call and in call services of the Vadodara Escorts Services are absolutely popular among their clients and each one of them is said to be absolutely professional in this matter.
    Vadodara Escorts Services

    ReplyDelete
  7. get apk
    GetAPK Market Download Free For Android, Play Store alternative to download free apps using GetAPK Market. Now download unlimited apps for free.

    ReplyDelete
  8. Its giving a good info about lottery and Toursim place in karanataka
    http://www.keralalotteryresults.live/
    http://www.arsikere.live/
    http://sattakingmatka.org/
    www.keralalotteryresults.live
    www.arsikere.live
    www.sattakingmatka.org

    ReplyDelete
  9. Its giving a good info about lottery and Toursim place in karanataka
    http://www.keralalotteryresults.live/
    http://www.arsikere.live/
    http://sattakingmatka.org/
    www.keralalotteryresults.live
    www.arsikere.live
    www.sattakingmatka.org

    ReplyDelete
  10. Very nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again very soon!
    please check this link

    ReplyDelete
  11. To get nice apps and games, download Blackmart as it has got some of the best apps in every category.

    ReplyDelete
  12. Hi to all, the contents present at this website are truly awesome for people experience, well, keep up the good work fellows.VTU Notes

    ReplyDelete
  13. best one I always do it for my knowledge

    ReplyDelete
  14. Packing manager of android is a great tool for android users. You can similarly use this website for android apps.
    unlockmytv apk

    ReplyDelete
  15. There are blogs as good as this and no block can be trusted other than this marketing digital

    ReplyDelete
  16. all the blogs that I have read this time I see a blog as good as this slot gacor

    ReplyDelete
  17. From the blogs that I have read, only this blog adds to my knowledge the most distributor pulsa

    ReplyDelete
  18. Nice blog !! the issuue that you touch is very informative and great. Keep it up. we are waiting for more apk reviews.
    Must watch this

    ReplyDelete
  19. Thank you very much for upadate very informative blog. Hope you upload more these kin of blogs. Thanks.
    Visit here

    ReplyDelete
  20. the website design is very good and the information on this website looks also good

    Castle Clash MOD APK
    Castle Crush MOD APK

    ReplyDelete
  21. A great degree energizing article thanks for sharing it to each one of us. Looking forward to more information and you have continually intrigued me.
    Best Info

    ReplyDelete
  22. This is a great tribute. Thanks for posting it
    cuan777 situs slot pulsa tanpa potongan
    cuan777 agen Pay4D resmi dan terpercaya

    ReplyDelete
  23. Great site! I really love the way it is simple on my eyes. I am thinking about how I may be told at whatever point another post has been made.
    click Here

    ReplyDelete
  24. one of the many blogs that are very good and understandable and highly recommended pulsa murah

    ReplyDelete
  25. This blog is very complete and very high quality and very easy to understand Paket CCTV

    ReplyDelete
  26. Situs judi dan slot online terbaik dan terpercaya
    <a href="https://163.47.11.90/%22%3EMonas77</a>

    ReplyDelete
  27. I really liked reading your post! Very high quality content and useful information similar to my post on places to visit to With such a valuable website I believe you deserve to be ranking even higher in the search engines.

    click Here

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. เว็บนาซ่า The best online football betting Click Now

    ReplyDelete
  30. maria168 The number 1 best website to make Money.

    ReplyDelete
  31. maria website online the best in the worlds.

    ReplyDelete
  32. wwbet Online lottery website that gives full price best price in Thailand need this website only.

    ReplyDelete
  33. I am thankful to you for sharing this plethora of useful information. I found this resource utmost beneficial for me. Thanks a lot for hard work.เปิดยูสคาสิโนพร้อมโปร

    ReplyDelete
  34. เดิมพันที่เว็บ autobet กับสถานที่ไหนบ้าง การเข้ามาใช้บริการสำหรับเว็บเพื่อการเดิมพัน ถือว่าเป็นช่องทางสำหรับการเดิมพันที่ดีมากเลยทีเดียวที่ autobet

    ReplyDelete
  35. Many people wonder how to edit an apk file. This is a guide on how to do it. There are several ways to edit an apk file. This article will show you the easiest way to do it. You can use a text editor such as Notepad to edit an apk file.

    ReplyDelete

Feel free to talk back...