Obtaining version of youtube android app - android

I guys,
is there a method to find what version of the youtube player installed in my phone thru java code?
Thanks.

Try using:
String versionName = context.getPackageManager().getPackageInfo("youtube's package name as String", 0 ).versionName;
int versionCode = context.getPackageManager().getPackageInfo("youtube's package name as String", 0 ).versionCode;
Make sure to wrap this in a try-catch block for the NameNotFoundException incase YouTube isn't installed on the user's device.

Yes, it's possible. With the class PackageInfo (ref: PackageInfo)
// The version number of this package, as specified by the <manifest> tag's
// versionCode attribute. Added in API level 1
public int versionCode
// The version name of this package, as specified by the <manifest> tag's
// versionName attribute. Added in API level 1
public String versionName
code:
PackageInfo pinfo = null;
pinfo = getPackageManager().getPackageInfo("com.google.android.youtube", 0);
int versionNumber = pinfo.versionCode;
String versionName = pinfo.versionName;

Related

Use Gradle Play Publisher's version name override in Java

Gradle Play publisher lets you override the version name before publishing to the play store.
play {
// ...
resolutionStrategy = "auto"
outputProcessor { // this: ApkVariantOutput
versionNameOverride = "$versionNameOverride.$versionCode"
}
}
Is it possible to use the value of versionNameOverride in Java Code? We display the version name in the about page of the app using the versionName attribute. GPP updates the versionNameOverride value so the play store listing shows the correct version number but the app's about page keeps showing a different version number that's based on versionName.
Using the versionNameOverride could be done like this:
outputProcessor { output ->
output.versionNameOverride = output.versionNameOverride + "." + output.versionCode
def versionPropertyFile = file "version.properties"
def versionProperties = new Properties()
versionProperties.setProperty('versionCode', "$output.versionCode")
versionProperties.setProperty('versionName', output.versionNameOverride)
versionPropertyFile.withWriter { versionProperties.store(it, "Generated by the outputProcessor for the play plugin during publishing.") }
}
But if you want to show the versionName in your app it would be easier to use
try {
PackageInfo pInfo =
context.getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
The reason why your app did show the wrong values might be that your app was using BuildConfig. But the versionCode and versionName there do not reflect what has been set through versionNameOverride, but those values from your build gradle.

How to know an app's target sdk version

I'm doing some android analysis and need to find all the android app that have targetSdkversion over 23, right now I'm reverse engineering all the apk packages to see this...Wondering if there's more easy way to get this information given an app name?
Thanks :)
you can do that by this code:
int sdkVersion = 0;
IPackageManager packageManager = AppGlobals.getPackageManager();
try {
ApplicationInfo appInfo = packageManager.getApplicationInfo(yourAppName, 0);
if (applInfo != null) {
sdkVersion = applicationInfo.targetSdkVersion;
}
}

How to get minimum sdk version(minSdkVersion) at runtime?

I wanted to get the minSdkVersion used to build the app at runtime. I tried with the below code.
getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.targetSdkVersion
In the documentation the targetSdkVersion is mentioned with the comment The minimum SDK version this application targets.. But it is providing the current version where the application is running.
Is there any predefined way is available to get the minSdkVersion? (or)
Is there any hack is possible to get the minSdkVersion?
If you want you can also declare it in your gradle like:
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 5
versionName "some name"
applicationId "com.myapp"
buildConfigField "int", "MIN_SDK_VERSION", "$minSdkVersion.apiLevel"
}
then in your code you just need to get it like:
BuildConf.MIN_SDK_VERSION
targetSdkVersion : The minimum SDK version this application targets
int targetSdkVersion= 0;
IPackageManager manager = AppGlobals.getPackageManager();
try {
ApplicationInfo applicationInfo = manager.getApplicationInfo(appName, 0);
if (applicationInfo != null) {
targetSdkVersion= applicationInfo.targetSdkVersion;
}
}
You can get more info https://developer.android.com/reference/android/content/pm/ApplicationInfo.html#targetSdkVersion
#PedroAGSantos's answer is not 100% correct, we need to use apiLevel property of minSdkVersion and we don't need extra quotes. here is the corrected version:
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 5
versionName "some name"
applicationId "com.myapp"
buildConfigField "int", "MIN_SDK_VERSION", "$minSdkVersion.apiLevel"
}
This is how you can get minimum SDK and target SDK version during runtime
PackageManager packageM = getPackageManager();
String packageName = getPackageName();
int targetVersion = 0, minVersion = 0;
PackageInfo info = null;
try {
info = packageM.getPackageInfo(packageName, 0);
ApplicationInfo applicationInfo = info.applicationInfo;
targetVersion = applicationInfo.targetSdkVersion;
minVersion = applicationInfo.minSdkVersion;
......
}
catch (Exception ex)
{
}
Shorted way:
int minimumSDK = getApplicationContext().getApplicationInfo().minSdkVersion;
int targSDK = getApplicationContext().getApplicationInfo().targetSdkVersion;

