diff --git a/core/platform/android/libcocos2dx/axistools.gradle b/core/platform/android/libcocos2dx/axistools.gradle index 04ab8afc09..1c3edbee90 100644 --- a/core/platform/android/libcocos2dx/axistools.gradle +++ b/core/platform/android/libcocos2dx/axistools.gradle @@ -90,13 +90,41 @@ class VersionComparator implements Comparator { } class axistools { + + static String[] findNativeBuildTools(project, ndkVer = "23.2.8568313", cmakeVer = "3.22.1+") { + + // Detecting sdkRoot + def sdkRoot = null + Properties projProps = new Properties() + try { + projProps.load(project.rootProject.file("local.properties").newDataInputStream()) + sdkRoot = projProps.getProperty("sdk.dir") + } + catch(Exception ex) { + ex.printStackTrace() + } + + if (sdkRoot == null || !new File(sdkRoot).isDirectory()) { + sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}").toAbsolutePath().toString() + if (!new File(sdkRoot).isDirectory()) { + throw new Exception('android sdk not specified properly in local.properties or system env ANDROID_SDK_ROOT') + } + } + + def rets = new String[3] // ndkVer,ndkPath,cmakeVer + findNDK(sdkRoot, ndkVer, rets) + findCMake(sdkRoot, cmakeVer, rets) + return rets + } + /** * Find suitable ndk for current project * @param project The current android project * @param ndkVer The required ndk version for current project + * @param rets * @return */ - static String[] findNDK(project, ndkVer = "23.2.8568313") { + private static void findNDK(sdkRoot, ndkVer, rets) { def allowNewerNdk = null if (ndkVer.endsWith('+')) { allowNewerNdk = true @@ -104,38 +132,34 @@ class axistools { } def ndkDirs = [] - def sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}") - if (Files.exists(sdkRoot)) { - File dir = new File(sdkRoot.toAbsolutePath().toString() + '/ndk') - if (dir.isDirectory()) { - for (ndkDir in dir.listFiles()) { - ndkDirs.add(ndkDir.toString()) - } + File dir = new File("${sdkRoot}/ndk") + if (dir.isDirectory()) { + for (ndkDir in dir.listFiles()) { + ndkDirs.add(ndkDir.toString()) } } /* Find suitable ndk in dirs */ - def rets = new String[2] rets[0] = ndkVer for (ndkDir in ndkDirs) { if (findNDKInDir(ndkVer, allowNewerNdk, ndkDir, rets)) { - return rets + return } } println("No installed ndk found, the gradle will install ndk: $ndkVer automatically") rets[1] = null - return rets } /** * Find suitable cmake for current project * @param project The current android project * @param cmakeVer The required cmake version of project + * @param rets * @return */ - static String findCMake(project, String cmakeVer = "3.10.2+") { + private static void findCMake(sdkRoot, String cmakeVer, rets) { def allowNewerCMake = false if(cmakeVer.endsWith('+')) { allowNewerCMake = true @@ -145,38 +169,27 @@ class axistools { def cmakeBinDirs = [] def cmakeBinDir = null - if (project != null) { - cmakeBinDir = getCMakeBinFromProjectLocal(project) - if (cmakeBinDir != null) { - cmakeBinDirs.add(cmakeBinDir); + + def verList = [] + + // Scan installed cmake in $sdk_root/cmake + File sdkCMakeDir = new File(sdkRoot + '/cmake') + if (sdkCMakeDir.isDirectory()) { + for (cmakeDir in sdkCMakeDir.listFiles()) { + verList.add(cmakeDir.getName()) } } - def sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}") + // Sort cmake verList + verList.sort {a,b -> + return axistools.compareVersion(b, a) + } - if(Files.exists(sdkRoot)) { - def verList = [] - - // Scan installed cmake in $sdk_root/cmake - File sdkCMakeDir = new File(sdkRoot.toAbsolutePath().toString() + '/cmake') - if (sdkCMakeDir.isDirectory()) { - for (cmakeDir in sdkCMakeDir.listFiles()) { - verList.add(cmakeDir.getName()) - } - } - - // Sort cmake verList - verList.sort {a,b -> - return axistools.compareVersion(b, a) - } - - // Collect cmakeBinDirs for search - sdkRoot = sdkRoot.toAbsolutePath().toString() - for(foundVer in verList){ - cmakeBinDir = sdkRoot + File.separator + "cmake" + File.separator + foundVer + File.separator + "bin" - if(new File(cmakeBinDir).isDirectory()) { - cmakeBinDirs.add(cmakeBinDir) - } + // Collect cmakeBinDirs for search + for(foundVer in verList){ + cmakeBinDir = sdkRoot + File.separator + "cmake" + File.separator + foundVer + File.separator + "bin" + if(new File(cmakeBinDir).isDirectory()) { + cmakeBinDirs.add(cmakeBinDir) } } @@ -199,11 +212,11 @@ class axistools { foundCMakeVer = cmakeVer } - return foundCMakeVer + rets[2] = foundCMakeVer } private static int compareVersion(String ver1, String ver2) { - return new VersionComparator().compare(ver1, ver2); + return new VersionComparator().compare(ver1, ver2) } private static String findNDKInDir(ndkVer, allowNewerNdk, ndkDir, rets) { @@ -256,9 +269,9 @@ class axistools { int exitVal = proc.exitValue() if (exitVal == 0) { InputStream stdIn = proc.getInputStream(); - InputStreamReader isr = new InputStreamReader(stdIn); - BufferedReader br = new BufferedReader(isr); - String verLine = br.readLine(); + InputStreamReader isr = new InputStreamReader(stdIn) + BufferedReader br = new BufferedReader(isr) + String verLine = br.readLine() def verInfo = verLine.split("\\s") if (verInfo.length >= 3) { def foundVer = verInfo[2] @@ -288,37 +301,19 @@ class axistools { return foundCMakeVer } - private static String getCMakeBinFromProjectLocal(project) { - String programName = getCMakeProgramName(); - Properties properties = new Properties() - try { - properties.load(project.rootProject.file("local.properties")) - def cmakeDir = properties.getProperty("cmake.dir") - if(cmakeDir != null) { - def cmakeBin = "$cmakeDir/bin" - if(new File("$cmakeBin/$programName").isFile()) - return cmakeBin; - } - } - catch(Exception) { - } - - return null - } - private static String getCMakeBinFromPath() { - String cmakeExecName = getCMakeProgramName(); + String cmakeExecName = getCMakeProgramName() def foundBinPath = null Stream.of(System.getenv("PATH").split(Pattern.quote(File.pathSeparator))) .map(Paths::get) .anyMatch(path -> { - def programPath = path.resolve(cmakeExecName); + def programPath = path.resolve(cmakeExecName) boolean fileExist = Files.exists(path.resolve(cmakeExecName)) if(fileExist) { foundBinPath = path.toAbsolutePath().toString() } return fileExist - }); + }) return foundBinPath } @@ -332,7 +327,7 @@ class axistools { } private static boolean isWindows() { - return System.getProperty("os.name").toLowerCase().contains("windows"); + return System.getProperty("os.name").toLowerCase().contains("windows") } } diff --git a/templates/cpp-template-default/proj.android/app/build.gradle b/templates/cpp-template-default/proj.android/app/build.gradle index 76e6d22d2d..f3838087ee 100644 --- a/templates/cpp-template-default/proj.android/app/build.gradle +++ b/templates/cpp-template-default/proj.android/app/build.gradle @@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle" android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() - // setup ndk - def ndkInfo = axistools.findNDK(project) - ndkVersion = ndkInfo[0] - if(ndkInfo[1]) { - ndkPath = ndkInfo[1] + // Setup native build tools: ndk & cmake + def nbtInfo = axistools.findNativeBuildTools(project) + ndkVersion = nbtInfo[0] + if(nbtInfo[1]) { + ndkPath = nbtInfo[1] } - - // setup cmake - def cmakeVer = axistools.findCMake(project) + def cmakeVer = nbtInfo[2] defaultConfig { applicationId "org.cocos2dx.hellocpp" diff --git a/templates/lua-template-default/frameworks/runtime-src/proj.android/app/build.gradle b/templates/lua-template-default/frameworks/runtime-src/proj.android/app/build.gradle index 8b8aee927d..1805769c1a 100644 --- a/templates/lua-template-default/frameworks/runtime-src/proj.android/app/build.gradle +++ b/templates/lua-template-default/frameworks/runtime-src/proj.android/app/build.gradle @@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle" android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() - // setup ndk - def ndkInfo = axistools.findNDK(project) - ndkVersion = ndkInfo[0] - if(ndkInfo[1]) { - ndkPath = ndkInfo[1] + // Setup native build tools: ndk & cmake + def nbtInfo = axistools.findNativeBuildTools(project) + ndkVersion = nbtInfo[0] + if(nbtInfo[1]) { + ndkPath = nbtInfo[1] } - - // setup cmake - def cmakeVer = axistools.findCMake(project) + def cmakeVer = nbtInfo[2] defaultConfig { applicationId "org.cocos2dx.hellolua" diff --git a/tests/cpp-tests/proj.android/app/build.gradle b/tests/cpp-tests/proj.android/app/build.gradle index aad5879bc9..863b387435 100644 --- a/tests/cpp-tests/proj.android/app/build.gradle +++ b/tests/cpp-tests/proj.android/app/build.gradle @@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle" android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() - // setup ndk - def ndkInfo = axistools.findNDK(project) - ndkVersion = ndkInfo[0] - if(ndkInfo[1]) { - ndkPath = ndkInfo[1] + // Setup native build tools: ndk & cmake + def nbtInfo = axistools.findNativeBuildTools(project) + ndkVersion = nbtInfo[0] + if(nbtInfo[1]) { + ndkPath = nbtInfo[1] } - - // setup cmake - def cmakeVer = axistools.findCMake(project) + def cmakeVer = nbtInfo[2] defaultConfig { applicationId "org.cocos2dx.cpp_tests" diff --git a/tests/fairygui-tests/proj.android/app/build.gradle b/tests/fairygui-tests/proj.android/app/build.gradle index 5fa87b8d6d..b268411b78 100644 --- a/tests/fairygui-tests/proj.android/app/build.gradle +++ b/tests/fairygui-tests/proj.android/app/build.gradle @@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle" android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() - // setup ndk - def ndkInfo = axistools.findNDK(project) - ndkVersion = ndkInfo[0] - if(ndkInfo[1]) { - ndkPath = ndkInfo[1] + // Setup native build tools: ndk & cmake + def nbtInfo = axistools.findNativeBuildTools(project) + ndkVersion = nbtInfo[0] + if(nbtInfo[1]) { + ndkPath = nbtInfo[1] } - - // setup cmake - def cmakeVer = axistools.findCMake(project) + def cmakeVer = nbtInfo[2] defaultConfig { applicationId "org.cocos2dx.fairygui_tests" diff --git a/tests/live2d-tests/proj.android/app/build.gradle b/tests/live2d-tests/proj.android/app/build.gradle index 040cbcbb85..3292d29a40 100644 --- a/tests/live2d-tests/proj.android/app/build.gradle +++ b/tests/live2d-tests/proj.android/app/build.gradle @@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle" android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() - // setup ndk - def ndkInfo = axistools.findNDK(project) - ndkVersion = ndkInfo[0] - if(ndkInfo[1]) { - ndkPath = ndkInfo[1] + // Setup native build tools: ndk & cmake + def nbtInfo = axistools.findNativeBuildTools(project) + ndkVersion = nbtInfo[0] + if(nbtInfo[1]) { + ndkPath = nbtInfo[1] } - - // setup cmake - def cmakeVer = axistools.findCMake(project) + def cmakeVer = nbtInfo[2] defaultConfig { applicationId "org.cocos2dx.live2d_tests" diff --git a/tests/lua-tests/project/proj.android/app/build.gradle b/tests/lua-tests/project/proj.android/app/build.gradle index 193947afb0..a8e875bbd3 100644 --- a/tests/lua-tests/project/proj.android/app/build.gradle +++ b/tests/lua-tests/project/proj.android/app/build.gradle @@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle" android { compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() - // setup ndk - def ndkInfo = axistools.findNDK(project) - ndkVersion = ndkInfo[0] - if(ndkInfo[1]) { - ndkPath = ndkInfo[1] + // Setup native build tools: ndk & cmake + def nbtInfo = axistools.findNativeBuildTools(project) + ndkVersion = nbtInfo[0] + if(nbtInfo[1]) { + ndkPath = nbtInfo[1] } - - // setup cmake - def cmakeVer = axistools.findCMake(project) + def cmakeVer = nbtInfo[2] defaultConfig { applicationId "org.cocos2dx.lua_tests"