How to write creation timestamp in apk

  • Making changes to the Android contacts pack
  • Using the mm (make) command to create this application

Because I need to change and create this application again and again, so I want to add the assembly time stamp in Contacts.apk to check the assembly time when we started it in the handset.

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

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

But how can we write this assembly timestamp to a file that our application can read at runtime?

Any suggestions?

+55
android build makefile
Sep 30 '11 at 7:21
source share
10 answers

If you are using Gradle, you can add buildConfigField with a timestamp updated during build.

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

Then read it at runtime.

 Date buildDate = new Date(BuildConfig.TIMESTAMP); 
+123
Oct. 15 '14 at 0:13
source share

A method that checks the date of the last modification of classes.dex, this means the last time your application code was created:

  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 great even if the application is installed on an SD card.

+94
Sep 30 2018-11-11T00:
source share

Starting with API version 9:

PackageInfo.lastUpdateTime

The time the application was last updated.

 try { PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0); //TODO use packageInfo.lastUpdateTime } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } 

In lower versions of the API, you must create the build time yourself. For example, paste the file into the data folder containing the date. Or using the __ DATE__ macro in native code. Or check the date your .dex class was created (file date in your APK).

+24
Sep 30 2018-11-11T00:
source share

A hint for solving "the time of the last modification of the classes.dex file" is newer versions of AndroidStudio: In the default configuration, the time stamp is no longer written to the files in the apk file. The timestamp is always "November 30, 1979."

You can change this behavior by adding this line to the file

% userdir% /. gradle / gradle.properties (create if not exist)

 android.keepTimestampsInApk = true 

See Problem 220039

(Should be in userdir, gradle.properties in the build dir project doesn't seem to work)

+13
Oct 07 '16 at 13:00
source share

in your build.gradle:

 android { defaultConfig { buildConfigField 'String', 'BUILD_TIME', 'new java.text.SimpleDateFormat("MM.dd.yy HH:mm", java.util.Locale.getDefault()).format(new java.util.Date(' + System.currentTimeMillis() +'L))' } } 
+7
05 Oct '16 at 10:36
source share
 Install time : packageInfo.lastUpdateTime build time : zf.getEntry("classes.dex").getTime() 

Both have different times. You can check the code below.

 public class BuildInfoActivity extends Activity { private static final String TAG = BuildInfoActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { PackageManager pm = getPackageManager(); PackageInfo packageInfo = null; try { packageInfo = pm.getPackageInfo(getPackageName(), 0); } catch (NameNotFoundException e) { e.printStackTrace(); } // install datetime String appInstallDate = DateUtils.getDate( "yyyy/MM/dd hh:mm:ss.SSS", packageInfo.lastUpdateTime); // build datetime String appBuildDate = DateUtils.getDate("yyyy/MM/dd hh:mm:ss.SSS", DateUtils.getBuildDate(this)); Log.i(TAG, "appBuildDate = " + appBuildDate); Log.i(TAG, "appInstallDate = " + appInstallDate); } catch (Exception e) { } } static class DateUtils { public static String getDate(String dateFormat) { Calendar calendar = Calendar.getInstance(); return new SimpleDateFormat(dateFormat, Locale.getDefault()) .format(calendar.getTime()); } public static String getDate(String dateFormat, long currenttimemillis) { return new SimpleDateFormat(dateFormat, Locale.getDefault()) .format(currenttimemillis); } public static long getBuildDate(Context context) { try { ApplicationInfo ai = context.getPackageManager() .getApplicationInfo(context.getPackageName(), 0); ZipFile zf = new ZipFile(ai.sourceDir); ZipEntry ze = zf.getEntry("classes.dex"); long time = ze.getTime(); return time; } catch (Exception e) { } return 0l; } } } 
+6
Feb 19 '13 at 7:51
source share

I use the same strategy as the Null pointer, except that I prefer the MANIFEST.MF file. This one is regenerated even if the layout is changed (which does not apply to classes.dex). I also force the date to be formed in GMT to avoid confusion between terminal and server TK (if you need to make a comparison, for example: check the latest version).

The result is 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){ } 
+5
Jun 02 '14 at 2:02
source share

I know this is really old, but here is how I did it using ant inside eclipse:

build.xml at the root of the project

 <project name="set_strings_application_build_date" default="set_build_date" basedir="."> <description> This ant script updates strings.xml application_build_date to the current date </description> <!-- set global properties for this build --> <property name="strings.xml" location="./res/values/strings.xml"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> </target> <target name="set_build_date" depends="init" description="sets the build date" > <replaceregexp file="${strings.xml}" match="(&lt;string name=&quot;application_build_date&quot;&gt;)\d+(&lt;/string&gt;)" replace="&lt;string name=&quot;application_build_date&quot;&gt;${DSTAMP}&lt;/string&gt;" /> </target> </project> 

Then add the line application_build_date to your strings.xml file

 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <string name="app_name">your app name</string> <string name="application_build_date">20140101</string> ... </resources> 

Make sure that the ant script is executed as a pre-build operation and you will always have a valid build date available to you in the R.string.application_build_date file.

+4
Jan 6 '14 at 15:50
source share

So, Android Developer - Android Studio User Guide - Tips and Recipes Gradle - Simplified application development actually documents what to add in order to have a release time stamp available for your application:

 android { ... buildTypes { release { // These values are defined only for the release build, which // is typically used for full builds and continuous builds. buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"") resValue("string", "build_time", "${minutesSinceEpoch}") ... } debug { // Use static values for incremental builds to ensure that // resource files and BuildConfig aren't rebuilt with each run. // If they were dynamic, they would prevent certain benefits of // Instant Run as well as Gradle UP-TO-DATE checks. buildConfigField("String", "BUILD_TIME", "\"0\"") resValue("string", "build_time", "0") } } } ... 

In the application code, you can access the properties as follows:

 ... Log.i(TAG, BuildConfig.BUILD_TIME); Log.i(TAG, getString(R.string.build_time)); 

I have included this here, since all other solutions, apparently, were before the official example.

+3
Dec 02 '18 at 6:45
source share

To timestamp and version control build.gradle / android / defaultConfig:

 def buildDateStamp = new Date().format("yyyyMMdd").toInteger() versionCode buildDateStamp versionName "$buildDateStamp" buildConfigField "String", "BUILD_DATE_STAMP", "\"$buildDateStamp\"" 

Use in code: BuildConfig.BUILD_DATE_STAMP

 resValue "string", "build_date_stamp", "$buildDateStamp" 

Usage in xml: "@string/build_date_stamp"

Caution: adding HHmm will lead to errors (possibly integer overflow)

+1
Sep 05 '19 at 12:42
source share



All Articles