Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the versionName without the suffix from Android?

Consider app.gradleincludes the following:

    defaultConfig {
       versionName "2.1.6"
    }
    debug {
        debuggable true
        versionNameSuffix "-debug"
    }

Is it possible to get the version name without the suffix? When using PackageInfo pInfo = applicationContext.getPackageManager().getPackageInfo(applicationContext.getPackageName(), 0); pInfo.versionName returns 2.1.6-debug is there a way to get only 2.1.6 without doing some string or regex matching.

Thank you!

like image 996
jrichardlai Avatar asked Apr 05 '16 19:04

jrichardlai


2 Answers

I would do it using generated BuildConfig:

Gradle

defaultConfig {
  def version = "2.1.6"
  versionName version
  buildConfigField "String", "VERSION", "\"$version\""
}

Java

String version = BuildConfig.VERSION;
like image 84
Michael Avatar answered Sep 18 '22 09:09

Michael


Question is old, but it might help someone - I have looked for something similar recently and used this:

debug{
     buildConfigField "String", "APP_VER", "\"" + android.defaultConfig.versionName + "\""
}

OR for all buidTypes

buildTypes.each {
       it.buildConfigField "String", "APP_VER", "\"" + android.defaultConfig.versionName + "\""
}

OR for all app variants:

applicationVariants.all { variant ->
    variant.buildConfigField "String", "APP_VER", "\"" + android.defaultConfig.versionName + "\""
}
like image 27
Luke Avatar answered Sep 21 '22 09:09

Luke