7

I am developing an android application, i want to display the Version of the .apk and date now i can able to display app version of the application using PackageInfo and now i want to display the date when app is created or .apk creation date.

W I Z A R D
  • 1,224
  • 3
  • 17
  • 44
  • When you make the application just put the current date in a constant, then read it in the application. – PurkkaKoodari Apr 10 '14 at 06:08
  • @Pietu1998 Ok i can get the current date but how can i make it constant. – W I Z A R D Apr 10 '14 at 06:10
  • I mean, whenever you build your APK just put the date in a constant (`public static final String`) yourself, and have your application use that value. If you put it in a simple enough file you can also do it automatically, like [this one](http://stackoverflow.com/questions/6758685/auto-increment-version-code-in-android-app). – PurkkaKoodari Apr 10 '14 at 06:14
  • Kotlin example (ready to use): https://stackoverflow.com/a/57689199/2098595 – Alexander Gavriliuk Aug 28 '19 at 21:40

3 Answers3

7

For new readers:

public static String getAppTimeStamp(Context context) {
        String timeStamp = "";

        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
            String appFile = appInfo.sourceDir;
            long time = new File(appFile).lastModified();

            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        timeStamp = formatter.format(time);

        } catch (Exception e) {

        }

        return timeStamp;

    }

https://stackoverflow.com/a/2832419/1968592

Community
  • 1
  • 1
NBApps
  • 511
  • 5
  • 12
  • This uses the directory the APK is in. Not the apk itself. But I guess that's ok since the folder's modify time will change when the apk changes. – Yetti99 Feb 18 '17 at 17:08
  • As @Yetti99 noticed, this is the time stamp on the apk directory, not on the apk file. That makes it the install date. I finally added a timestamp to the BuildConfig.java file as discussed at https://stackoverflow.com/questions/7607165/how-to-write-build-time-stamp-into-apk/39917953. – steven smith Mar 25 '18 at 02:32
6

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

 }catch(Exception e){
 }
Irshad Khan
  • 794
  • 6
  • 21
  • 1
    This is no longer working for me. It used to, but now it doesn't. Don't know since when. – Daniel F Nov 27 '16 at 20:50
  • 1
    @DanielF http://stackoverflow.com/a/7608719/934646 It's discussed there, why this code doesn't work, i'm also looking for the same – Shirish Herwade Dec 02 '16 at 11:11
  • What exactly is the time that this code returns? The time the developer has created the "classes.dex" file? Can it be used to uniquely decide if the APK is the correct one, and have easy clue to verify that 2 APKs are identical ? – android developer Feb 25 '17 at 00:36
3

I use the same strategy as Irshad Khan and Pointer Null except I prefer the MANIFEST.MF file. This one is regenerated even if a layout is modified (which is not the case for classes.dex). I also force the date to be formated in GMT to avoid confusion between terminal and server TZs (if a comparison has to be made, ex: check latest version).

It result in the following code:

  try{
     ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
     ZipFile zf = new ZipFile(ai.sourceDir);
     ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
     long time = ze.getTime();
     SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getInstance();
     formatter.setTimeZone(TimeZone.getTimeZone("gmt"));
     String s = formatter.format(new java.util.Date(time));
     zf.close();
  }catch(Exception e){
  }
Damien
  • 767
  • 9
  • 13