3

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?

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That did the trick. But, I don't get, then, why none of the other postings mentioned this, or why anyone would bother entering the items in the manifest, if they will not be used, and would just add to confusion.
I'll just mention that for me an added problem was that I'd switched from a non-Gradle to a Gradle-based build technique, and then strange things were happening with Gradle indicating that it was installing the .apk on the device, but in reality the old app was still being launched. Only after I uninstalled the old app on the device did things start to make sense again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.