我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。

我必须使用AndroidManifest.xml吗?


当前回答

这是一个干净的解决方案,基于scottyab(由哈维编辑)的解决方案。它显示了如果方法没有提供上下文,如何首先获取上下文。此外,它使用多行而不是每行调用多个方法。这使您在调试应用程序时更容易。

Context context = getApplicationContext(); // or activity.getApplicationContext()
PackageManager packageManager = context.getPackageManager();
String packageName = context.getPackageName();

String myVersionName = "not available"; // initialize String

try {
    myVersionName = packageManager.getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

现在您在StringmyVersionName中收到了版本名,您可以将其设置为TextView或任何您喜欢的内容。。

// Set the version name to a TextView
TextView tvVersionName = (TextView) findViewById(R.id.tv_versionName);
tvVersionName.setText(myVersionName);

其他回答

始终使用try-catch块:

String versionName = "Version not found";

try {
    versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    Log.i(TAG, "Version Name: " + versionName);
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    Log.e(TAG, "Exception Version Name: " + e.getLocalizedMessage());
}

内部片段用法示例。

import android.content.pm.PackageManager;
.......

private String VersionName;
private String VersionCode;
.......


Context context = getActivity().getApplicationContext();

/* Getting application version name and code */
try
{
     VersionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;

     /* I find it useful to convert vervion code into String,
        so it's ready for TextViev/server side checks
     */

     VersionCode = Integer.toString(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
}
catch (PackageManager.NameNotFoundException e)
{
     e.printStackTrace();
}

// Do something useful with that

就写这个。

XML代码

<TextView
        android:id="@+id/appversion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

初始化textview的变量

TextView appversion;

将textview id设置为变量

appversion = findViewById(R.id.appversion);

这是显示应用程序版本BuildConfig.version_NAME的主要代码,仅将其用作字符串值

textview.setText("v"+BuildConfig.VERSION_NAME);
private String GetAppVersion() {
    try {
        PackageInfo _info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
        return _info.versionName;
    }
    catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

private int GetVersionCode() {
    try {
        PackageInfo _info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
        return _info.versionCode;
    }
    catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return -1;
    }
}

在这个问题中有两种不同的情况,在任何答案中都没有得到正确的解决。

场景1:您没有使用模块

如果您没有使用模块,则可以访问BuildConfig文件,并立即使用以下命令获取版本代码:

val versionCode = BuildConfig.VERSION_CODE 

这是有效的,因为这是应用程序级别的BuildConfig文件,因此它将包含对应用程序版本代码的引用

场景2:您的应用程序有许多模块,您假装从模块层次结构中的较低模块访问应用程序版本代码

对于您来说,有许多具有给定层次结构的模块是正常的,例如app->data->domain->ui等。在这种情况下,如果您从“ui”模块访问BuildConfig文件,它将不会向您提供应用程序版本代码的引用,而是该模块的版本代码。

为了获得应用程序版本代码,您可以使用给定的kotlin扩展函数:

fun Activity.getVersionCode(): Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    packageManager.getPackageInfo(packageName, 0).longVersionCode.toInt()
} else {
    packageManager.getPackageInfo(packageName, 0).versionCode
}

版本名称的方法类似:

fun Activity.getVersionName(): String = try {
    packageManager?.getPackageInfo(packageName, 0)?.versionName ?: ""
} catch (e: PackageManager.NameNotFoundException) {
    ""
}