如何以编程方式读取 Android apk 的构建日期?
是否可以以编程方式读取我的 Android apk 的构建日期?我在 PackageInfo 类中找不到任何内容。
我想让我的应用程序的测试版过期,最简单的方法是读出这样的日期并在固定的天数后使其过期,因此我不必每次构建和部署应用程序时都更新代码测试版。
Is it possible to programmatically 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 fixed period of days, so I don't have to update the code for that every time I build and deploy a beta version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
12/2/22 注意:与我之前建议的相比,这是一种将时间戳放入
BuildConfig.java
的更新方法,但我将旧版说明保留在底部,以供任何可能的人使用想要它。这些说明适用于相对较新的build.gradle.kts
格式。首先,更新
build.gradle
以包括:以上内容是
所必需的从 Android Studio Flamingo Canary 9 开始,buildConfigField
功能可能已默认禁用。请参阅 此处了解更多相关信息。接下来,添加到
build.gradle.kts
的顶部以及
build.config.kts
的android { ... }
部分之外> 添加此内容:与前面的说明一样,在 Kotlin 中可以这样访问构建时间戳:
就是这样。如果您有旧版本的 Android Studio/Gradle 并且想要更旧的、更面向 Java 的说明,请继续阅读。
注意 2020 年 6 月 1 日:Android Studio 4.1 Canary 10 w/Gradle >= 6.5 已更新说明。如果您看到“
不支持的 BuildConfig 类型:java.util.Date
错误,请向下滚动以查找修复程序。[旧说明]
在
build.gradle
中,每个构建类型应该有一个 < code>buildConfigField。(这是配置的简化版本 - 您可能在此处包含其他内容,但我想展示您放置它的位置):(请注意“
BuildConfigField” 是“
BuildConfigLine 的新版本 code>" 从 0.8.0 构建系统开始。)
下次 gradle 进行 assembleRelease 或 assembleDebug 时,它应该生成:
./build/source/buildConfig/releaseORdebug/com/your/project/BuildConfig.java
接下来,在
BuildConfig
文件中,您应该会看到自动生成的内容,例如:So, to access the build date inside your app....
(您可以根据需要设置日期格式)使用 SimpleDateFormat。)
更新 6/1/2020< /strong> -- 在 Android Studio 4.1 Canary 10 w/Gradle 6.5 中,上述解决方案会导致“
不支持的 BuildConfig 类型:java.util.Date
”错误。 应使用与上述内容略有不同的:更新 7/30/2020 在 gradle 6.6-rc4 后,您需要通过更改为以下行来包含当时的引号:
Inside在
BuildConfig
文件中,您应该看到现在已经自动生成了一些内容,例如:现在,要访问应用程序中的构建日期...
和以前一样,您可以使用您喜欢的方式格式化日期SimpleDateFormat。
希望这有帮助。
12/2/22 Note: This is a newer way to put the timestamp into the
BuildConfig.java
than I suggested previously, but I am keeping the legacy instructions at the bottom for anyone who might want it. The instructions are for the relatively newbuild.gradle.kts
format.First, update
build.gradle
to include:The above is needed as the
buildConfigField
feature may have been disabled by default, starting with Android Studio Flamingo Canary 9. See here for more about that.Next, add to the top of
build.gradle.kts
:and outside of the
android { ... }
part ofbuild.config.kts
add this:As with the previous instructions, the build timestamp is accessed in Kotlin like this:
That's it. Keep reading if you've got an older versions of Android Studio/Gradle and want the older more Java-oriented instructions.
Note 6/1/2020: Android Studio 4.1 Canary 10 w/Gradle >= 6.5 has updated instructions. If you are seeing an "
Unsupported BuildConfig type : java.util.Date
error, scroll down for the fix.[Older Instructions]
In
build.gradle
, each of your build types should have abuildConfigField
. (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):(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:So, to access the build date within your app....
(You can format the date however you like using a SimpleDateFormat.)
Update 6/1/2020 -- In Android Studio 4.1 Canary 10 w/Gradle 6.5, the above solution results in an "
Unsupported BuildConfig type : java.util.Date
" error. A slight variation to the above should be used instead:Update 7/30/2020 After gradle 6.6-rc4, you need to include the enclosing quotes for the time by changing to this line:
Inside the
BuildConfig
file, you should see something has now been auto-generated like:So now, to access the build date within your app....
As before, you can format the date however you like using a SimpleDateFormat.
Hope this is helpful.