是否可以在运行时检索改造的版本?我想在调试屏幕中显示此信息。
最佳答案
免责声明:此答案假设您使用 Gradle 来构建 Android 项目。另外,我不是 Gradle 专家,因此这种方法可能存在我不知道的缺点。
虽然没有明显的方法在代码中执行此操作,但您可以使用 Gradle 在 BuildConfig
中创建一个简单的字段。考虑以下因素:
/**
* Adds a public static String field to BuildConfig containing
* the Retrofit version, no matter what build type is used.
*/
task createRetrofitBuildConfigField << {
project.configurations.compile.each {
if (it.name.contains("retrofit")) {
android.buildTypes.each { flavor ->
def version = it.name.replaceFirst(~/\.[^\.]+$/, "") // Removes extension.
flavor.buildConfigField("String", "RETROFIT_VERSION", "\"${version}\"")
}
}
}
}
preBuild.dependsOn createRetrofitBuildConfigField
当您重建项目时,一个静态字段会添加到 BuildConfig
中,可以在代码中轻松访问:
public static final String RETROFIT_VERSION = "retrofit-2.0.0-beta2";
您可能希望将上述代码添加到声明 Retrofit 依赖项的 build.gradle
中。
关于java - 如何在运行时确定retrofit的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33569852/