Is it possible to get other application version?

Is it possible to findout version of another application?
I can find if it is installed
android.content.pm.PackageManager mPm = getPackageManager(); // 1
PackageInfo info = mPm.getPackageInfo(pName, 0); // 2,3
Boolean installed = info != null;
But i miss relative field in packageinfo or applicationinfo
versionCode and versionName are each fields in PackageInfo.

How to write build time stamp into apk

Making some changes in Android Contacts package
Using mm (make) command to build this application
Because I have to change and build this app again and again, so I want to add a build time stamp in the Contacts.apk to check the build time when we runn it in the handset.
As we know, when we run mm command, the Android.mk (makefile) in Contacts package will be called.
And now, we can get the build time using date-macro.
But how we can write this build time stamp into a file that our application can read at runtime?
Any suggestions?
If you use Gradle, you can add buildConfigField with timestamp updated at build time.
android {
defaultConfig {
buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"
}
}
Then read it at runtime.
Date buildDate = new Date(BuildConfig.TIMESTAMP);
Method which checks date of last modification of classes.dex, this means last time when your app's code was built:
try{
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
ZipFile zf = new ZipFile(ai.sourceDir);
ZipEntry ze = zf.getEntry("classes.dex");
long time = ze.getTime();
String s = SimpleDateFormat.getInstance().format(new java.util.Date(time));
zf.close();
}catch(Exception e){
}
Tested, and works fine, even if app is installed on SD card.
Since API version 9 there's:
PackageInfo.lastUpdateTime
The time at which the app was last updated.
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
//TODO use packageInfo.lastUpdateTime
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
On lower API versions you must make build time yourself. For example putting a file into assets folder containing the date. Or using __ DATE__ macro in native code. Or checking date when your classes.dex was built (date of file in your APK).
Edit: My answer does not work anymore since option keepTimestampsInApk was removed.
Working in 2020 is https://stackoverflow.com/a/26372474/6937282 (also https://stackoverflow.com/a/22649533/6937282 for more details)
Original answer:
A hint for solution "last modification time of classes.dex file" an newer AndroidStudio versions:
In default config the timestamp is not written anymore to files in apk file. Timestamp is always "Nov 30 1979".
You can change this behavior by adding this line to file
%userdir%/.gradle/gradle.properties (create if not existing)
android.keepTimestampsInApk = true
See Issue 220039
(Must be in userdir, gradle.properties in project build dir seems not to work)
Install time : packageInfo.lastUpdateTime
build time : zf.getEntry("classes.dex").getTime()
Both are differnet time.
You can check with the code below.
public class BuildInfoActivity extends Activity {
private static final String TAG = BuildInfoActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
PackageManager pm = getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
// install datetime
String appInstallDate = DateUtils.getDate(
"yyyy/MM/dd hh:mm:ss.SSS", packageInfo.lastUpdateTime);
// build datetime
String appBuildDate = DateUtils.getDate("yyyy/MM/dd hh:mm:ss.SSS",
DateUtils.getBuildDate(this));
Log.i(TAG, "appBuildDate = " + appBuildDate);
Log.i(TAG, "appInstallDate = " + appInstallDate);
} catch (Exception e) {
}
}
static class DateUtils {
public static String getDate(String dateFormat) {
Calendar calendar = Calendar.getInstance();
return new SimpleDateFormat(dateFormat, Locale.getDefault())
.format(calendar.getTime());
}
public static String getDate(String dateFormat, long currenttimemillis) {
return new SimpleDateFormat(dateFormat, Locale.getDefault())
.format(currenttimemillis);
}
public static long getBuildDate(Context context) {
try {
ApplicationInfo ai = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), 0);
ZipFile zf = new ZipFile(ai.sourceDir);
ZipEntry ze = zf.getEntry("classes.dex");
long time = ze.getTime();
return time;
} catch (Exception e) {
}
return 0l;
}
}
}
in your build.gradle:
android {
defaultConfig {
buildConfigField 'String', 'BUILD_TIME', 'new java.text.SimpleDateFormat("MM.dd.yy HH:mm", java.util.Locale.getDefault()).format(new java.util.Date(' + System.currentTimeMillis() +'L))'
}
}
I use the same strategy as Pointer Null except I prefer the MANIFEST.MF file.
This one is regenerated even if a layout is modified (which is not the case for classes.dex).
I also force the date to be formated in GMT to avoid confusion between terminal and server TZs (if a comparison has to be made, ex: check latest version).
It result in the following code:
try{
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
ZipFile zf = new ZipFile(ai.sourceDir);
ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
long time = ze.getTime();
SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getInstance();
formatter.setTimeZone(TimeZone.getTimeZone("gmt"));
String s = formatter.format(new java.util.Date(time));
zf.close();
}catch(Exception e){
}
So Android Developer - Android Studio User Guide - Gradle Tips and Recipes - Simplify App Development actually documents what to add in order to have a release timestamp available to your app:
android {
...
buildTypes {
release {
// These values are defined only for the release build, which
// is typically used for full builds and continuous builds.
buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"")
resValue("string", "build_time", "${minutesSinceEpoch}")
...
}
debug {
// Use static values for incremental builds to ensure that
// resource files and BuildConfig aren't rebuilt with each run.
// If they were dynamic, they would prevent certain benefits of
// Instant Run as well as Gradle UP-TO-DATE checks.
buildConfigField("String", "BUILD_TIME", "\"0\"")
resValue("string", "build_time", "0")
}
}
}
...
In your app code, you can access the properties as follows:
...
Log.i(TAG, BuildConfig.BUILD_TIME);
Log.i(TAG, getString(R.string.build_time));
I'm including this here since all of the other solutions appear to be from before the official example.
I know this is really old, but here's how I did it using ant within eclipse:
build.xml in project root
<project name="set_strings_application_build_date" default="set_build_date" basedir=".">
<description>
This ant script updates strings.xml application_build_date to the current date
</description>
<!-- set global properties for this build -->
<property name="strings.xml" location="./res/values/strings.xml"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
</target>
<target name="set_build_date" depends="init" description="sets the build date" >
<replaceregexp file="${strings.xml}"
match="(<string name="application_build_date">)\d+(</string>)"
replace="<string name="application_build_date">${DSTAMP}</string>" />
</target>
</project>
Then add an application_build_date string to your strings.xml
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">your app name</string>
<string name="application_build_date">20140101</string>
...
</resources>
Ensure the ant script is executed as a pre-build activity and you will always have a valid build date available to you within R.string.application_build_date.
For time stamping and versioning, build.gradle/android/defaultConfig:
def buildDateStamp = new Date().format("yyyyMMdd").toInteger()
versionCode buildDateStamp
versionName "$buildDateStamp"
buildConfigField "String", "BUILD_DATE_STAMP", "\"$buildDateStamp\""
Usage in code: BuildConfig.BUILD_DATE_STAMP
resValue "string", "build_date_stamp", "$buildDateStamp"
Usage in xml: "#string/build_date_stamp"
Caveat: adding HHmm will cause errors (probably integer overflow)

Categories

Resources