【问题标题】:Why ZipEntry shows incorrect compilation time of APK file?为什么 ZipEntry 显示错误的 APK 文件编译时间?
【发布时间】:2017-01-29 08:15:08
【问题描述】:

我使用此代码显示 App 编译日期:

ApplicationInfo ai = a.getPackageManager().getApplicationInfo(a.getPackageName(), 0);
        ZipFile zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
        long time = ze.getTime() + (3600000 * 2);
        Log.i("date", new SimpleDateFormat("HH:mm:ss dd.MM.yyyy").format(new Date(time)));
        zf.close();

昨天一切都很好,但今天(Android Studio 升级后???)每次都显示

02:00:00 30.11.1979

??? 我已经清理并重建了项目。 这里有什么问题?

【问题讨论】:

  • 你为什么要在时间里加上3600000 * 2? 83.3333 天?从什么时候开始的清单日期是编译日期?
  • 没有任何关于使用清单日期作为编译日期或将3600000 * 2 添加到时间。
  • 添加 3600000 * 2 只是为了我,没关系
  • 是的,Gradle 更新时需要 AS 清除选项。

标签: java android


【解决方案1】:

好的,我有一个答案来自:How to write build time stamp into apk

最好的选择是在 Gradle 中使用时间戳

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

}

Date buildDate = new Date(BuildConfig.TIMESTAMP);

谢谢:)

【讨论】:

    【解决方案2】:

    根据response #3 on this Google Issue,获取构建日期的ZipFile方法仍然可以使用,只要在Gradle属性中设置“android.keepTimestampsInApk = true”。

    Gradle 有意将构建时间戳清零,除非设置了该属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多