Improve android gradle cmake & ndk setup

This commit is contained in:
halx99 2022-08-06 23:09:16 +08:00
parent e31640ca7b
commit f03bce1380
7 changed files with 98 additions and 115 deletions

View File

@ -90,13 +90,41 @@ class VersionComparator implements Comparator<String> {
} }
class axistools { 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 * Find suitable ndk for current project
* @param project The current android project * @param project The current android project
* @param ndkVer The required ndk version for current project * @param ndkVer The required ndk version for current project
* @param rets
* @return * @return
*/ */
static String[] findNDK(project, ndkVer = "23.2.8568313") { private static void findNDK(sdkRoot, ndkVer, rets) {
def allowNewerNdk = null def allowNewerNdk = null
if (ndkVer.endsWith('+')) { if (ndkVer.endsWith('+')) {
allowNewerNdk = true allowNewerNdk = true
@ -104,38 +132,34 @@ class axistools {
} }
def ndkDirs = [] def ndkDirs = []
def sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}") File dir = new File("${sdkRoot}/ndk")
if (Files.exists(sdkRoot)) {
File dir = new File(sdkRoot.toAbsolutePath().toString() + '/ndk')
if (dir.isDirectory()) { if (dir.isDirectory()) {
for (ndkDir in dir.listFiles()) { for (ndkDir in dir.listFiles()) {
ndkDirs.add(ndkDir.toString()) ndkDirs.add(ndkDir.toString())
} }
} }
}
/* Find suitable ndk in dirs */ /* Find suitable ndk in dirs */
def rets = new String[2]
rets[0] = ndkVer rets[0] = ndkVer
for (ndkDir in ndkDirs) { for (ndkDir in ndkDirs) {
if (findNDKInDir(ndkVer, allowNewerNdk, ndkDir, rets)) { if (findNDKInDir(ndkVer, allowNewerNdk, ndkDir, rets)) {
return rets return
} }
} }
println("No installed ndk found, the gradle will install ndk: $ndkVer automatically") println("No installed ndk found, the gradle will install ndk: $ndkVer automatically")
rets[1] = null rets[1] = null
return rets
} }
/** /**
* Find suitable cmake for current project * Find suitable cmake for current project
* @param project The current android project * @param project The current android project
* @param cmakeVer The required cmake version of project * @param cmakeVer The required cmake version of project
* @param rets
* @return * @return
*/ */
static String findCMake(project, String cmakeVer = "3.10.2+") { private static void findCMake(sdkRoot, String cmakeVer, rets) {
def allowNewerCMake = false def allowNewerCMake = false
if(cmakeVer.endsWith('+')) { if(cmakeVer.endsWith('+')) {
allowNewerCMake = true allowNewerCMake = true
@ -145,20 +169,11 @@ class axistools {
def cmakeBinDirs = [] def cmakeBinDirs = []
def cmakeBinDir = null def cmakeBinDir = null
if (project != null) {
cmakeBinDir = getCMakeBinFromProjectLocal(project)
if (cmakeBinDir != null) {
cmakeBinDirs.add(cmakeBinDir);
}
}
def sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}")
if(Files.exists(sdkRoot)) {
def verList = [] def verList = []
// Scan installed cmake in $sdk_root/cmake // Scan installed cmake in $sdk_root/cmake
File sdkCMakeDir = new File(sdkRoot.toAbsolutePath().toString() + '/cmake') File sdkCMakeDir = new File(sdkRoot + '/cmake')
if (sdkCMakeDir.isDirectory()) { if (sdkCMakeDir.isDirectory()) {
for (cmakeDir in sdkCMakeDir.listFiles()) { for (cmakeDir in sdkCMakeDir.listFiles()) {
verList.add(cmakeDir.getName()) verList.add(cmakeDir.getName())
@ -171,14 +186,12 @@ class axistools {
} }
// Collect cmakeBinDirs for search // Collect cmakeBinDirs for search
sdkRoot = sdkRoot.toAbsolutePath().toString()
for(foundVer in verList){ for(foundVer in verList){
cmakeBinDir = sdkRoot + File.separator + "cmake" + File.separator + foundVer + File.separator + "bin" cmakeBinDir = sdkRoot + File.separator + "cmake" + File.separator + foundVer + File.separator + "bin"
if(new File(cmakeBinDir).isDirectory()) { if(new File(cmakeBinDir).isDirectory()) {
cmakeBinDirs.add(cmakeBinDir) cmakeBinDirs.add(cmakeBinDir)
} }
} }
}
cmakeBinDir = getCMakeBinFromPath() cmakeBinDir = getCMakeBinFromPath()
if(cmakeBinDir != null) { if(cmakeBinDir != null) {
@ -199,11 +212,11 @@ class axistools {
foundCMakeVer = cmakeVer foundCMakeVer = cmakeVer
} }
return foundCMakeVer rets[2] = foundCMakeVer
} }
private static int compareVersion(String ver1, String ver2) { 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) { private static String findNDKInDir(ndkVer, allowNewerNdk, ndkDir, rets) {
@ -256,9 +269,9 @@ class axistools {
int exitVal = proc.exitValue() int exitVal = proc.exitValue()
if (exitVal == 0) { if (exitVal == 0) {
InputStream stdIn = proc.getInputStream(); InputStream stdIn = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdIn); InputStreamReader isr = new InputStreamReader(stdIn)
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr)
String verLine = br.readLine(); String verLine = br.readLine()
def verInfo = verLine.split("\\s") def verInfo = verLine.split("\\s")
if (verInfo.length >= 3) { if (verInfo.length >= 3) {
def foundVer = verInfo[2] def foundVer = verInfo[2]
@ -288,37 +301,19 @@ class axistools {
return foundCMakeVer 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() { private static String getCMakeBinFromPath() {
String cmakeExecName = getCMakeProgramName(); String cmakeExecName = getCMakeProgramName()
def foundBinPath = null def foundBinPath = null
Stream.of(System.getenv("PATH").split(Pattern.quote(File.pathSeparator))) Stream.of(System.getenv("PATH").split(Pattern.quote(File.pathSeparator)))
.map(Paths::get) .map(Paths::get)
.anyMatch(path -> { .anyMatch(path -> {
def programPath = path.resolve(cmakeExecName); def programPath = path.resolve(cmakeExecName)
boolean fileExist = Files.exists(path.resolve(cmakeExecName)) boolean fileExist = Files.exists(path.resolve(cmakeExecName))
if(fileExist) { if(fileExist) {
foundBinPath = path.toAbsolutePath().toString() foundBinPath = path.toAbsolutePath().toString()
} }
return fileExist return fileExist
}); })
return foundBinPath return foundBinPath
} }
@ -332,7 +327,7 @@ class axistools {
} }
private static boolean isWindows() { private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows"); return System.getProperty("os.name").toLowerCase().contains("windows")
} }
} }

View File

@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
android { android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
// setup ndk // Setup native build tools: ndk & cmake
def ndkInfo = axistools.findNDK(project) def nbtInfo = axistools.findNativeBuildTools(project)
ndkVersion = ndkInfo[0] ndkVersion = nbtInfo[0]
if(ndkInfo[1]) { if(nbtInfo[1]) {
ndkPath = ndkInfo[1] ndkPath = nbtInfo[1]
} }
def cmakeVer = nbtInfo[2]
// setup cmake
def cmakeVer = axistools.findCMake(project)
defaultConfig { defaultConfig {
applicationId "org.cocos2dx.hellocpp" applicationId "org.cocos2dx.hellocpp"

View File

@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
android { android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
// setup ndk // Setup native build tools: ndk & cmake
def ndkInfo = axistools.findNDK(project) def nbtInfo = axistools.findNativeBuildTools(project)
ndkVersion = ndkInfo[0] ndkVersion = nbtInfo[0]
if(ndkInfo[1]) { if(nbtInfo[1]) {
ndkPath = ndkInfo[1] ndkPath = nbtInfo[1]
} }
def cmakeVer = nbtInfo[2]
// setup cmake
def cmakeVer = axistools.findCMake(project)
defaultConfig { defaultConfig {
applicationId "org.cocos2dx.hellolua" applicationId "org.cocos2dx.hellolua"

View File

@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
android { android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
// setup ndk // Setup native build tools: ndk & cmake
def ndkInfo = axistools.findNDK(project) def nbtInfo = axistools.findNativeBuildTools(project)
ndkVersion = ndkInfo[0] ndkVersion = nbtInfo[0]
if(ndkInfo[1]) { if(nbtInfo[1]) {
ndkPath = ndkInfo[1] ndkPath = nbtInfo[1]
} }
def cmakeVer = nbtInfo[2]
// setup cmake
def cmakeVer = axistools.findCMake(project)
defaultConfig { defaultConfig {
applicationId "org.cocos2dx.cpp_tests" applicationId "org.cocos2dx.cpp_tests"

View File

@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
android { android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
// setup ndk // Setup native build tools: ndk & cmake
def ndkInfo = axistools.findNDK(project) def nbtInfo = axistools.findNativeBuildTools(project)
ndkVersion = ndkInfo[0] ndkVersion = nbtInfo[0]
if(ndkInfo[1]) { if(nbtInfo[1]) {
ndkPath = ndkInfo[1] ndkPath = nbtInfo[1]
} }
def cmakeVer = nbtInfo[2]
// setup cmake
def cmakeVer = axistools.findCMake(project)
defaultConfig { defaultConfig {
applicationId "org.cocos2dx.fairygui_tests" applicationId "org.cocos2dx.fairygui_tests"

View File

@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
android { android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
// setup ndk // Setup native build tools: ndk & cmake
def ndkInfo = axistools.findNDK(project) def nbtInfo = axistools.findNativeBuildTools(project)
ndkVersion = ndkInfo[0] ndkVersion = nbtInfo[0]
if(ndkInfo[1]) { if(nbtInfo[1]) {
ndkPath = ndkInfo[1] ndkPath = nbtInfo[1]
} }
def cmakeVer = nbtInfo[2]
// setup cmake
def cmakeVer = axistools.findCMake(project)
defaultConfig { defaultConfig {
applicationId "org.cocos2dx.live2d_tests" applicationId "org.cocos2dx.live2d_tests"

View File

@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
android { android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
// setup ndk // Setup native build tools: ndk & cmake
def ndkInfo = axistools.findNDK(project) def nbtInfo = axistools.findNativeBuildTools(project)
ndkVersion = ndkInfo[0] ndkVersion = nbtInfo[0]
if(ndkInfo[1]) { if(nbtInfo[1]) {
ndkPath = ndkInfo[1] ndkPath = nbtInfo[1]
} }
def cmakeVer = nbtInfo[2]
// setup cmake
def cmakeVer = axistools.findCMake(project)
defaultConfig { defaultConfig {
applicationId "org.cocos2dx.lua_tests" applicationId "org.cocos2dx.lua_tests"