Skip to content

Build and Release Android Lua Libraries #40

Build and Release Android Lua Libraries

Build and Release Android Lua Libraries #40

name: Build and Release Android Lua Libraries
# 仓库写入权限
permissions:
contents: write
on:
workflow_dispatch: # 允许手动触发
push:
tags:
- 'android-lua-v*' # 当推送标签时自动触发发布
env:
LUA_VERSION: 5.4.8
LUA_CJSON_VERSION: 2.1.0.15
ANDROID_NDK_VERSION: r26b
ANDROID_API_LEVEL: 21
CACHE_VERSION: v1 # 缓存版本,当NDK或工具链更新时修改此值
jobs:
build-android:
runs-on: ubuntu-latest
strategy:
matrix:
architecture: [armeabi-v7a, arm64-v8a, x86, x86_64]
outputs:
output_dir: ${{ steps.set_outputs.outputs.output_dir }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Cache Android NDK
id: cache-ndk
uses: actions/cache@v4
with:
path: android-ndk-${{ env.ANDROID_NDK_VERSION }}
key: android-ndk-${{ env.ANDROID_NDK_VERSION }}-${{ env.CACHE_VERSION }}
- name: Download and extract Android NDK (if not cached)
if: steps.cache-ndk.outputs.cache-hit != 'true'
run: |
wget -q https://dl.google.com/android/repository/android-ndk-${{ env.ANDROID_NDK_VERSION }}-linux.zip
unzip -q android-ndk-${{ env.ANDROID_NDK_VERSION }}-linux.zip
rm android-ndk-${{ env.ANDROID_NDK_VERSION }}-linux.zip
- name: Set NDK environment
run: |
export ANDROID_NDK_HOME=$PWD/android-ndk-${{ env.ANDROID_NDK_VERSION }}
echo "ANDROID_NDK_HOME=$ANDROID_NDK_HOME" >> $GITHUB_ENV
- name: Cache Lua source code
id: cache-lua
uses: actions/cache@v4
with:
path: lua-${{ env.LUA_VERSION }}
key: lua-source-${{ env.LUA_VERSION }}
- name: Download Lua source code (if not cached)
if: steps.cache-lua.outputs.cache-hit != 'true'
run: |
wget -q https://www.lua.org/ftp/lua-${{ env.LUA_VERSION }}.tar.gz
tar -xzf lua-${{ env.LUA_VERSION }}.tar.gz
rm lua-${{ env.LUA_VERSION }}.tar.gz
- name: Cache lua-cjson source
id: cache-lua-cjson
uses: actions/cache@v4
with:
path: lua-cjson
key: lua-cjson-${{ env.LUA_CJSON_VERSION }}
- name: Download lua-cjson (if not cached)
if: steps.cache-lua-cjson.outputs.cache-hit != 'true'
run: |
git clone --depth 1 --branch ${{ env.LUA_CJSON_VERSION }} \
https://github.com/openresty/lua-cjson.git
- name: Setup toolchain
id: set_toolchain
run: |
case "${{ matrix.architecture }}" in
"armeabi-v7a")
export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi${{ env.ANDROID_API_LEVEL }}-clang
export ARCH=arm
export STRIP=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip
;;
"arm64-v8a")
export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${{ env.ANDROID_API_LEVEL }}-clang
export ARCH=aarch64
export STRIP=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip
;;
"x86")
export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android${{ env.ANDROID_API_LEVEL }}-clang
export ARCH=i686
export STRIP=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip
;;
"x86_64")
export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android${{ env.ANDROID_API_LEVEL }}-clang
export ARCH=x86_64
export STRIP=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip
;;
esac
echo "TOOLCHAIN=$TOOLCHAIN" >> $GITHUB_ENV
echo "ARCH=$ARCH" >> $GITHUB_ENV
echo "STRIP=$STRIP" >> $GITHUB_ENV
- name: Build Lua for Android
run: |
cd lua-${{ env.LUA_VERSION }}
make clean
# Create custom Makefile for Android
cat > src/Makefile.android << 'EOF'
CC=${{ env.TOOLCHAIN }}
AR=${{ env.TOOLCHAIN }}ar
RANLIB=${{ env.TOOLCHAIN }}ranlib
STRIP=${{ env.STRIP }}
MYCFLAGS=-fPIC -O2 -I$(LUA_INC) -DLUA_USE_DLOPEN
MYLDFLAGS=-shared -Wl,-z,global
MYLIBS=-ldl -lm
PLAT= generic
CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \
ltm.o lundump.o lvm.o lzio.o
LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \
lutf8lib.o loadlib.o lcorolib.o linit.o
LUA_T= liblua.so
LUA_O= lua.o
LUAC_T= luac
LUAC_O= luac.o
ALL_T= $(CORE_T) $(LUA_T)
ALL_O= $(CORE_O) $(LUA_O) $(LIB_O)
ALL_A= $(CORE_T)
all: $(ALL_T)
o: $(ALL_O)
$(LUA_T): $(CORE_O) $(LIB_O)
$(CC) -o $@ $(MYLDFLAGS) $(CORE_O) $(LIB_O) $(MYLIBS)
$(STRIP) --strip-unneeded $@
$(LUAC_T): $(LUAC_O) $(LIB_O)
$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LIB_O) $(CORE_O) $(MYLIBS)
clean:
$(RM) $(ALL_T) $(ALL_O)
.c.o:
$(CC) $(MYCFLAGS) -c $<
EOF
# Build Lua
make -C src -f Makefile.android
# Create output directory with version in name
OUTPUT_NAME=liblua-${{ env.LUA_VERSION }}-android-${{ matrix.architecture }}.so
mkdir -p ../output/android/${{ matrix.architecture }}
cp src/liblua.so ../output/android/${{ matrix.architecture }}/$OUTPUT_NAME
# Also keep the original name for compatibility
cp src/liblua.so ../output/android/${{ matrix.architecture }}/liblua.so
echo "Built: $OUTPUT_NAME"
- name: Build lua-cjson for Android
run: |
LUA_SRC=$PWD/lua-${{ env.LUA_VERSION }}
CJSON_SRC=$PWD/lua-cjson
cd $CJSON_SRC
make clean || true
# 编译 cjson
${{ env.TOOLCHAIN }} \
-fPIC -O2 -shared \
-I$LUA_SRC/src \
-DLUA_USE_DLOPEN \
-o cjson.so \
lua_cjson.c strbuf.c fpconv.c \
-lm
# Strip
${{ env.STRIP }} --strip-unneeded cjson.so
# 输出
OUTPUT_NAME=cjson-${{ env.LUA_CJSON_VERSION }}-android-${{ matrix.architecture }}.so
mkdir -p ../output/android/${{ matrix.architecture }}
cp cjson.so ../output/android/${{ matrix.architecture }}/$OUTPUT_NAME
echo "Built cjson.so for ${{ matrix.architecture }}"
- name: Set output variables
id: set_outputs
run: |
echo "output_dir=output" >> $GITHUB_OUTPUT
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: lua-${{ env.LUA_VERSION }}-android-${{ matrix.architecture }}
path: output/android/${{ matrix.architecture }}/
retention-days: 7
create-release:
runs-on: ubuntu-latest
needs: build-android
if: startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
pattern: lua-*-android-*
merge-multiple: true
- name: List downloaded files (debug)
run: |
echo "Downloaded artifacts:"
find artifacts -type f
- name: Create Release
uses: softprops/action-gh-release@v1
with:
name: Lua ${{ env.LUA_VERSION }} for Android
tag_name: Lua-v${{ env.LUA_VERSION }}-for-Android
files: |
artifacts/**/liblua-*-android-*.so
artifacts/**/cjson-*-android-*.so
generate_release_notes: false
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}