如何以编程方式阅读我的Android apk的日期? [英] How to programmatically read the date when my Android apk was built?

查看:415
本文介绍了如何以编程方式阅读我的Android apk的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编程阅读建立我的Android apk的日期?我在PackageInfo类中找不到任何东西。



我想要过期我的应用程序的测试版本,最简单的方法是读出这样的日期并过期一个修复preiode的日子,所以我不必更新代码,我建立和部署测试版的eavery时间。

解决方案

这是一段时间以来被问到,但是这是我如何使用Android Studio w / gradle ....



build.gradle ,您的每个构建类型应该有一个 buildConfigField 。 (这是一个简化版本的配置 - 你可能会有其他的东西在这里,但我想显示你的地方):

  android {
signatureConfigs {
buildTypes {
debug {
buildConfigFieldjava.util.Date,buildTime,new java.util.Date (+ System.currentTimeMillis()+L)
}
release {
buildConfigFieldjava.util.Date,buildTime,new java.util.Date + System.currentTimeMillis()+L
}
}
}
}

(请注意, BuildConfigField 较新版本 BuildConfigLine 。)



下一次gradle会组合发布或组合Debug,它应该生成:



./ build / source / buildConfig / releaseORdebug / com / your / project / BuildConfig.java



接下来,在 BuildConfig 文件,你应该会看到自动生成的东西就像:

  public final class BuildConfig {
public static final java.util.Date buildTime = new java.util.Date(1395794838381L);
}

所以,要访问您的应用程序中的构建日期....



  Date buildDate = BuildConfig.buildTime; 
Log.i(MyProgram,这个.apk建立在+ buildDate.toString());

(您可以格式化日期,但您喜欢使用 SimpleDateFormat 。)



希望这是有帮助的。 / p>

Is it possible to programmatially read the date when my Android apk was built? I could not find anything in the PackageInfo class.

I want to expire beta versions of my app and the easiest way would be to read out such a date and expire it after a fix preiode of days, so I don't have to update the code for that eavery time I build and deploy a beta version.

解决方案

It's been a while since this was asked, but here's how I did it using Android Studio w/gradle....

In build.gradle, each of your build types should have a buildConfigField. (This is a simplified version of the configuration- you're likely to have other stuff in here, but I wanted to show where you put it):

android {
    signingConfigs {
        buildTypes {
            debug {
                buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + System.currentTimeMillis() + "L)"
            }
            release { 
                buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + System.currentTimeMillis() + "L)"
            }
        }
    }
}    

(Note that "BuildConfigField" is the newer version of "BuildConfigLine" as of the 0.8.0 build system.)

The next time gradle does assembleRelease or assembleDebug, it should generate:

./build/source/buildConfig/releaseORdebug/com/your/project/BuildConfig.java

Next, inside that BuildConfig file, you should see something has been auto-generated like:

public final class BuildConfig {
    public static final java.util.Date buildTime = new java.util.Date(1395794838381L);
}

So, to access the build date within your app....

Date buildDate = BuildConfig.buildTime;
Log.i("MyProgram", "This .apk was built on " + buildDate.toString());

(You can format the date however you like using a SimpleDateFormat.)

Hope this is helpful.

这篇关于如何以编程方式阅读我的Android apk的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