I am trying to get package version code compiled in another project and linked as a dependency on this. I often make changes to both projects, so I like to know version numbers.
The solutions are here: How to get the build / version number of your Android application? shows how to get the version code from the context, but this will give the version code for packages in the current project, not dependent ones.
Here is the code I tried (3 scenarios):
public static int getPackageVersion(Context context, Class<?> tClass) {
int versionCode = 0;
String packageName = tClass.getPackage().getName();
Log.i(TAG, packageName);
try {
versionCode = context.getPackageManager().getPackageInfo(
packageName, 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, packageName);
throw new RuntimeException();
}
return versionCode;
}
# 1) Shows how the method was originally designed to work; it uses the package name for the class. Results in NameNotFound.
# 2) I manually set the package name topName for the dependent package, which is similar to the returned context.getPackageName (). Results in NameNotFound.
# 3) context.getPackageName(), - , , .
, , ( ) .