Get installing package name from package installer activity - android

Is it possible to get package name of the installing application before app installed. i.e., While showing installer activity itself can we find?
Thanks.

We can get package name of application from .apk file using its filepath.
PackageInfo packageInfo = getPackageManager().getPackageArchiveInfo(filepath,PackageManager.GET_ACTIVITIES);
if(packageInfo != null) {
ApplicationInfo appInfo = packageInfo.applicationInfo;
String package_name= appInfo.packageName;
}
where filepath is a String containing filepath of the apk e.g. "/sdcard/xyz.apk".
Make sure you add READ_EXTERNAL_STORAGE uses permission in AndroidManifest.xml
I hope this helps.

Related

How can you launch Termux through another android app?

I am working on automating the process of launching the dependencies for an android project.
One of the dependencies is launching Termux (Installed through F-Droid not Play store as recommended).
I am trying to launch the installed Termux application through another application and add some commands to its ~./bashrc file for the sake of automation.
I know that an installed app can be launched trough another android app (more details are here).
I wonder to know if this is possible for Termux as well? I wonder to know if we can use intent concept to launch Termux from an android app as well? If yes what is the Termux package name?
I tried using "com.termux" as its packagename in my sample code, but it did not work.
In other words, the following line returns null:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.termux");
Updated:
I can open another installed app (the app that I developed and installed its apk file on tablet) using Intent concept as shown above (by replacing the appropriate package name instead of termux package name).
Note:
I have installed the Termux through F-Drioid not google play store.
New observation:
I confirmed through Package names application the Termux package name is "com.termux" and its activity class name is also "com.termux.app.termuxActivity"
But seems like the "com.termux" is not accessible through package manager. When i try to pass "com.termux" to the following function it returns false.
Any idea or suggestion?
public boolean isPackageExisted(String targetPackage){
enter code here
PackageManager pm=getPackageManager();
try {
PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
Add com.termux to queries element or declare QUERY_ALL_PACKAGES permission in AndroidManifest.xml if targetSdkVersion is 30+. Check Package Visibility or this article for more info. Otherwise, you will will get PackageSetting{...... com.termux/......} BLOCKED errors in logcat.
<manifest
<queries>
<package android:name="com.termux" />
</queries>
</manifest>
Moreover, you can run commands in termux via RUN_COMMAND intent. The termux-shared lib is published on jitpack since v0.116, check Termux Libraries for import instructions.
Moreover, activity name is com.termux.app.TermuxActivity.

get package name of apk file from path of application

I have one non activity class, in this class I have several apk file path like /system/priv-app/TelephonyProvider/TelephonyProvider.apk, Now from this file path I have to retrieve package name of apk files.
Any suggestion will be appreciated. Thanks
Ok I got the answer:
PackageManager pm = mContext.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(path, 0);
String pkgName = info.packageName
Thanks all, Actually after cleaning project it works

Compare downloaded apk file version with installed one version programmaticaly

I wanted to know that is it possible to find version of an apk file that i've downloaded from the server?
i can retrieve the version of my app that is installed on device but i want to check installed app version on device with the downloaded apk file version.
You cannot easily get the version of the APK file you downloaded until you have installed it.
That being said, you can unzip the APK file (its just a zip file) and parse the AndroidManifest.xml to get the value of the version code.
Since the AndroidManifest.xml you extract is a binary file, you will need to write some code to parse it. It's not as simple as inspecting a plain text file.
Check out this post on how to parse the AndroidManifest which has been extracted from an APK.
How to parse the AndroidManifest.xml file inside an .apk package
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo("com.whatsapp", 0);
String packageName = info.packageName;
int versionCode = info.versionCode;
String versionName = info.versionName;
Replace com.whatsapp with your package name.

.apk filename changes after doing adb install

If I do:
adb install myAppRelease-2012-07-24_14-35-14.apk
When I try to reference the actual .apk file after it is installed
PackageManager pm = this.getPackageManager();
for (ApplicationInfo app : pm.getInstalledApplications(PackageManager.GET_META_DATA)) {
Log.d("PackageList", "package: " + app.packageName + ", sourceDir: " + app.sourceDir);
String appName = "myAppRelease"
if(app.packageName.contains("myApp")){
if(app.sourceDir.contains(appName)){
apkVersion = app.sourceDir.substring(app.sourceDir.indexOf(appName), app.sourceDir.indexOf(apk));
}
}
}
What I see is this:
07-24 14:46:40.190: D/PackageList(7421): package: myApp, sourceDir: /data/app/myApp-1.apk
What I expected to see is this:
07-24 14:46:40.190: D/PackageList(7421): package: myApp, sourceDir: /data/app/myAppRelease-2012-07-24_14-35-14.apk
It appears that it uses
android:label="myApp"
android:versionCode="1"
from the manifest file.
My question is, why doesn't it keep the original .apk filename? I am relying on the .apk filename to display version information for my app.
You can't rely on this, and it definitely doesn't use the version code for the number after the '-'. Especially with newer versions, apps can be moved to the SD card, or forward locked (aka, 'app encryption'), so the actual file on disk can be very different from the original file.
Use PackageManager to get version info, that is guaranteed to be correct and up to date. Change your build system to update the version code and maybe display it in an About dialog or similar so it is easy for users to report it.

Reading android MANIFEST.MF file

Is there a way to read META-INF\MANIFEST.MF file of the currently running application using Android API?
I want to read SHA1 for classes.dex file and use it as an encrpytion key to one of my assets.
I cannot use the signature for .apk file because everytime I create a new apk I need to re-encrpyte my asset and put in to apk file which requires re-signing the .apk and it becomes a chicken and egg problem.
I have found a way to do it. I opne the .apk file as a JarFile and then I can access individual files inside .apk. I believe this will only work for the .apk file that is running this code.
Here it is:
// Get Package Name
String packageName = ctx.getPackageName();
// Get classes.dex file signature
ApplicationInfo ai = ctx.getApplicationInfo();
String source = ai.sourceDir;
JarFile jar = new JarFile(source);
Manifest mf = jar.getManifest();
Map<String, Attributes> map = mf.getEntries();
Attributes a = map.get("classes.dex");
String sha1 = (String)a.getValue("SHA1-Digest");
Use following way to detect External Jar/SDK MANIFEST.MF Info. We could use this info for detecting Jar version etc. Use http://docs.oracle.com/javase/6/docs/api/java/util/jar/Manifest.html
public void getSDKInfo() {
Package package = Manifest.class.getPackage();
String specTitle = package.getSpecificationTitle();
String vendor = package.getSpecificationVendor();
String virsion = package.getSpecificationVersion();
}

Categories

Resources