如何以编程方式读取 Android apk 的构建日期?

发布于 2024-09-15 02:26:42 字数 141 浏览 3 评论 0原文

是否可以以编程方式读取我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-09-22 02:26:42

12/2/22 注意:与我之前建议的相比,这是一种将时间戳放入 BuildConfig.java 的更新方法,但我将旧版说明保留在底部,以供任何可能的人使用想要它。这些说明适用于相对较新的 build.gradle.kts 格式。

首先,更新 build.gradle 以包括:

android {
    buildFeatures {
        buildConfig = true
    }
}

以上内容是 所必需的从 Android Studio Flamingo Canary 9 开始,buildConfigField 功能可能已默认禁用。请参阅 此处了解更多相关信息。

接下来,添加到 build.gradle.kts 的顶部

 import com.android.build.api.variant.BuildConfigField

以及 build.config.ktsandroid { ... } 部分之外> 添加此内容:

androidComponents {
    onVariants {
       it.buildConfigFields.put(
            "BUILD_TIME", BuildConfigField(
                "String", "\"" + System.currentTimeMillis().toString() + "\"", "build timestamp"
            )
        )
    }
}

与前面的说明一样,在 Kotlin 中可以这样访问构建时间戳:

private val buildDate = Date(BuildConfig.BUILD_TIME.toLong())
Log.i("MyProgram", "This .apk was built on ${buildDate.toString()}");

就是这样。如果您有旧版本的 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。(这是配置的简化版本 - 您可能在此处包含其他内容,但我想展示您放置它的位置):(

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 的新版本 code>" 从 0.8.0 构建系统开始。)

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

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

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

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

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

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

(您可以根据需要设置日期格式)使用 SimpleDateFormat。)

更新 6/1/2020< /strong> -- 在 Android Studio 4.1 Canary 10 w/Gradle 6.5 中,上述解决方案会导致“不支持的 BuildConfig 类型:java.util.Date”错误。 应使用与上述内容略有不同的

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 后,您需要通过更改为以下行来包含当时的引号:

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

Inside在 BuildConfig 文件中,您应该看到现在已经自动生成了一些内容,例如:

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

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

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

和以前一样,您可以使用您喜欢的方式格式化日期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 new build.gradle.kts format.

First, update build.gradle to include:

android {
    buildFeatures {
        buildConfig = true
    }
}

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:

 import com.android.build.api.variant.BuildConfigField

and outside of the android { ... } part of build.config.kts add this:

androidComponents {
    onVariants {
       it.buildConfigFields.put(
            "BUILD_TIME", BuildConfigField(
                "String", "\"" + System.currentTimeMillis().toString() + "\"", "build timestamp"
            )
        )
    }
}

As with the previous instructions, the build timestamp is accessed in Kotlin like this:

private val buildDate = Date(BuildConfig.BUILD_TIME.toLong())
Log.i("MyProgram", "This .apk was built on ${buildDate.toString()}");

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 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)"
            }
        }
    }
}    

(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 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());

(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:

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

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() + "\"")

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());

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

Hope this is helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文