How to write build time stamp into apk

后端 未结 10 623
别跟我提以往
别跟我提以往 2020-12-02 07:57
  1. Making some changes in Android Contacts package
  2. Using mm (make) command to build this application

Because I have to change and

相关标签:
10条回答
  • 2020-12-02 08:12

    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)

    0 讨论(0)
  • 2020-12-02 08:13

    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);
    
    0 讨论(0)
  • 2020-12-02 08:15
    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;
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-02 08:21

    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.

    0 讨论(0)
提交回复
热议问题