How to write build time stamp into apk

android get app build date
buildconfigfield
testbuildtype
buildconfig debug
gradle tips and tricks for android
com android build outputfile
android applicationvariants all
speed up gradle build
  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?

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

How to get build time of APK? - Zhihui Tang, Add buildConfigField with timestamp updated at build time. android { defaultConfig { buildConfigField "long", "TIMESTAMP", System. public final class BuildConfig { public static final java.util.Date BUILD_TIME = new java.util.Date(1395794838381L); } So, to access the build date within your app…. Date buildDate = BuildConfig.BUILD_TIME; Log.i("MyProgram", "This .apk was built on " + buildDate.toString()); (You can format the date however you like using a SimpleDateFormat.)

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.

How to write build time stamp into apk, How to write build time stamp into apk. speed up gradle build buildconfig android get app build date gradle resvalue get date from timestamp unix timestamp You might try to get hold of the .apk file and try to "unzip" it with the help of a ZipInputStream in order to get at the entries. But as you say this will give you the timestamps of the build date (probably because the build chain is copying the files into a target directory before zipping it up).

Since API version 9 there's:

PackageInfo.lastUpdateTime

The time at which the app was last updated.

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

On lower API versions you must make build time yourself. For example putting a file into assets folder containing the date. Or using __ DATE__ macro in native code. Or checking date when your classes.dex was built (date of file in your APK).

How to write build time stamp into apk, But how we can write this build time stamp into a file that our application can read Gradle, you can add buildConfigField with timestamp updated at build time. Use DateTime.Now to get the current timestamp, then call .ToString on it with the date format you want (for example, yyyy-MM-dd-HH-mm-ss which is year-month-day-hour-minute-second) and put that in your file name. Also, if you use a verbatim string literal (with @), you don't have to escape the backslashes.

Edit: My answer does not work anymore since option keepTimestampsInApk was removed. Working in 2020 is https://stackoverflow.com/a/26372474/6937282 (also https://stackoverflow.com/a/22649533/6937282 for more details)

Original answer:

A hint for solution "last modification time of classes.dex file" an newer AndroidStudio versions: In default config the timestamp is not written anymore to files in apk file. Timestamp is always "Nov 30 1979".

You can change this behavior by adding this line to file

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

android.keepTimestampsInApk = true

See Issue 220039

(Must be in userdir, gradle.properties in project build dir seems not to work)

How to write build time stamp into apk, But how we can write this build time stamp into a file that our In default config the timestamp is not written anymore to files in apk file. Timestamp Camera is maybe the only App that can record video with the time accurate to second. – Support 20 timestamp format – Support change font, font color, font size – Support set timestamp in 6 positions: top left, top center, top right, bottom left, bottom center, bottom right – Support auto add location.

Install time : packageInfo.lastUpdateTime
build time   : zf.getEntry("classes.dex").getTime()

Both are differnet time. You can check with 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;
        }

    }
}

Timestamp, Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date  I'm experimenting with new Android build system based on Gradle and I'm thinking, what is the best way to autoincrease versionCode with it. I am thinking about two options. create versionCode file, read number from it, increase it and write it back to the file

custom versionName with versionCode + timestamp, in release apk , custom versionName with versionCode + timestamp, in release apk filename. build.gradle. android { . defaultConfig { versionCode 3. versionName  Adding something like this to your build project configuration should do the trick: allProjects { gradle.taskGraph.whenReady { taskGraph -> versionNameSuffix += '-build' + // Java/Groovy code to produce the timestamp formatted the way you want } }

Get current time and date on Android, In this example, we have imported simple date format class from java as Step 1 − Create a new project in Android Studio, go to File ⇒ New Date; public class MainActivity extends AppCompatActivity { @TargetApi(Build. You might want to dig into the subject, by reading the official docs. In synthesis: Add this permission to your Manifest: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> It includes the READ permission, so no need to specify it too.

Prepare & roll out releases - Play Console Help, With a release, you can manage your app's Android App Bundle, which is Google A release is a combination of one or more build artifacts that you'll prepare to roll releases in the menu, make sure you have permission to manage releases. Rollout details: A timestamp indicating the last rollout event for each release. Timestamp camera offers you an app with few ads and no distorted images in case you think this application reduces the quality of your pictures - it doesn't. It is FREE to use for the most part and doesn't freeze when called upon to carry out its function.

Comments
  • Are you wanting to add the build timestamp to the apk itself? so it becomes MyApp-201109302359.apk?
  • Thanks, I have achieve this. as MyApp-201109302359.apk, but as you know, I have to build this app many times. If I do so, there will be many MyApp-*.apk in *system/app folder, it's not what I want.
  • I want to write the build timestamp into a file that my apk can read from and display it in Activity at runtime.
  • I posted a way to do this w/Android Studio & gradle here: stackoverflow.com/a/22649533/3035127
  • I find this to be the best option if you are using Gradle
  • Thanks, I was looking quite a while for something so easy
  • This should be considered as the best answer.
  • What is BuildConfig?. you should add the class definition.
  • No longer seems to work. I get the error: Could not find method buildConfigField() for arguments [long, BUILD_TIMESTAMP] on DefaultConfig_Decorated
  • Yes, now I use it too in About dialog :)
  • how can I add that "String s" to .apk name?