kivy默认编译出来的apk中是包含你所有.py源码的,很轻松会被反编译,使用cython将.py编译为.so文件调用,会更好的保护你的源码,这里要着重说下,只是比kivy默认的要好一点,并不是不可以反编译。
但是安卓调用的.so文件需要交叉编译为arm平台的才可以,这里记录下交叉编译的步骤,我也是踩坑了好几天。
将下面代码保存为 setuparm.py 红色部分根据自己的情况修改其中 android 指定的是安卓版本
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import os
os.environ['CC'] = ‘/home/kivydev/andr/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc’
os.environ['LDSHARED'] = ‘/home/kivydev/andr/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -shared’
compile_args=['--sysroot=/home/kivydev/andr/android-ndk-r10e/platforms/android-19/arch-arm/']
link_args=['--sysroot=/home/kivydev/andr/android-ndk-r10e/platforms/android-19/arch-arm/','-L/home/kivydev/.local/share/python-for-android/dists/py2dist/libs/armeabi/','-lpython2.7']
setup(cmdclass = {‘build_ext’: build_ext},
ext_modules= [ Extension("core", ["core.py"],
extra_compile_args=compile_args,
extra_link_args=link_args) ])
执行编译: 红色部分是项目路径也是 setuparm.py 所在路径
cd/home/kivydev/work/xytest/core/
python setuparm.py build_ext --inplace
编译完成后可以使用 file ***.so 和 readelf -d ***.so 查看文件格式信息
kivydev@kivydev-VirtualBox:~/work/xytest/core$ file core.socore.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/bin/linker, not stripped
kivydev@kivydev-VirtualBox:~/work/xytest/core$ readelf -d core.so
Dynamic section at offset 0x9db4 contains 24 entries:
Tag Type Name/Value
0×00000003 (PLTGOT) 0xaeec
0×00000002 (PLTRELSZ) 528 (bytes)
0×00000017 (JMPREL) 0x15c8
0×00000014 (PLTREL) REL
0×00000011 (REL) 0xf40
0×00000012 (RELSZ) 1672 (bytes)
0×00000013 (RELENT) 8 (bytes)
0x6ffffffa (RELCOUNT) 190
0×00000006 (SYMTAB) 0×128
0x0000000b (SYMENT) 16 (bytes)
0×00000005 (STRTAB) 0x6c8
0x0000000a (STRSZ) 1532 (bytes)
0×00000004 (HASH) 0xcc4
0×00000001 (NEEDED) Shared library: [libpython2.7.so]
0×00000001 (NEEDED) Shared library: [libc.so]
0×00000001 (NEEDED) Shared library: [libdl.so]
0x0000001a (FINI_ARRAY) 0xada8
0x0000001c (FINI_ARRAYSZ) 8 (bytes)
0×00000019 (INIT_ARRAY) 0xadb0
0x0000001b (INIT_ARRAYSZ) 4 (bytes)
0×00000010 (SYMBOLIC) 0×0
0x0000001e (FLAGS) SYMBOLIC BIND_NOW
0x6ffffffb (FLAGS_1) Flags: NOW
0×00000000 (NULL) 0×0