- 在AndroidManifest文件的application标签下设置一个占位符,如:
<meta-data
android:name="releaseTime"
android:value="${RELEASE_TIME}">
</meta-data>
- 然后在gradle打包时,获取时间,设置这个值,如下:
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
buildType {
release {
...
manifestPlaceholders = [RELEASE_TIME: releaseTime()]
}
}
- 在java代码中读取时间
String releaseTime = "";
try {
releaseTime = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData.getString("releaseTime");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
来自-https://segmentfault.com/q/1010000007032862 回答,并修改其中一个错误。