Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write build time stamp into apk

  1. Making some changes in Android Contacts package
  2. Using mm (make) command to build this application

Because I have to change and build this app again and again, so I want to add a build time stamp in the Contacts.apk to check the build time when we runn it in the handset.

As we know, when we run mm command, the Android.mk (makefile) in Contacts package will be called.

And now, we can get the build time using date-macro.

But how we can write this build time stamp into a file that our application can read at runtime?

Any suggestions?

like image 421
FallingRain Avatar asked Sep 30 '11 07:09

FallingRain


People also ask

How do you write an APK?

Creating a Signed APK File To generate a signed APK file, open the Build menu from the toolbar and select Generate Signed Bundle/APK. This opens up a screen where you have to select between creating an Android App Bundle and creating an APK file.


2 Answers

Method which checks date of last modification of classes.dex, this means last time when your app's code was built:

  try{      ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);      ZipFile zf = new ZipFile(ai.sourceDir);      ZipEntry ze = zf.getEntry("classes.dex");      long time = ze.getTime();      String s = SimpleDateFormat.getInstance().format(new java.util.Date(time));      zf.close();   }catch(Exception e){   } 

Tested, and works fine, even if app is installed on SD card.

like image 40
Pointer Null Avatar answered Sep 21 '22 16:09

Pointer Null


If you use Gradle, you can add buildConfigField with timestamp updated at build time.

android {     defaultConfig {         buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"     } } 

Then read it at runtime.

Date buildDate = new Date(BuildConfig.TIMESTAMP); 
like image 140
Aleksejs Mjaliks Avatar answered Sep 19 '22 16:09

Aleksejs Mjaliks