Skip to content

Commit 6488f3c

Browse files
committed
Adapt python recipes to ndk's r19 build system
More work done:   - update docker's files to match the python recipes needs   - update documentation with the new ndk version requirements We move python recipe to use a minimal ndk version of 19 because it will simplify the python's cross compilation (this way we can remove some environment variables that we don't need anymore). The only side effect detected is that the python's `zlib` module isn't built anymore. This is caused because python3 and python2 build systems contains libz's hardcoded version, which does not match the version used in ndk 19 (may be this the cause of some `zip/unzip` errors reported in python-for-android's issues?). We will take care later of this zlib problem, or we will get a runtime crash...so it seems that zlib is an `essential module` to successfully run a python-for-android app
1 parent 48a28d5 commit 6488f3c

File tree

5 files changed

+24
-59
lines changed

5 files changed

+24
-59
lines changed

Dockerfile.py2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RUN curl https://raw.githubusercontent.com/kadwanev/retry/1.0.1/retry \
3838
--output /usr/local/bin/retry && chmod +x /usr/local/bin/retry
3939

4040
ENV ANDROID_NDK_HOME="${ANDROID_HOME}/android-ndk"
41-
ENV ANDROID_NDK_VERSION="17c"
41+
ENV ANDROID_NDK_VERSION="19b"
4242
ENV ANDROID_NDK_HOME_V="${ANDROID_NDK_HOME}-r${ANDROID_NDK_VERSION}"
4343

4444
# get the latest version from https://developer.android.com/ndk/downloads/index.html
@@ -82,7 +82,7 @@ RUN ${RETRY} apt -y install -qq --no-install-recommends openjdk-8-jdk \
8282
RUN yes | "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;${ANDROID_SDK_BUILD_TOOLS_VERSION}" > /dev/null
8383

8484
# download platforms, API, build tools
85-
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-19" && \
85+
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-21" && \
8686
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-27" && \
8787
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;${ANDROID_SDK_BUILD_TOOLS_VERSION}" && \
8888
chmod +x "${ANDROID_SDK_HOME}/tools/bin/avdmanager"

Dockerfile.py3

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RUN curl https://raw.githubusercontent.com/kadwanev/retry/1.0.1/retry \
3838
--output /usr/local/bin/retry && chmod +x /usr/local/bin/retry
3939

4040
ENV ANDROID_NDK_HOME="${ANDROID_HOME}/android-ndk"
41-
ENV ANDROID_NDK_VERSION="17c"
41+
ENV ANDROID_NDK_VERSION="19b"
4242
ENV ANDROID_NDK_HOME_V="${ANDROID_NDK_HOME}-r${ANDROID_NDK_VERSION}"
4343

4444
# get the latest version from https://developer.android.com/ndk/downloads/index.html
@@ -82,7 +82,7 @@ RUN ${RETRY} apt -y install -qq --no-install-recommends openjdk-8-jdk \
8282
RUN yes | "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;${ANDROID_SDK_BUILD_TOOLS_VERSION}" > /dev/null
8383

8484
# download platforms, API, build tools
85-
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-19" && \
85+
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-21" && \
8686
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-27" && \
8787
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;${ANDROID_SDK_BUILD_TOOLS_VERSION}" && \
8888
chmod +x "${ANDROID_SDK_HOME}/tools/bin/avdmanager"

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,14 @@ pygame bootstrap (in a near future we will fix it or remove it). Also should be
104104
mentioned that the old python recipe is still usable, and has been renamed to
105105
`python2legacy`. This `python2legacy` recipe allow us to build with a minimum
106106
api lower than 21, which is not the case for the new python recipes which are
107-
limited to a minimum api of 21. All this work has been done using android ndk
108-
version r17c, and your build should success with that version...but be aware
109-
that the project is in constant development so...the ndk version will change
110-
at some time.
107+
limited to a minimum api of 21 (all this work has been done using android ndk
108+
version r17c).
109+
110+
As per time of writing, ``we recommend to use android's NDK r19b`` because the
111+
toolchains installed by default with the NDK may be used *in-place* and the
112+
python-for-android project has been adapted to that feature, so, it's mandatory
113+
to use, at least, ndk version r19 (be aware that more modern versions of the
114+
android's ndk may not work).
111115

112116
Those mentioned changes has been done this way to make easier the transition
113117
between python3 and python2. We will slowly phase out python2 support

doc/source/quickstart.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ named ``tools``, and you will need to run extra commands to install
9494
the SDK packages needed.
9595

9696
For Android NDK, note that modern releases will only work on a 64-bit
97-
operating system. The minimal, and recommended, NDK version to use is r17c:
97+
operating system. The minimal, and recommended, NDK version to use is r19b:
9898

9999
- `Go to ndk downloads page <https://developer.android.com/ndk/downloads/>`_
100100
- Windows users should create a virtual machine with an GNU Linux os
@@ -132,7 +132,7 @@ Then, you can edit your ``~/.bashrc`` or other favorite shell to include new env
132132

133133
# Adjust the paths!
134134
export ANDROIDSDK="$HOME/Documents/android-sdk-27"
135-
export ANDROIDNDK="$HOME/Documents/android-ndk-r17c"
135+
export ANDROIDNDK="$HOME/Documents/android-ndk-r19b"
136136
export ANDROIDAPI="26" # Target API version of your application
137137
export NDKAPI="19" # Minimum supported API version of your application
138138
export ANDROIDNDKVER="r10e" # Version of the NDK you installed

pythonforandroid/python.py

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import sh
1313

1414
from pythonforandroid.recipe import Recipe, TargetPythonRecipe
15-
from pythonforandroid.logger import logger, info, shprint
15+
from pythonforandroid.logger import info, shprint
1616
from pythonforandroid.util import (
1717
current_directory, ensure_dir, walk_valid_filens,
1818
BuildInterruptingException, build_platform)
@@ -113,62 +113,23 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
113113
arch=arch, with_flags_in_cc=with_flags_in_cc)
114114

115115
env = environ.copy()
116+
env['HOSTARCH'] = arch.command_prefix
116117

117-
android_host = env['HOSTARCH'] = arch.command_prefix
118-
toolchain = '{toolchain_prefix}-{toolchain_version}'.format(
119-
toolchain_prefix=self.ctx.toolchain_prefix,
120-
toolchain_version=self.ctx.toolchain_version)
121-
toolchain = join(self.ctx.ndk_dir, 'toolchains',
122-
toolchain, 'prebuilt', build_platform)
123-
124-
env['CC'] = (
125-
'{clang} -target {target} -gcc-toolchain {toolchain}').format(
126-
clang=join(self.ctx.ndk_dir, 'toolchains', 'llvm', 'prebuilt',
127-
build_platform, 'bin', 'clang'),
128-
target=arch.target,
129-
toolchain=toolchain)
130-
env['AR'] = join(toolchain, 'bin', android_host) + '-ar'
131-
env['LD'] = join(toolchain, 'bin', android_host) + '-ld'
132-
env['RANLIB'] = join(toolchain, 'bin', android_host) + '-ranlib'
133-
env['READELF'] = join(toolchain, 'bin', android_host) + '-readelf'
134-
env['STRIP'] = join(toolchain, 'bin', android_host) + '-strip'
135-
env['STRIP'] += ' --strip-debug --strip-unneeded'
118+
env['CC'] = '{}'.format(
119+
join(self.ctx.ndk_dir, 'toolchains', 'llvm', 'prebuilt',
120+
build_platform, 'bin', arch.target + '-clang'))
136121

137122
env['PATH'] = (
138123
'{hostpython_dir}:{old_path}').format(
139124
hostpython_dir=self.get_recipe(
140125
'host' + self.name, self.ctx).get_path_to_python(),
141126
old_path=env['PATH'])
142127

143-
ndk_flags = (
144-
'-fPIC --sysroot={ndk_sysroot} -D__ANDROID_API__={android_api} '
145-
'-isystem {ndk_android_host} -I{ndk_include}').format(
146-
ndk_sysroot=join(self.ctx.ndk_dir, 'sysroot'),
147-
android_api=self.ctx.ndk_api,
148-
ndk_android_host=join(
149-
self.ctx.ndk_dir, 'sysroot', 'usr', 'include', android_host),
150-
ndk_include=join(self.ctx.ndk_dir, 'sysroot', 'usr', 'include'))
151-
sysroot = self.ctx.ndk_platform
152-
env['CFLAGS'] = env.get('CFLAGS', '') + ' ' + ndk_flags
153-
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + ' ' + ndk_flags
154-
env['LDFLAGS'] = env.get('LDFLAGS', '') + ' --sysroot={} -L{}'.format(
155-
sysroot, join(sysroot, 'usr', 'lib'))
156-
157-
# Manually add the libs directory, and copy some object
158-
# files to the current directory otherwise they aren't
159-
# picked up. This seems necessary because the --sysroot
160-
# setting in LDFLAGS is overridden by the other flags.
161-
# TODO: Work out why this doesn't happen in the original
162-
# bpo-30386 Makefile system.
163-
logger.warning('Doing some hacky stuff to link properly')
164-
lib_dir = join(sysroot, 'usr', 'lib')
165-
if arch.arch == 'x86_64':
166-
lib_dir = join(sysroot, 'usr', 'lib64')
167-
env['LDFLAGS'] += ' -L{}'.format(lib_dir)
168-
shprint(sh.cp, join(lib_dir, 'crtbegin_so.o'), './')
169-
shprint(sh.cp, join(lib_dir, 'crtend_so.o'), './')
170-
171-
env['SYSROOT'] = sysroot
128+
env['CFLAGS'] = ' '.join([
129+
'-fPIC',
130+
'-DANDROID',
131+
'-D__ANDROID_API__={}'.format(self.ctx.ndk_api),
132+
])
172133

173134
return env
174135

0 commit comments

Comments
 (0)