wwfloyd
wwfloyd

Reputation: 312

code pulls wrong versionName from manifest

OK, I read several threads on getting the versionName from the manifest, as for use in an About dialog. Two techniques are noted. I did it both ways, and both come up with version 1.0, which is NOT what I have in the manifest. I can't find any posts noting the same problem. I'm working in Android Studio.

Lines from the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wabner.awesome_app"
    android:versionCode="23"
    android:versionName="0.70b">

    <uses-permission android:name="android.permission.INTERNET" />

Lines from the About DialogFragment:

Context c = getActivity().getApplicationContext();
String versionName = "";

try {
    versionName = c.getPackageManager().getPackageInfo(c.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

TextView tv1 = (TextView) view.findViewById(R.id.version_number);
tv1.setText(versionName);

Or, the simpler way:

TextView tv1 = (TextView) view.findViewById(R.id.version_number);
tv1.setText(BuildConfig.VERSION_NAME);

I'm probably missing something obvious, but, I'm new at Android. And tired. To bed...

Minutes later... is it possible the version number can only be accessed if the app is packed for distribution?

Upvotes: 3

Views: 1171

Answers (1)

IvanP
IvanP

Reputation: 1104

If you are using gradle build system versionCode and versionName fields from the manifest will be overridden with values from build.gradle. So, go to module's build.gradle, change versionCode/versionName, it must looks like

android {
    ...
    defaultConfig {
        ...
        versionCode 23
        versionName '0.70b'
    }
    ...
}

You may check it using first way that you have mentioned

Upvotes: 10

Related Questions