Skip to content

Commit

Permalink
fix: Modified the way to read the Java version in build.gradle. (#1157)
Browse files Browse the repository at this point in the history
Signed-off-by: Caijinglong <[email protected]>
  • Loading branch information
CaiJingLong authored Jul 5, 2024
1 parent 72a488e commit fe4fa9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To know more about breaking changes, see the [Migration Guide][].
### Fixes

* Fixes compile exceptions with Xcode versions that are not compatible with iOS 17.0.
* Modified the way to read the Java version in `build.gradle`.

## 3.2.0

Expand Down
84 changes: 17 additions & 67 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,82 +30,32 @@ void log(Object msg) {
logger.log(LogLevel.INFO, "[PhotoManager] $msg")
}

class ExecResult {
int exitValue
String output

ExecResult(int exitValue, String output) {
this.exitValue = exitValue
this.output = output
}
}

static ExecResult executeCommand(String... args) {

int exitValue;
String output;

try {
Process process = new ProcessBuilder(args).start()

def stream = new ByteArrayOutputStream()
stream.withStream { bos ->
process.inputStream.withStream { ins ->
bos << ins
}
process.errorStream.withStream { ins ->
bos << ins
}
output = bos.toString("UTF-8")
}
exitValue = process.waitFor()
} catch (Exception e) {
e.printStackTrace()
exitValue = -1
output = e.toString()
}

return new ExecResult(exitValue, output)
}

/**
* Get java version from JAVA_HOME
* @param path JAVA_HOME path
* @return java version
*/
JavaVersion getJavaFromHome(String path) {
log("Reading java version from JAVA_HOME: ${path}")
def javaExe = path + "/bin/java"

def version = executeCommand(javaExe, "-version")

if (version.exitValue != 0) {
return null
}

def versionText = version.output
log("versionText: ${versionText}")

String regex = "\"(.+)\""
Pattern pattern = Pattern.compile(regex)

// 创建 Matcher 对象
Matcher matcher = pattern.matcher(versionText)

if (matcher.find()) {
String v = matcher.group(1)

if (v.startsWith("1.")) {
// just save the major and minor version
v = v.substring(0, 3)
def releaseFile = new File(path, "release")

if (releaseFile.exists()) {
def pattern = Pattern.compile("JAVA_VERSION=\"(\\d+)\"")
def matcher = pattern.matcher(releaseFile.text)
if (matcher.find()) {
def version = matcher.group(1)
log("Get java version from release file: $version")
return JavaVersion.toVersion(version)
}
} else {
def javaBin = new File("${path}/bin/java")
if (javaBin.exists()) {
return JavaVersion.VERSION_1_8
} else if (new File("${path}/bin/java.exe").exists()) {
return JavaVersion.VERSION_1_8
}

log("java version string: ${v}")

return JavaVersion.toVersion(v)
return null
}

return null
}

JavaVersion getJavaVersion() {
Expand Down

0 comments on commit fe4fa9c

Please sign in to comment.