是否有可能programmatially读取日期时,我的Android APK建?我找不到在PackageInfo类东西。
Is it possible to programmatially read the date when my Android apk was built? I could not find anything in the PackageInfo class.
我想终止我的应用程序以及最简单的方法的测试版将读出这样一个日期后几天之内修复$ P $皮奥德到期,所以我没有更新code对于eavery一次我构建和部署一个测试版。
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.
这已经有一段时间,因为这是问,但这里是我做到了采用Android工作室瓦特/摇篮......
It's been a while since this was asked, but here's how I did it using Android Studio w/gradle....
首先, build.gradle
,您可以添加这样的:
First, in build.gradle
, you add something like:
def getDateAsMillis() {
def Calendar cal = Calendar.getInstance();
return cal.getTimeInMillis().toString()
}
随后,同样在 build.gradle
,您的每一个的生成类型应确保有一个 buildConfigField
。 (这是你很可能有其他的东西在这里配置 - 的简化版本,但我想表现,你把它):
Then, also in build.gradle
, each of your build types should make sure to 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(" + getDateAsMillis() + "L)"
}
release {
buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + getDateAsMillis() + "L)"
}
}
}
}
请注意是 BuildConfigField
是新版本 BuildConfigLine
为0.8.0版本系统。下一次摇篮并assembleRelease或assembleDebug,它应该产生:
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:
./编译/来源/ buildConfig / releaseORdebug / COM /你/项目/ BuildConfig.java
里面的 BuildConfig
文件,你应该看到的东西已经被自动生成的,如:
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());
(你可以按你喜欢使用格式化日期的SimpleDateFormat.)
希望这是有帮助的。