Skip to content

Commit 428dec3

Browse files
Bin Zhangminggo
authored andcommitted
Solve the error that when building android project with Android SDK Tools 25.3.x. (#410)
1 parent afc22f3 commit 428dec3

File tree

3 files changed

+84
-20
lines changed

3 files changed

+84
-20
lines changed

bin/strings.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@
184184
"COMPILE_ERROR_LOW_VS_VER" : "Version of VS is too low.",
185185
"COMPILE_ERROR_MSDBUILD_NOT_FOUND" : "MSBuild is not found.",
186186
"COMPILE_ERROR_UNKNOWN_ENGINE_VERSION" : "Unknown engine version!",
187+
"COMPILE_ERROR_UNKNOWN_ANDROID_SDK_TOOLS_VERSION" : "Unknown Android SDK Tools version!",
188+
"COMPILE_ERROR_ANT_NOT_SUPPORTED": "Ant (Eclipse project) is not supported from Android SDK Tools 25.3.0.",
187189
"COMPILE_INFO_FIND_VS_PATH_FMT" : "Find VS path : %s",
188190
"COMPILE_INFO_DEVENV_NOT_FOUND" : "Not found devenv. Try to use msbuild instead.",
189191
"COMPILE_INFO_FIND_MSBUILD_FMT" : "Found msbuild path: %s",
@@ -527,7 +529,9 @@
527529
"COMPILE_ERROR_WRONG_VS_VER_FMT" : "不支持 VS %d。",
528530
"COMPILE_ERROR_LOW_VS_VER" : "VS 版本过低。",
529531
"COMPILE_ERROR_MSDBUILD_NOT_FOUND" : "未找到 MSBuild 工具。",
530-
"COMPILE_ERROR_UNKNOWN_ENGINE_VERSION" : "無法識別引擎版本!",
532+
"COMPILE_ERROR_UNKNOWN_ENGINE_VERSION" : "无法识别引擎版本!",
533+
"COMPILE_ERROR_UNKNOWN_ANDROID_SDK_TOOLS_VERSION" : "无法识别 Android SDK Tools 版本.",
534+
"COMPILE_ERROR_ANT_NOT_SUPPORTED": "Android SDK Tools 25.3.0 版本开始, 不再支持 Ant (Eclipse 工程) 编译打包.",
531535
"COMPILE_INFO_FIND_VS_PATH_FMT" : "找到 VS 安装路径:%s",
532536
"COMPILE_INFO_DEVENV_NOT_FOUND" : "未找到 devenv。尝试使用 msbuild 来代替。",
533537
"COMPILE_INFO_FIND_MSBUILD_FMT" : "找到 msbuild 安装路径:%s",
@@ -870,7 +874,9 @@
870874
"COMPILE_ERROR_WRONG_VS_VER_FMT" : "不支持 VS %d。",
871875
"COMPILE_ERROR_LOW_VS_VER" : "VS 版本過低。",
872876
"COMPILE_ERROR_MSDBUILD_NOT_FOUND" : "未找到 MSBuild 工具。",
873-
"COMPILE_ERROR_UNKNOWN_ENGINE_VERSION" : "无法识别引擎版本!",
877+
"COMPILE_ERROR_UNKNOWN_ENGINE_VERSION" : "無法識別引擎版本!",
878+
"COMPILE_ERROR_UNKNOWN_ANDROID_SDK_TOOLS_VERSION" : "無法識別 Android SDK Tools 版本!",
879+
"COMPILE_ERROR_ANT_NOT_SUPPORTED": "Android SDK Tools 25.3.0 版本開始, 不再支持 Ant (Eclipse 工程) 編譯打包.",
874880
"COMPILE_INFO_FIND_VS_PATH_FMT" : "找到 VS 安裝路徑:%s",
875881
"COMPILE_INFO_DEVENV_NOT_FOUND" : "未找到 devenv。嘗試使用 msbuild 來代替。",
876882
"COMPILE_INFO_FIND_MSBUILD_FMT" : "找到 msbuild 安裝路徑:%s",

plugins/plugin_compile/build_android.py

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,37 +181,95 @@ def remove_c_libs(self, libs_dir):
181181
ext = os.path.splitext(lib_file)[1]
182182
if ext == ".a" or ext == ".so":
183183
os.remove(lib_file)
184-
184+
185+
def _get_android_sdk_tools_ver(self, sdk_tools_path):
186+
cfg_file = os.path.join(sdk_tools_path, 'source.properties')
187+
188+
if os.path.isfile(cfg_file):
189+
f = open(cfg_file)
190+
lines = f.readlines()
191+
pattern = r'^Pkg\.Revision=(\d+)\.(\d+)'
192+
for l in lines:
193+
match = re.match(pattern, l.strip())
194+
if match:
195+
return ((int)(match.group(1)), (int)(match.group(2)))
196+
197+
raise cocos.CCPluginError(MultiLanguage.get_string('COMPILE_ERROR_UNKNOWN_ANDROID_SDK_TOOLS_VERSION'),
198+
cocos.CCPluginError.ERROR_PATH_NOT_FOUND)
199+
200+
def _update_project_properties(self, folder_path, target_str):
201+
props_path = os.path.join(folder_path, 'project.properties')
202+
f = open(props_path)
203+
lines = f.readlines()
204+
f.close()
205+
206+
pattern = r'^target=(.*)$'
207+
matched = False
208+
new_line = 'target=%s\n' % target_str
209+
for i in range(0, len(lines)):
210+
l = lines[i]
211+
match = re.match(pattern, l.strip())
212+
if match:
213+
lines[i] = new_line
214+
matched = True
215+
216+
if not matched:
217+
lines.append('\n')
218+
lines.append(new_line)
219+
220+
f = open(props_path, 'w')
221+
f.writelines(lines)
222+
f.close()
223+
224+
def _write_local_properties(self, folder_path):
225+
local_porps_path = os.path.join(folder_path, 'local.properties')
226+
lines = [
227+
'sdk.dir=%s\n' % self.sdk_root,
228+
'ndk.dir=%s\n' % cocos.check_environment_variable('NDK_ROOT')
229+
]
230+
f = open(local_porps_path, 'w')
231+
f.writelines(lines)
232+
f.close()
233+
185234
def update_project(self, android_platform):
186235
if self.gradle_support_ndk:
187236
# If gradle supports ndk build, should write local.properties manually
188-
local_porps_path = os.path.join(self.app_android_root, 'local.properties')
189-
lines = [
190-
'sdk.dir=%s\n' % self.sdk_root,
191-
'ndk.dir=%s\n' % cocos.check_environment_variable('NDK_ROOT')
192-
]
193-
f = open(local_porps_path, 'w')
194-
f.writelines(lines)
195-
f.close()
237+
self._write_local_properties(self.app_android_root)
196238
return
197239

240+
# Android SDK removed android command & ant support from SDK tools 25.3.0
241+
# So, we should check the Android SDK tools version
242+
sdk_tools_folder = os.path.join(self.sdk_root, 'tools')
243+
main_ver, minor_ver = self._get_android_sdk_tools_ver(sdk_tools_folder)
244+
no_ant = False
245+
if main_ver > 25 or (main_ver == 25 and minor_ver >= 3):
246+
no_ant = True
247+
248+
if not self.use_studio and no_ant:
249+
# Tip the message that ant is not supported from Android SDK tools 25.3.0
250+
raise cocos.CCPluginError(MultiLanguage.get_string('COMPILE_ERROR_ANT_NOT_SUPPORTED'),
251+
cocos.CCPluginError.ERROR_OTHERS)
252+
198253
if self.use_studio:
199254
manifest_path = os.path.join(self.app_android_root, 'app')
200255
else:
201256
manifest_path = self.app_android_root
202257

203-
sdk_tool_path = os.path.join(self.sdk_root, "tools", "android")
204-
205258
# check the android platform
206259
target_str = self.check_android_platform(self.sdk_root, android_platform, manifest_path)
207260

208-
# update project
209-
command = "%s update project -t %s -p %s" % (cocos.CMDRunner.convert_path_to_cmd(sdk_tool_path), target_str, manifest_path)
210-
self._run_cmd(command)
261+
if no_ant:
262+
# should manually update the project
263+
self._write_local_properties(manifest_path)
264+
self._update_project_properties(manifest_path, target_str)
265+
else:
266+
# update project
267+
sdk_tool_path = os.path.join(sdk_tools_folder, "android")
268+
command = "%s update project -t %s -p %s" % (cocos.CMDRunner.convert_path_to_cmd(sdk_tool_path), target_str, manifest_path)
269+
self._run_cmd(command)
211270

212-
# update lib-projects
213-
property_path = manifest_path
214-
self.update_lib_projects(self.sdk_root, sdk_tool_path, android_platform, property_path)
271+
# update lib-projects
272+
self.update_lib_projects(self.sdk_root, sdk_tool_path, android_platform, manifest_path)
215273

216274
if self.use_studio:
217275
# copy the local.properties to the app_android_root

plugins/plugin_compile/project_compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def get_engine_version_num(self):
454454
if engine_ver_str is None:
455455
return None
456456

457-
version_pattern = r'.*([\d]+)\.([\d]+)'
457+
version_pattern = r'cocos2d-x-([\d]+)\.([\d]+)'
458458
match = re.match(version_pattern, engine_ver_str)
459459
if match:
460460
return ((int)(match.group(1)), (int)(match.group(2)))

0 commit comments

Comments
 (0)