android 获取到的版本号一直显示为1.0 VersionName VersionCode

刚开始接手android开发了,做了个自动更新程序,拿本地版本号与服务器版本比较,发现获取本地的版本号一直为1.0,一直找不到原因,后来没办法了只能用string 代替

,刚又从网上查了一下 发现是配置的问题 ,配置文件

修改之前

apply plugin: 'android'

android {
 compileSdkVersion 19
 buildToolsVersion '19.0.3'

 defaultConfig {
 minSdkVersion 8
 targetSdkVersion 19
 versionCode 1
 versionName "1.0"
 }
 buildTypes {
 release {
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
 versionNameSuffix '1.5'
 debuggable true
 runProguard true
 }
 }
}

dependencies {
 compile 'com.android.support:appcompat-v7:+'
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile files('libs/ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar')
}

修改之后

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.3'

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }
    buildTypes {
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            versionNameSuffix '1.5'
            debuggable true
            runProguard true
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar')
}


把中间defaultConfig节中的后两项去掉,成功获取到版本号


获取版本号代码

 PackageManager manager = this.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            appVersion = info.versionName; // 版本名,versionCode同理
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("当前版本号为:"+appVersion);

转载请注明出处




猜你喜欢

转载自blog.csdn.net/queenpong/article/details/41871219