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

我必须使用AndroidManifest.xml吗?


当前回答

    Log.e("TAG","VERSION.RELEASE {" + Build.VERSION.RELEASE + "}");
    Log.e("TAG","\nVERSION.INCREMENTAL {" + Build.VERSION.INCREMENTAL + "}");
    Log.e("TAG","\nVERSION.SDK {" + Build.VERSION.SDK + "}");
    Log.e("TAG","\nBOARD {" + Build.BOARD + "}");
    Log.e("TAG","\nBRAND {" + Build.BRAND + "}");
    Log.e("TAG","\nDEVICE {" + Build.DEVICE + "}");
    Log.e("TAG","\nFINGERPRINT {" + Build.FINGERPRINT + "}");
    Log.e("TAG","\nHOST {" + Build.HOST + "}");
    Log.e("TAG","\nID {" + Build.ID + "}");

幸亏https://stackoverflow.com/a/40421709/2828651

其他回答

我已经通过使用Preference类解决了这个问题。

package com.example.android;

import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;

public class VersionPreference extends Preference {
    public VersionPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        String versionName;
        final PackageManager packageManager = context.getPackageManager();
        if (packageManager != null) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
                versionName = packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                versionName = null;
            }
            setSummary(versionName);
        }
    }
}

您需要两个部分:

android:版本代码android:versionName

versionCode是一个数字,您向市场提交的应用程序的每个版本都需要有一个比上一个版本更高的数字。

VersionName是一个字符串,可以是任何你想要的东西。这是你定义你的应用程序为“1.0”或“2.5”或“2 Alpha EXTREME!”或任何东西的地方。

例子:

科特林:

val manager = this.packageManager
val info = manager.getPackageInfo(this.packageName, PackageManager.GET_ACTIVITIES)
toast("PackageName = " + info.packageName + "\nVersionCode = "
            + info.versionCode + "\nVersionName = "
            + info.versionName + "\nPermissions = " + info.permissions)

Java语言:

PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), PackageManager.GET_ACTIVITIES);
Toast.makeText(this,
     "PackageName = " + info.packageName + "\nVersionCode = "
       + info.versionCode + "\nVersionName = "
       + info.versionName + "\nPermissions = " + info.permissions, Toast.LENGTH_SHORT).show();

如果您使用Gradle插件/AndroidStudio,从0.7.0版起,版本代码和版本名称在BuildConfig中静态可用。确保导入应用程序的包,而不是另一个BuildConfig:

import com.yourpackage.BuildConfig;
...
int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;

不需要Context对象!

还要确保在build.gradle文件中指定它们,而不是AndroidManifest.xml。

defaultConfig {
    versionCode 1
    versionName "1.0"
}

始终使用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());
}

这段代码在上面提到过,但在这里,它再次全部包含在内。您需要try/catch块,因为它可能引发“NameNotFoundException”。

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

我希望这能为将来的人简化事情