summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-05-09 22:51:04 +0800
committerruki <[email protected]>2019-05-09 11:06:24 +0800
commit1e711f4240904d564062aea9ca9c43d832dcbe61 (patch)
tree90f2e58269b2cac29192428d43fb5eb4ad307cfe
parent51af9c5c30a3bd22ca6687b1474d0cccf98a798a (diff)
deploy android qt app
-rw-r--r--xmake/modules/detect/sdks/find_ndk.lua2
-rw-r--r--xmake/rules/qt/xmake.lua123
2 files changed, 123 insertions, 2 deletions
diff --git a/xmake/modules/detect/sdks/find_ndk.lua b/xmake/modules/detect/sdks/find_ndk.lua
index 711af1e04..16631fe89 100644
--- a/xmake/modules/detect/sdks/find_ndk.lua
+++ b/xmake/modules/detect/sdks/find_ndk.lua
@@ -168,7 +168,7 @@ function main(sdkdir, opt)
local arch = opt.arch or config.get("arch") or "armv7-a"
-- find ndk
- local ndk = _find_ndk(sdkdir or config.get("ndk") or global.get("ndk") or config.get("sdk"), arch, opt.sdkver or config.get("ndk_sdkver"), opt.toolchains_ver or config.get("ndk_toolchains_ver"))
+ local ndk = _find_ndk(sdkdir or config.get("ndk") or global.get("ndk"), arch, opt.sdkver or config.get("ndk_sdkver"), opt.toolchains_ver or config.get("ndk_toolchains_ver"))
if ndk and ndk.sdkdir then
-- save to config
diff --git a/xmake/rules/qt/xmake.lua b/xmake/rules/qt/xmake.lua
index beb87f7f9..7d6fc74a7 100644
--- a/xmake/rules/qt/xmake.lua
+++ b/xmake/rules/qt/xmake.lua
@@ -74,7 +74,7 @@ rule("qt.application")
-- we must set kind before target.on_load(), may we will use target in on_load()
before_load(function (target)
- target:set("kind", "binary")
+ target:set("kind", is_plat("android") and "shared" or "binary")
end)
-- after load
@@ -92,3 +92,124 @@ rule("qt.application")
end
end)
+ -- after build for android
+ after_build("android", function (target, opt)
+
+ -- imports
+ import("core.theme.theme")
+ import("core.base.option")
+ import("core.project.config")
+
+ -- get target apk path
+ local target_apk = path.join(path.directory(target:targetfile()), target:basename() .. ".apk")
+
+ -- trace progress info
+ cprintf("${color.build.progress}" .. theme.get("text.build.progress_format") .. ":${clear} ", opt.progress)
+ if option.get("verbose") then
+ cprint("${dim color.build.target}generating.qt.app %s.apk", target:basename())
+ else
+ cprint("${color.build.target}generating.qt.app %s.apk", target:basename())
+ end
+
+ -- get qt sdk
+ local qt = target:data("qt")
+
+ -- get ndk
+ local ndk = path.translate(assert(config.get("ndk"), "cannot get NDK!"))
+ local ndk_sdkver = assert(config.get("ndk_sdkver"), "cannot get the sdk version of NDK!")
+
+ -- get ndk host
+ local ndk_host = os.host() .. "-" .. os.arch()
+ if is_host("windows") then
+ ndk_host = os.arch() == "x64" and "windows-x86_64" or "windows-x86"
+ elseif is_host("macosx") then
+ ndk_host = "darwin-x86_64"
+ elseif is_host("linux") then
+ ndk_host = "linux-x86_64"
+ end
+
+ -- get androiddeployqt
+ local androiddeployqt = path.join(qt.bindir, "androiddeployqt" .. (is_host("windows") and ".exe" or ""))
+ assert(os.isexec(androiddeployqt), "androiddeployqt not found!")
+
+ -- get working directory
+ local workdir = path.join(config.buildir(), ".qt", "app", "android", target:name())
+
+ -- get android-build directory
+ local android_buildir = path.join(workdir, "android-build")
+
+ -- get android platform
+ local android_platform = "android-" .. tostring(ndk_sdkver)
+
+ -- get java home
+ local java_home = assert(os.getenv("JAVA_HOME"), "please set $JAVA_HOME environment variable first!")
+
+ -- get android sdk directory
+ local android_sdkdir = path.translate(assert(config.get("sdk"), "please run `xmake f --sdk=xxx` to set the android sdk directory!"))
+
+ -- get the target architecture
+ local target_archs =
+ {
+ ["armv5te"] = "armeabi"
+ , ["armv7-a"] = "armeabi-v7a"
+ , ["arm64-v8a"] = "arm64-v8a"
+ , i386 = "x86"
+ , x86_64 = "x86_64"
+ , mips = "mips"
+ , mips64 = "mips64"
+ }
+ local target_arch = assert(target_archs[config.arch()], "unsupport target arch(%s)!", config.arch())
+
+ -- install target to android-build/libs first
+ os.cp(target:targetfile(), path.join(android_buildir, "libs", target_arch, path.filename(target:targetfile())))
+
+ -- get stdcpp path
+ local stdcpp_path = path.join(ndk, "sources/cxx-stl/llvm-libc++/libs", target_arch, "libc++_shared.so")
+
+ -- generate android-deployment-settings.json file
+ local android_deployment_settings = path.join(workdir, "android-deployment-settings.json")
+ io.writefile(android_deployment_settings, format([[
+ {
+ "description": "This file is generated by qmake to be read by androiddeployqt and should not be modified by hand.",
+ "qt": "%s",
+ "sdk": "%s",
+ "ndk": "%s",
+ "toolchain-prefix": "llvm",
+ "tool-prefix": "llvm",
+ "toolchain-version": "4.9",
+ "ndk-host": "%s",
+ "target-architecture": "%s",
+ "qml-root-path": "%s",
+ "stdcpp-path": "%s",
+ "useLLVM": true,
+ "application-binary": "%s"
+ }]], qt.sdkdir, android_sdkdir, ndk, ndk_host, target_arch, os.projectdir(), stdcpp_path, target:targetfile()))
+
+ -- do deploy
+ local argv = {"--input", android_deployment_settings,
+ "--output", android_buildir,
+ "--android-platform", android_platform,
+ "--jdk", java_home,
+ "--gradle", "--no-gdbserver"}
+ os.vrunv(androiddeployqt, argv)
+
+ -- output apk
+ os.cp(path.join(android_buildir, "build", "outputs", "apk", "debug", "android-build-debug.apk"), target_apk)
+
+ -- show apk output path
+ vprint("the apk output path: %s", target_apk)
+ end)
+
+ -- on install for android
+ on_install("android", function (target, opt)
+
+ -- get target apk path
+ local target_apk = path.join(path.directory(target:targetfile()), target:basename() .. ".apk")
+ assert(os.isfile(target_apk), "apk not found, please build %s first!", target:name())
+
+ -- show install info
+ print("installing %s ..", target_apk)
+
+ -- install apk to device
+ os.vrunv("adb", {"install", "-r", target_apk})
+ end)