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

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

问题描述

是否可以以编程方式读取我的 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.

我想让我的应用程序的 beta 版本过期,最简单的方法是读出这样的日期并在修复前几天后过期,所以我不必每次构建时都更新代码并部署测试版.

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.

推荐答案

注意 6/1/2020:Android Studio 4.1 Canary 10 w/Gradle >= 6.5 已更新说明.如果您看到Unsupported BuildConfig type : java.util.Date 错误,请向下滚动以进行修复.

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.

[旧说明]

build.gradle 中,您的每个 构建类型 应该有一个 buildConfigField.(这是配置的简化版本 - 您可能会在此处添加其他内容,但我想展示您将其放置的位置):

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", "BUILD_TIME", "new java.util.Date(" + System.currentTimeMillis() + "L)"
            }
            release { 
                buildConfigField "java.util.Date", "BUILD_TIME", "new java.util.Date(" + System.currentTimeMillis() + "L)"
            }
        }
    }
}    

(请注意,BuildConfigField"是BuildConfigLine"的>较新版本,从 0.8.0 构建系统开始.)

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

下次 gradle 执行 assembleRelease 或 assembleDebug 时,它应该生成:

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

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

接下来,在 BuildConfig 文件中,您应该会看到一些已自动生成的内容,例如:

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

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

因此,要在您的应用中访问构建日期....

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

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

(您可以使用 SimpleDateFormat 随意格式化日期.)

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

更新 6/1/2020 -- 在 Android Studio 4.1 Canary 10 w/Gradle 6.5 中,上述解决方案导致不支持的 BuildConfig 类型:java.util.Date"错误.应使用与上述略有不同的代替:

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:

android {
    signingConfigs {
        buildTypes {
            debug {
                buildConfigField("String", "BUILD_TIME", System.currentTimeMillis().toString())
            }
            release { 
                buildConfigField("String", "BUILD_TIME", System.currentTimeMillis().toString())
            }
        }
    }
} 

更新 7/30/2020 在 gradle 6.6-rc4 之后,您需要通过更改为这一行来包含时间的封闭引号:

Update 7/30/2020 After gradle 6.6-rc4, you need to include the enclosing quotes for the time by changing to this line:

        buildConfigField("String", "BUILD_TIME", "\"" + System.currentTimeMillis().toString() + "\"")

BuildConfig 文件中,您应该会看到一些已经自动生成的内容,例如:

Inside the BuildConfig file, you should see something has now been auto-generated like:

public final class BuildConfig {
      public static final String BUILD_TIME = "1590735284503";
}

现在,要在您的应用中访问构建日期....

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

private Date buildDate = new Date(Long.parseLong(BuildConfig.BUILD_TIME));
Log.i("MyProgram", "This .apk was built on " + buildDate.toString());

和以前一样,您可以使用 SimpleDateFormat.

As before, you can format the date however you like using a SimpleDateFormat.

希望对您有所帮助.

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

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