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);

转载请注明出处



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值