Avoid hard coding in NativeLibrary.getMajorVersionNumber

This commit is contained in:
sharkdtu 2019-11-20 21:48:46 +08:00
parent 2ffb2fbed3
commit deb4d14153
2 changed files with 41 additions and 11 deletions

View File

@ -5,6 +5,7 @@ load(":build_defs.bzl", "JAVACOPTS")
load(":src/gen/gen_ops.bzl", "tf_java_op_gen_srcjar")
load(
"//tensorflow:tensorflow.bzl",
"VERSION",
"tf_binary_additional_srcs",
"tf_cc_binary",
"tf_cc_test",
@ -27,9 +28,26 @@ java_library(
data = tf_binary_additional_srcs() + [":libtensorflow_jni"],
javacopts = JAVACOPTS,
plugins = [":processor"],
resources = [":java_resources"],
visibility = ["//visibility:public"],
)
genrule(
name = "version-info",
outs = ["src/main/resources/tensorflow-version-info"],
cmd = "echo version=%s > $@" % VERSION,
output_to_bindir = 1,
)
filegroup(
name = "java_resources",
srcs = [":version-info"],
visibility = [
"//tensorflow/contrib/android:__pkg__",
"//tensorflow/java:__pkg__",
],
)
# NOTE(ashankar): Rule to include the Java API in the Android Inference Library
# .aar. At some point, might make sense for a .aar rule here instead.
filegroup(

View File

@ -19,6 +19,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Helper class for loading the TensorFlow Java native library.
@ -169,19 +170,30 @@ final class NativeLibrary {
* determined.
*/
private static String getMajorVersionNumber() {
// getImplementationVersion() retrun null.
String version = NativeLibrary.class.getPackage().getImplementationVersion();
// expecting a string like 1.14.0, we want to get the first '1'.
int dotIndex;
if (version == null || (dotIndex = version.indexOf('.')) == -1) {
// we want to get the version 1.
return "1";
InputStream resourceStream = NativeLibrary.class.getClassLoader()
.getResourceAsStream("tensorflow-version-info");
if (resourceStream == null) {
return null;
}
String majorVersion = version.substring(0, dotIndex);
try {
Integer.parseInt(majorVersion);
return majorVersion;
} catch (NumberFormatException unused) {
Properties props = new Properties();
props.load(resourceStream);
String version = props.getProperty("version");
// expecting a string like 1.14.0, we want to get the first '1'.
int dotIndex;
if (version == null || (dotIndex = version.indexOf('.')) == -1) {
return null;
}
String majorVersion = version.substring(0, dotIndex);
try {
Integer.parseInt(majorVersion);
return majorVersion;
} catch (NumberFormatException unused) {
return null;
}
} catch (IOException e) {
log("failed to load tensorflow version info.");
return null;
}
}