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

我必须使用AndroidManifest.xml吗?


当前回答

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

场景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) {
    ""
}

其他回答

这是一个干净的解决方案,基于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);

对于Xamarin用户,使用此代码获取版本名称和代码

版本名称:公共字符串getVersionName(){return Application.Context.ApplicationContext.BackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName,0).VersionName;}版本代码:公共字符串getVersionCode(){return Application.Context.ApplicationContext.BackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName,0).VersionCode;}

不,您不需要对AndroidManifest.xml执行任何操作

基本上,应用程序的版本名称和版本代码位于应用程序级Gradle文件中的defaultConfig标记下:

defaultConfig {
   versionCode 1
   versionName "1.0"
}

注意:当您希望将应用程序上载到play商店时,它可以提供任何名称作为版本名称,但如果此应用程序已在play商店中,则版本代码必须与当前版本代码不同。

只需使用以下代码片段即可从应用程序中的任何位置获取版本代码和版本名称:

try {
    PackageInfo pInfo =   context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    String version = pInfo.versionName;
    int verCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

Kotlin示例:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.act_signin)

    packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).apply {
        findViewById<TextView>(R.id.text_version_name).text = versionName
        findViewById<TextView>(R.id.text_version_code).text =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) "$longVersionCode" else "$versionCode"
    }

    packageManager.getApplicationInfo(packageName, 0).apply{
        findViewById<TextView>(R.id.text_build_date).text =
            SimpleDateFormat("yy-MM-dd hh:mm").format(java.io.File(sourceDir).lastModified())
    }
}

如果要在XML内容上使用它,请在Gradle文件中添加以下行:

applicationVariants.all { variant ->
    variant.resValue "string", "versionName", variant.versionName
}

然后将其用于XML内容,如下所示:

<TextView
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/versionName" />