Skip to content

Instantly share code, notes, and snippets.

@cawka
Last active March 22, 2021 10:09
Show Gist options
  • Save cawka/11fe9c23b7a13960330b to your computer and use it in GitHub Desktop.
Save cawka/11fe9c23b7a13960330b to your computer and use it in GitHub Desktop.
Recursively calculate shared library dependencies and generate System.load(); statements
# Set correct path to readelf binary in Android NDK
READELF=/opt/crystax-ndk-10.3.1/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-readelf
# List libraries that exist in /system/lib
STANDARD_LIBS=(c dl m stdc++ log z)
has() {
local p=$1
shift
local x
for x in "$@"; do
[[ "${x}" == "${p}" ]] && return 0
done
return 1
}
LIBS=()
x() {
local lib=$1
if [ ! -f "lib$lib.so" ]; then
return
fi
local i
for i in `$READELF -d "lib$lib.so" | grep NEEDED | sed -e "s/.*\[lib//g" | sed -e "s/\.so.*$//g" | xargs`; do
if has $i ${STANDARD_LIBS[*]}; then
continue
fi
x $i
if has $i ${LIBS[*]}; then
continue
fi
LIBS+=($i)
done
if has $lib ${LIBS[*]}; then
continue
fi
LIBS+=($lib)
}
x $1
for lib in ${LIBS[*]}; do
echo "System.loadLibrary(\"$lib\");"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment