Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting android:versionName as reference to string resource leads to NullPointerException under the emulator

I'm trying to set the android:versionName in my android manifest as a reference to string resource stored in external resource file.

Below is an excerpt from my AndroidManifest.xml:

<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.toycorporation"
  android:versionCode="@integer/version_code"
  android:versionName="@string/version_name"
>

and the content of build.xml file located under res/values disrectory of my project:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <item type="string" name="build_date">03/15/2012</item>
  <item type="integer" name="version_code">315281</item>
  <item type="string" name="version_name">3.15.28.1</item>                 
</resources>

Later I attempt to retrieve the version number to display it on the About screen of my application.

PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),0);
Log.d("PackageInfo", "Version name: " + String.valueOf(packageInfo.versionName));

Problem description:

When I build the application and automatically install it on my HTC device everything works perfect. Version name gets passed by reference and on the About screen I get version name value logged in the logcat. But when I attempt to build the application and launch it on the emulator I get null instead of the version name value.

I have already tried to build and install the app to emulator using Eclipse and IDEA. So it seems the issue is not related to IDE.

UPDATE:

One additional thing appeared which doesn't work with such use of versionName and versionCode. Application can not be deployed to the Google Play. The following error appears:

The file is invalid: ERROR getting 'android:versionCode' attribute: attribute is not an integer value

like image 466
Vladimir Kosinets Avatar asked Mar 15 '12 15:03

Vladimir Kosinets


2 Answers

I think there is a mix of issues with this approach / how you are using it.

1.) if you can compile and upload the APK to the android market and the correct version number and name is used then you are able to use resource references for the android:versionCode="@integer/version_code" android:versionName="@string/version_name"

If you can't then you shouldn't be using rerences.

2.) If you are going to take this approach, why then try to retrieve them from the packageinfo over using getResources().getString(R.string.version_name)?

All in all I don't see much of an advantage of this approach beyond being able to swap out a resources file by a build box. In which case you may need to for the latter approach to populate your about screen.

like image 65
Matt Gaunt Avatar answered Nov 08 '22 23:11

Matt Gaunt


You can set versionName in manifest as a reference to string resource, but not versionCode.

like image 24
Konpon96 Avatar answered Nov 08 '22 23:11

Konpon96