The code infers the wrong versionName from the manifest

OK, I have read multiple threads when getting the versionName from the manifest as to use in the About dialog. Two methods are noted. I did it both ways and both came up with version 1.0, which is NOT what I have in the manifest. I can't find any posts mentioning the same issue. I am 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 dialog:

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, more simply:

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

      

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

After a few minutes ... is it possible that the version number might only be available if the app is packaged for distribution?

+3


source to share


1 answer


If you are using gradle build system versionCode and versionName fields from manifest will be overridden with values ​​from build.gradle. So go to build.gradle module, change versionCode / versionName, it should look like

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

      



You can check this using the first way you mentioned

+8


source







All Articles