mirror of https://github.com/axmolengine/axmol.git
Improve android gradle cmake & ndk setup
This commit is contained in:
parent
1009b29a06
commit
93550405bc
|
@ -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)) {
|
if (dir.isDirectory()) {
|
||||||
File dir = new File(sdkRoot.toAbsolutePath().toString() + '/ndk')
|
for (ndkDir in dir.listFiles()) {
|
||||||
if (dir.isDirectory()) {
|
ndkDirs.add(ndkDir.toString())
|
||||||
for (ndkDir in dir.listFiles()) {
|
|
||||||
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,38 +169,27 @@ class axistools {
|
||||||
def cmakeBinDirs = []
|
def cmakeBinDirs = []
|
||||||
|
|
||||||
def cmakeBinDir = null
|
def cmakeBinDir = null
|
||||||
if (project != null) {
|
|
||||||
cmakeBinDir = getCMakeBinFromProjectLocal(project)
|
def verList = []
|
||||||
if (cmakeBinDir != null) {
|
|
||||||
cmakeBinDirs.add(cmakeBinDir);
|
// 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)) {
|
// Collect cmakeBinDirs for search
|
||||||
def verList = []
|
for(foundVer in verList){
|
||||||
|
cmakeBinDir = sdkRoot + File.separator + "cmake" + File.separator + foundVer + File.separator + "bin"
|
||||||
// Scan installed cmake in $sdk_root/cmake
|
if(new File(cmakeBinDir).isDirectory()) {
|
||||||
File sdkCMakeDir = new File(sdkRoot.toAbsolutePath().toString() + '/cmake')
|
cmakeBinDirs.add(cmakeBinDir)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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"
|
||||||
|
|
Loading…
Reference in New Issue