Android : Unable to get version name in code after moving to Gradle

on Friday, October 10, 2014


I've just moved one of my Android projects over to Gradle, and as per recommendations inside the IDE and online I've moved all the configurations (min/max sdk, version name/code etc) into the build.gradle file. Now a bug has been reported from a customer that where he could usually see the version name and code inside the product there is nothing.


After checking it out I've found out that the usual way of extracting those values has stopped working after I removed the android:versionCode="" and android:versionName="" tags from AndroidManifest.xml. I have verified that it works when these values are there.


Now I've been trying to search for a way of extracting these same values to use in code, but I have not found any reference to this behaviour else where (this not working like this).


The code I used to use to get the values was to simply extract them from the PackageManager as so



try{
PackageInfo pInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
return pInfo.versionName + " Build " +pInfo.versionCode;
} catch(PackageManager.NameNotFoundException e){
e.printStackTrace();
}
return "";


All hints, tips or solutions appreciated.


0 comments:

Post a Comment