Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Compile Python for Android
# This script builds & bundles Python for Android
# You'll end up with a tar.bz2 file that contains a Python distribution
# Requires all prerequisites to build Android on the host, and the NDK
# installed.
# This script creates a file python4android.tbz2. Unpack it on your device
# (into a non-noexec partition!) and enjoy.
set -e
# Set the correct NDK path here!
# This shouldn't need to be updated often.. but it might need updating, so check this:
## The remainder shouldn't need any changes, unless you want a newer Python version:
# 1) Download Python
tar xaf Python-2.7.9.tar.xz
cd Python-2.7.9
# 2) Compile host pgen/python
make python Parser/pgen
mv python pythonh
mv Parser/pgen Parser/pgenh
) 2>&1 | tee host-build.log
# 3) Patch the Makefile to use the host versions
sed -i -re 's#^(\s+)\$\(PGEN\)#\1$(PGEN)h#m'
sed -i -re 's#./\$\(BUILDPYTHON\)#./$(BUILDPYTHON)h#m'
make distclean
# 4) Patch some usually misdetected functions, setup modules to compile and fix some other problems
sed -i -re 's# ftime # #' configure
sed -i -re 's#p->pw_gecos#""#' Modules/pwdmodule.c
sed -i -re "s#exit_status = .+#exit_status = 0#" Lib/
for MOD in _socket array cmath math _struct time operator _random _collections _heapq itertools strop _functools _elementtree datetime _bisect unicodedata _locale fcntl "select" mmap _md5 _sha256 _sha512 _sha binascii cPickle cStringIO _io; do
grep "#$MOD" Modules/Setup.dist | grep "\.c" | sed -re 's%^#%%' >> Modules/Setup.local
# 5) Configure
export CFLAGS="-fPIC -fPIE --sysroot $SYSROOT -Dandroid -mandroid"
export LDFLAGS="--sysroot $SYSROOT"
export ac_cv_file__dev_ptmx=no
export ac_cv_file__dev_ptc=no
./configure --disable-ipv6 --host=arm-linux-androideabi --build=x86_64-linux-gnu ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no 2>&1 | tee dev-build.log
# 6) Fix some mistakes configure made
sed -i -re "s%^#define HAVE_GETHOSTBYNAME_.+%%" pyconfig.h
echo "#define HAVE_GETHOSTBYNAME 1" >> pyconfig.h
# 7) Compile python
make 2>&1 | tee -a dev-build.log
# 8) Recompile the python executable as a position independent executable
rm -f python
sed -i -re 's#^LDFLAGS=#LDFLAGS= -pie #' Makefile
make 2>&1 | tee deb-build-pie.log
# 9) Install into our prefix and prepare an archive
make install DESTDIR=`pwd`/../prefix 2>&1 | tee install.log
cd ../prefix/usr/local/lib
rm -f *.a
cd python*
rm -rf test
find -iname "*.pyc" -exec rm \{\} \;
cd ../../bin
arm-linux-androideabi-strip python2.7
cd ..
rm -f python.tbz2
tar cjf python.tbz2 *
mv python.tbz2 ../../../python4android.tbz2
# Done. You may remove the temporary directories now.

This comment has been minimized.

Copy link

@sunil7620 sunil7620 commented Feb 1, 2018

Thanks for this article. I am compiling Python2.7 using androidndk10e. but following module is not compiling.running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers

Python build finished, but the necessary bits to build these modules were not found:
_bsddb _curses _curses_panel
_sqlite3 _ssl _tkinter
bsddb185 bz2 dbm
dl gdbm imageop
linuxaudiodev nis ossaudiodev
readline spwd sunaudiodev
To find the necessary bits, look in in detect_modules() for the module's name.
For me zlib is needed so can you point why its failing


This comment has been minimized.

Copy link

@huhuang03 huhuang03 commented Sep 18, 2020

configure:3815: gcc --version >&5
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

configure:3826: $? = 0
configure:3815: gcc -v >&5
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.4' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.4) 
configure:3826: $? = 0
configure:3815: gcc -V >&5
gcc: error: unrecognized command line option '-V'
gcc: fatal error: no input files
compilation terminated.
configure:3826: $? = 4
configure:3815: gcc -qversion >&5
gcc: error: unrecognized command line option '-qversion'
gcc: fatal error: no input files
compilation terminated.
configure:3826: $? = 4
configure:3846: checking whether the C compiler works
configure:3868: gcc -fPIC -fPIE --sysroot /home/th/sdks/android-sdk-linux/ndk/19.2.5345600//platforms/android-21/arch-arm -Dandroid -mandroid  --sysroot /home/th/sdks/android-sdk-linux/ndk/19.2.5345600//platforms/android-21/arch-arm conftest.c  >&5
/usr/bin/ld: /home/th/sdks/android-sdk-linux/ndk/19.2.5345600//platforms/android-21/arch-arm/usr/lib/../lib/crtbegin_dynamic.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: /home/th/sdks/android-sdk-linux/ndk/19.2.5345600//platforms/android-21/arch-arm/usr/lib/../lib/crtbegin_dynamic.o: Relocations in generic ELF (EM: 40)
/home/th/sdks/android-sdk-linux/ndk/19.2.5345600//platforms/android-21/arch-arm/usr/lib/../lib/crtbegin_dynamic.o: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
configure:3872: $? = 1
configure:3910: result: no
configure: failed program was:
| /* confdefs.h */
| #define _GNU_SOURCE 1
| #define _NETBSD_SOURCE 1
| #define __BSD_VISIBLE 1
| #define _BSD_TYPES 1
| #define _DARWIN_C_SOURCE 1
| #define _XOPEN_SOURCE 600
| #define _POSIX_C_SOURCE 200112L
| /* end confdefs.h.  */
| int
| main ()
| {
|   ;
|   return 0;
| }
configure:3915: error: in `/home/th/source/python_build/Python-2.7.9':
configure:3917: error: C compiler cannot create executables
See `config.log' for more details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment