Obtaining Build Target in android development - android

I want to obtain build target in run time and show it on the app. As I know build target is shown when you right click properties and select the android tab. But how can I get it in the code and show in run time on the activity?
Thanx.

You can read out your own ApplicationInfo:
PackageManager pm = context.getPackageManager();
ApplicationInfo info = pm.getApplicationInfo(
context.getPackageName(), PackageManager.GET_META_DATA );
Log.i( "Info", "Target SDK is: " + info.targetSdkVersion );

use Build.VERSION.SDK_INT, it returns a version that app was built on

Related

Android PackageManager: check if a library APK has been installed

The PackageManager.getPackageInfo(packageName, flags) method can be used to check whether a specific package has been installed. It either returns the PackageInfo or throws PackageManager.NameNotFoundException.
How to check if a library APK is installed? An example of the library APK is Trichrome Library: https://www.apkmirror.com/apk/google-inc/trichrome-library/trichrome-library-98-0-4758-101-release/trichrome-library-98-0-4758-101-3-android-apk-download/download/
After installation of this APK, calling PackageManager.getPackageInfo('com.google.android.trichromelibrary', 0) throws PackageManager.NameNotFoundException.
Looking via ADB, I see that once installed, it's not visible under pm list packages but it's visible under pm list libraries with the name "library:com.google.android.trichromelibrary".
Is there any way to determine programmatically whether the library has been installed?
As you can see in the source code of pm in this link, pm list libraries command uses PackageManager.getSystemSharedLibraryNames() which is documented here.
If this method is not working, there are also other methods in PackageManager to get info about shared libraries. As mentioned by #vmayorow, One of these methods is PackageManager.getSharedLibraries(0).

PackageManager.getInstalledApplications(0); in Android 7.0

My problem is that I use package manager to list all the installed applications
final PackageManager pm = parentActivity.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
With this code I can list successfully all the applications in other versions of android except in Android 7.0 (which only list the app that I'm using), can anyone knows why this is happening and how to solve it?
Try the below code. It is working fine for me:
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
for (ApplicationInfo applicationInfo : packages) {
Log.d("APP_INFO", "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);
}
Tested on Android 6, 7 and 8
Hope this will help!

Android - how to get installed package in JNI C code

I have a requirement to implement an equivalent of following java code in JNI C on android:
PackageManager pm = context.getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
for(PackageInfo packageInfo : packages) {
...
}
My question is how to get installed packages in JNI C. Is it even possible?
Thanks
Your best bet is to call PacakgeManager via JNI. There is no "C interface", you have to go through the system service. Installing a package does quite a few things, so just copying the APK to the right place is not enough.

.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.

How to get the file *.apk location in Android device

Need some help by retrieving *.apk file name from Android device programmaticaly ? Could any body provide some Android methods doing that or even shell commands run under Android to get the file location , if I know just a part from file name ?
Also if there is a way how to find out where the *.apk file went after downloading from Market or outside Market ?
Thank you in advance.
List<ApplicationInfo> PackageManager.getInstalledApplications() will give you a list of the installed applications, and ApplicationInfo.sourceDir is the path to the .apk file.
PackageManager pm = getPackageManager();
for (ApplicationInfo app : pm.getInstalledApplications(0)) {
Log.d("PackageList", "package: " + app.packageName + ", sourceDir: " + app.sourceDir);
}
Outputs something like this:
package: com.tmobile.themechooser, sourceDir: /system/app/ThemeChooser.apk
package: com.tmobile.thememanager, sourceDir: /system/app/ThemeManager.apk
package: com.touchtype.swiftkey, sourceDir: /data/app/com.touchtype.swiftkey-1.apk
package: com.twitter.android, sourceDir: /data/app/com.twitter.android-2.apk
package: fm.last.android, sourceDir: /data/app/fm.last.android-1.apk
$ adb shell pm list packages -f
This gave me path in form /data/app/xxx.apk:
https://developer.android.com/reference/android/content/Context.html#getPackageResourcePath%28%29
You can also do adb bugreport and look at the output. You'll want to look for <package name="com.app.package" codePath="path/to/the/app.apk"...
The apps that you download go to /data/app so using ls you can get a list of the downloaded apks.

Categories

Resources