Skip to content

Commit f03bce1

Browse files
committed
Improve android gradle cmake & ndk setup
1 parent e31640c commit f03bce1

File tree

7 files changed

+97
-114
lines changed

7 files changed

+97
-114
lines changed

core/platform/android/libcocos2dx/axistools.gradle

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -90,52 +90,76 @@ class VersionComparator implements Comparator<String> {
9090
}
9191

9292
class axistools {
93+
94+
static String[] findNativeBuildTools(project, ndkVer = "23.2.8568313", cmakeVer = "3.22.1+") {
95+
96+
// Detecting sdkRoot
97+
def sdkRoot = null
98+
Properties projProps = new Properties()
99+
try {
100+
projProps.load(project.rootProject.file("local.properties").newDataInputStream())
101+
sdkRoot = projProps.getProperty("sdk.dir")
102+
}
103+
catch(Exception ex) {
104+
ex.printStackTrace()
105+
}
106+
107+
if (sdkRoot == null || !new File(sdkRoot).isDirectory()) {
108+
sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}").toAbsolutePath().toString()
109+
if (!new File(sdkRoot).isDirectory()) {
110+
throw new Exception('android sdk not specified properly in local.properties or system env ANDROID_SDK_ROOT')
111+
}
112+
}
113+
114+
def rets = new String[3] // ndkVer,ndkPath,cmakeVer
115+
findNDK(sdkRoot, ndkVer, rets)
116+
findCMake(sdkRoot, cmakeVer, rets)
117+
return rets
118+
}
119+
93120
/**
94121
* Find suitable ndk for current project
95122
* @param project The current android project
96123
* @param ndkVer The required ndk version for current project
124+
* @param rets
97125
* @return
98126
*/
99-
static String[] findNDK(project, ndkVer = "23.2.8568313") {
127+
private static void findNDK(sdkRoot, ndkVer, rets) {
100128
def allowNewerNdk = null
101129
if (ndkVer.endsWith('+')) {
102130
allowNewerNdk = true
103131
ndkVer = ndkVer.substring(0, ndkVer.length() - 1)
104132
}
105133

106134
def ndkDirs = []
107-
def sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}")
108-
if (Files.exists(sdkRoot)) {
109-
File dir = new File(sdkRoot.toAbsolutePath().toString() + '/ndk')
110-
if (dir.isDirectory()) {
111-
for (ndkDir in dir.listFiles()) {
112-
ndkDirs.add(ndkDir.toString())
113-
}
135+
File dir = new File("${sdkRoot}/ndk")
136+
if (dir.isDirectory()) {
137+
for (ndkDir in dir.listFiles()) {
138+
ndkDirs.add(ndkDir.toString())
114139
}
115140
}
116141

117142
/* Find suitable ndk in dirs */
118-
def rets = new String[2]
119143
rets[0] = ndkVer
120144

121145
for (ndkDir in ndkDirs) {
122146
if (findNDKInDir(ndkVer, allowNewerNdk, ndkDir, rets)) {
123-
return rets
147+
return
124148
}
125149
}
126150

127151
println("No installed ndk found, the gradle will install ndk: $ndkVer automatically")
128152
rets[1] = null
129-
return rets
130153
}
131154

132155
/**
133156
* Find suitable cmake for current project
134157
* @param project The current android project
135158
* @param cmakeVer The required cmake version of project
159+
* @param rets
136160
* @return
137161
*/
138-
static String findCMake(project, String cmakeVer = "3.10.2+") {
162+
private static void findCMake(sdkRoot, String cmakeVer, rets) {
139163
def allowNewerCMake = false
140164
if(cmakeVer.endsWith('+')) {
141165
allowNewerCMake = true
@@ -145,38 +169,27 @@ class axistools {
145169
def cmakeBinDirs = []
146170

147171
def cmakeBinDir = null
148-
if (project != null) {
149-
cmakeBinDir = getCMakeBinFromProjectLocal(project)
150-
if (cmakeBinDir != null) {
151-
cmakeBinDirs.add(cmakeBinDir);
152-
}
153-
}
154-
155-
def sdkRoot = Paths.get("${System.env.ANDROID_SDK_ROOT}")
156172

157-
if(Files.exists(sdkRoot)) {
158-
def verList = []
173+
def verList = []
159174

160-
// Scan installed cmake in $sdk_root/cmake
161-
File sdkCMakeDir = new File(sdkRoot.toAbsolutePath().toString() + '/cmake')
162-
if (sdkCMakeDir.isDirectory()) {
163-
for (cmakeDir in sdkCMakeDir.listFiles()) {
164-
verList.add(cmakeDir.getName())
165-
}
175+
// Scan installed cmake in $sdk_root/cmake
176+
File sdkCMakeDir = new File(sdkRoot + '/cmake')
177+
if (sdkCMakeDir.isDirectory()) {
178+
for (cmakeDir in sdkCMakeDir.listFiles()) {
179+
verList.add(cmakeDir.getName())
166180
}
181+
}
167182

168-
// Sort cmake verList
169-
verList.sort {a,b ->
170-
return axistools.compareVersion(b, a)
171-
}
183+
// Sort cmake verList
184+
verList.sort {a,b ->
185+
return axistools.compareVersion(b, a)
186+
}
172187

173-
// Collect cmakeBinDirs for search
174-
sdkRoot = sdkRoot.toAbsolutePath().toString()
175-
for(foundVer in verList){
176-
cmakeBinDir = sdkRoot + File.separator + "cmake" + File.separator + foundVer + File.separator + "bin"
177-
if(new File(cmakeBinDir).isDirectory()) {
178-
cmakeBinDirs.add(cmakeBinDir)
179-
}
188+
// Collect cmakeBinDirs for search
189+
for(foundVer in verList){
190+
cmakeBinDir = sdkRoot + File.separator + "cmake" + File.separator + foundVer + File.separator + "bin"
191+
if(new File(cmakeBinDir).isDirectory()) {
192+
cmakeBinDirs.add(cmakeBinDir)
180193
}
181194
}
182195

@@ -199,11 +212,11 @@ class axistools {
199212
foundCMakeVer = cmakeVer
200213
}
201214

202-
return foundCMakeVer
215+
rets[2] = foundCMakeVer
203216
}
204217

205218
private static int compareVersion(String ver1, String ver2) {
206-
return new VersionComparator().compare(ver1, ver2);
219+
return new VersionComparator().compare(ver1, ver2)
207220
}
208221

209222
private static String findNDKInDir(ndkVer, allowNewerNdk, ndkDir, rets) {
@@ -256,9 +269,9 @@ class axistools {
256269
int exitVal = proc.exitValue()
257270
if (exitVal == 0) {
258271
InputStream stdIn = proc.getInputStream();
259-
InputStreamReader isr = new InputStreamReader(stdIn);
260-
BufferedReader br = new BufferedReader(isr);
261-
String verLine = br.readLine();
272+
InputStreamReader isr = new InputStreamReader(stdIn)
273+
BufferedReader br = new BufferedReader(isr)
274+
String verLine = br.readLine()
262275
def verInfo = verLine.split("\\s")
263276
if (verInfo.length >= 3) {
264277
def foundVer = verInfo[2]
@@ -288,37 +301,19 @@ class axistools {
288301
return foundCMakeVer
289302
}
290303

291-
private static String getCMakeBinFromProjectLocal(project) {
292-
String programName = getCMakeProgramName();
293-
Properties properties = new Properties()
294-
try {
295-
properties.load(project.rootProject.file("local.properties"))
296-
def cmakeDir = properties.getProperty("cmake.dir")
297-
if(cmakeDir != null) {
298-
def cmakeBin = "$cmakeDir/bin"
299-
if(new File("$cmakeBin/$programName").isFile())
300-
return cmakeBin;
301-
}
302-
}
303-
catch(Exception) {
304-
}
305-
306-
return null
307-
}
308-
309304
private static String getCMakeBinFromPath() {
310-
String cmakeExecName = getCMakeProgramName();
305+
String cmakeExecName = getCMakeProgramName()
311306
def foundBinPath = null
312307
Stream.of(System.getenv("PATH").split(Pattern.quote(File.pathSeparator)))
313308
.map(Paths::get)
314309
.anyMatch(path -> {
315-
def programPath = path.resolve(cmakeExecName);
310+
def programPath = path.resolve(cmakeExecName)
316311
boolean fileExist = Files.exists(path.resolve(cmakeExecName))
317312
if(fileExist) {
318313
foundBinPath = path.toAbsolutePath().toString()
319314
}
320315
return fileExist
321-
});
316+
})
322317

323318
return foundBinPath
324319
}
@@ -332,7 +327,7 @@ class axistools {
332327
}
333328

334329
private static boolean isWindows() {
335-
return System.getProperty("os.name").toLowerCase().contains("windows");
330+
return System.getProperty("os.name").toLowerCase().contains("windows")
336331
}
337332
}
338333

templates/cpp-template-default/proj.android/app/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
88
android {
99
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
1010

11-
// setup ndk
12-
def ndkInfo = axistools.findNDK(project)
13-
ndkVersion = ndkInfo[0]
14-
if(ndkInfo[1]) {
15-
ndkPath = ndkInfo[1]
11+
// Setup native build tools: ndk & cmake
12+
def nbtInfo = axistools.findNativeBuildTools(project)
13+
ndkVersion = nbtInfo[0]
14+
if(nbtInfo[1]) {
15+
ndkPath = nbtInfo[1]
1616
}
17-
18-
// setup cmake
19-
def cmakeVer = axistools.findCMake(project)
17+
def cmakeVer = nbtInfo[2]
2018

2119
defaultConfig {
2220
applicationId "org.cocos2dx.hellocpp"

templates/lua-template-default/frameworks/runtime-src/proj.android/app/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
88
android {
99
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
1010

11-
// setup ndk
12-
def ndkInfo = axistools.findNDK(project)
13-
ndkVersion = ndkInfo[0]
14-
if(ndkInfo[1]) {
15-
ndkPath = ndkInfo[1]
11+
// Setup native build tools: ndk & cmake
12+
def nbtInfo = axistools.findNativeBuildTools(project)
13+
ndkVersion = nbtInfo[0]
14+
if(nbtInfo[1]) {
15+
ndkPath = nbtInfo[1]
1616
}
17-
18-
// setup cmake
19-
def cmakeVer = axistools.findCMake(project)
17+
def cmakeVer = nbtInfo[2]
2018

2119
defaultConfig {
2220
applicationId "org.cocos2dx.hellolua"

tests/cpp-tests/proj.android/app/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
88
android {
99
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
1010

11-
// setup ndk
12-
def ndkInfo = axistools.findNDK(project)
13-
ndkVersion = ndkInfo[0]
14-
if(ndkInfo[1]) {
15-
ndkPath = ndkInfo[1]
11+
// Setup native build tools: ndk & cmake
12+
def nbtInfo = axistools.findNativeBuildTools(project)
13+
ndkVersion = nbtInfo[0]
14+
if(nbtInfo[1]) {
15+
ndkPath = nbtInfo[1]
1616
}
17-
18-
// setup cmake
19-
def cmakeVer = axistools.findCMake(project)
17+
def cmakeVer = nbtInfo[2]
2018

2119
defaultConfig {
2220
applicationId "org.cocos2dx.cpp_tests"

tests/fairygui-tests/proj.android/app/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
88
android {
99
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
1010

11-
// setup ndk
12-
def ndkInfo = axistools.findNDK(project)
13-
ndkVersion = ndkInfo[0]
14-
if(ndkInfo[1]) {
15-
ndkPath = ndkInfo[1]
11+
// Setup native build tools: ndk & cmake
12+
def nbtInfo = axistools.findNativeBuildTools(project)
13+
ndkVersion = nbtInfo[0]
14+
if(nbtInfo[1]) {
15+
ndkPath = nbtInfo[1]
1616
}
17-
18-
// setup cmake
19-
def cmakeVer = axistools.findCMake(project)
17+
def cmakeVer = nbtInfo[2]
2018

2119
defaultConfig {
2220
applicationId "org.cocos2dx.fairygui_tests"

tests/live2d-tests/proj.android/app/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
88
android {
99
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
1010

11-
// setup ndk
12-
def ndkInfo = axistools.findNDK(project)
13-
ndkVersion = ndkInfo[0]
14-
if(ndkInfo[1]) {
15-
ndkPath = ndkInfo[1]
11+
// Setup native build tools: ndk & cmake
12+
def nbtInfo = axistools.findNativeBuildTools(project)
13+
ndkVersion = nbtInfo[0]
14+
if(nbtInfo[1]) {
15+
ndkPath = nbtInfo[1]
1616
}
17-
18-
// setup cmake
19-
def cmakeVer = axistools.findCMake(project)
17+
def cmakeVer = nbtInfo[2]
2018

2119
defaultConfig {
2220
applicationId "org.cocos2dx.live2d_tests"

tests/lua-tests/project/proj.android/app/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ apply from: project(':libcocos2dx').projectDir.toString() + "/axistools.gradle"
88
android {
99
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
1010

11-
// setup ndk
12-
def ndkInfo = axistools.findNDK(project)
13-
ndkVersion = ndkInfo[0]
14-
if(ndkInfo[1]) {
15-
ndkPath = ndkInfo[1]
11+
// Setup native build tools: ndk & cmake
12+
def nbtInfo = axistools.findNativeBuildTools(project)
13+
ndkVersion = nbtInfo[0]
14+
if(nbtInfo[1]) {
15+
ndkPath = nbtInfo[1]
1616
}
17-
18-
// setup cmake
19-
def cmakeVer = axistools.findCMake(project)
17+
def cmakeVer = nbtInfo[2]
2018

2119
defaultConfig {
2220
applicationId "org.cocos2dx.lua_tests"

0 commit comments

Comments
 (0)