How to read gradle file properties from Android app programatically -


the build.gradle file has following section. how read android.defaultconfig.versioncode android app mainactivity

android {     defaultconfig {         applicationid "com.gatta.e.gatta"         minsdkversion 11         targetsdkversion 21         versioncode 1         versionname "1.0"     } } 

you don't have read gradle file obtain version code. here direct way within activity:

private packageinfo getpackageinfo() {     packageinfo pi = null;     try {         pi = this.getpackagemanager().getpackageinfo(                          this.getpackagename(), packagemanager.get_activities);     } catch (packagemanager.namenotfoundexception e) {         e.printstacktrace();         log.e("yourtaghere", e.getmessage());     }     return pi; } 

and use in oncreate(...) method example obtain version code:

string versioncode = "" + getpackageinfo().versioncode; 

more info on packageinfo class here. , quick description of class:

overall information contents of package. corresponds of information collected androidmanifest.xml.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -