实现了一个纯C/C++开发安卓应用的例子 #5665
Replies: 3 comments 5 replies
-
404
这个应该很简单吧,不是加个导出修饰符就行了么。
目前是不支持的,除非是弄多个 target 或写一个 build.lua 多次 config/build
|
Beta Was this translation helpful? Give feedback.
-
我稍微改了一下。 set_plat("android")
set_arch("arm64-v8a")
set_config("android_sdk", "C:/android")
set_config("ndk", "C:/android/android-ndk-r27")
set_config("ndk_sdkver", "30")
target("xmake-androidcpp")
set_kind("shared")
add_rules("androidcpp")
add_files("src/*.c")
add_syslinks("log", "android", "GLESv2", "EGL", "OpenSLES", "m")
add_deps("wrapper")
target("wrapper")
set_kind("object")
add_files("androidcpp/*.c")
add_includedirs("androidcpp", {public = true})
rule("androidcpp")
on_package(function (target)
local ndk = target:toolchain("ndk")
local android_sdk_version = ndk:config("ndk_sdkver")
local android_manifest = "AndroidManifest.xml"
local android_res = "res"
local android_assets = "assets"
local keystore = "xmake-debug.jks"
local keystore_pass = "123456"
local apk_output_path = target:targetdir()
import("androidcpp/android_build")(target, android_sdk_version, android_manifest, android_res, android_assets, keystore, keystore_pass, apk_output_path)
end) function main(target, android_sdk_version, android_manifest, android_res, android_assets, keystore, keystore_pass, apk_output_path)
import("utils.archive")
local ndk = target:toolchain("ndk")
-- get android sdk directory
local android_sdkdir = path.unix(assert(ndk:config("android_sdk"), "please run `xmake f --android_sdk=xxx` to set the android sdk directory!"))
-- get android build-tools version
local android_build_toolver = assert(ndk:config("build_toolver"), "please run `xmake f --build_toolver=xxx` to set the android build-tools version!")
local aapt = path.join(android_sdkdir, "build-tools", android_build_toolver, "aapt" .. (is_host("windows") and ".exe" or ""))
local resonly_apk = path.join(outputtemppath, "res_only.zip")
local aapt_argv = {"package", "-f",
"-M", android_manifest,
"-S", android_res,
"-A", android_assets,
"-I", path.join(android_sdkdir, "platforms", string.format("android-%s", android_sdk_version), "android.jar"),
"-F", resonly_apk
}
print("packing resources...")
os.vrunv(aapt, aapt_argv)
-- import("lib.detect.find_tool")
-- local zip = find_tool("7z")
-- assert(zip, "zip not found!")
-- local zip_argv = { "a", "-tzip", "-r",
-- resonly_apk,
-- path.join(".", libpath)
-- }
-- print("archiving libs...")
-- archive.archive("/tmp/a.zip", "/tmp/outputdir")
local aligned_apk = path.join(outputtemppath, "unsigned.apk")
local zipalign = path.join(android_sdkdir, "build-tools", android_build_toolver, "zipalign" .. (is_host("windows") and ".exe" or ""))
local zipalign_argv = {
"-f", "4",
resonly_apk, aligned_apk
}
print("align apk...")
os.vrunv(zipalign, zipalign_argv)
local final_output_path = outputtemppath
local final_apk = path.join(final_output_path, target:basename() .. ".apk")
local apksigner = path.join(android_sdkdir, "build-tools", android_build_toolver, "apksigner" .. (is_host("windows") and ".bat" or ""))
local apksigner_argv = {
"sign",
"--ks", keystore,
"--ks-pass", string.format("pass:%s", keystore_pass),
"--out", final_apk,
"--in", aligned_apk
}
print("signing apk...")
os.vrunv(apksigner, apksigner_argv)
if apk_output_path then
os.mv(final_apk, apk_output_path)
end
end |
Beta Was this translation helpful? Give feedback.
-
经过一段时间的开发,支持打包一个jar。并且支持通过xmake命令往设备上安装和运行程序。 其实我还实现了在安卓上使用sdl,不过还不太完善,之后再放上来。 |
Beta Was this translation helpful? Give feedback.
-
仓库:https://github.com/vkensou/xmake-androidcpp
参考了https://github.com/cnlohr/rawdrawandroid 和 https://github.com/VadimBoev/FlappyBird
效果如下:
实现了以下功能:
通过xmake install将程序打包成apk
通过xmake run将apk安装到设备上并运行
目前有一个限制:不能通过一个命令build多个arch,比如arm64-v8a和armeabi-v7a,然后打包apk
希望未来完善后可以加入xmake的template里
Beta Was this translation helpful? Give feedback.
All reactions