Java – how to get the version code of dependent packages?

public static int getPackageVersion(Context context, Class<?> tClass) {
    int versionCode = 0;

    //#1) Get Package name from class
    String packageName = tClass.getPackage().getName();

    //#2) Set Domain manually to top level package
    //String packageName = "com.somedomain";

    //#3) Get package name from context from this package
    //String packageName = context.getPackageName();

    Log.i(TAG, packageName);

    try {
        versionCode = context.getPackageManager().getPackageInfo(
                packageName, 0).versionCode;
    } catch (PackageManager.NameNotFoundException e) {

        Log.e(TAG, packageName);
        throw new RuntimeException();
    }

    return versionCode;
}

resolvent:

android {
    defaultConfig {
        versionCode 1
        versionName "1.0.0"
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>