1 .设置apk版本号
Androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rockcheck.mes"> <!--版本号,用来升级--> android:versionCode="1" <!--版本名,用来展示给用户--> android:versionName="1.0.0"> </manifest>
android stduio里得改法
2.获取版本号类
import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; public class APKVersion { //获取当前本地apk的版本 public static int getVersionCode(Context context) { PackageManager manager = context.getPackageManager(); int versionCode = 0; try { //获取软件版本号,对应AndroidManifest.xml下android:versionCode PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); versionCode = info.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return versionCode; } //获取版本号名称 public static String getVerName(Context context) { PackageManager manager = context.getPackageManager(); String versionName = null; try { PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); versionName = info.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return versionName; } }
调用方法:
String versionCode = APKVersion.getVersionCode(this) + ""; String versionName = APKVersion.getVerName(this);
3.
APKVersion