Skip to content

Commit df0866d

Browse files
authored
Merge pull request #1516 from kivy/fix_androidbytes
Fix bytes/unicode issues in android recipe
2 parents abe9962 + 32ef178 commit df0866d

File tree

4 files changed

+160
-14
lines changed

4 files changed

+160
-14
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ before_script:
3131
- tox
3232

3333
script:
34-
- docker build --tag=p4a .
34+
- docker build --tag=p4a --file Dockerfile.py2 .
3535
- docker run p4a /bin/sh -c "$COMMAND"

Dockerfile renamed to Dockerfile.py2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# - python-for-android dependencies
44
#
55
# Build with:
6-
# docker build --tag=p4a .
6+
# docker build --tag=p4a --file Dockerfile.py2 .
77
#
88
# Run with:
99
# docker run -it --rm p4a /bin/sh -c '. venv/bin/activate && p4a apk --help'

Dockerfile.py3

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Dockerfile with:
2+
# - Android build environment
3+
# - python-for-android dependencies
4+
#
5+
# Build with:
6+
# docker build --tag=p4apy3 .
7+
#
8+
# Run with:
9+
# docker run -it --rm p4apy3 /bin/sh -c '. venv/bin/activate && p4a apk --help'
10+
#
11+
# Or for interactive shell:
12+
# docker run -it --rm p4apy3
13+
#
14+
# Note:
15+
# Use 'docker run' without '--rm' flag for keeping the container and use
16+
# 'docker commit <container hash> <new image>' to extend the original image
17+
18+
FROM ubuntu:18.04
19+
20+
ENV ANDROID_HOME="/opt/android"
21+
22+
RUN apt -y update -qq \
23+
&& apt -y install -qq --no-install-recommends curl unzip \
24+
&& apt -y autoremove \
25+
&& apt -y clean
26+
27+
28+
ENV ANDROID_NDK_HOME="${ANDROID_HOME}/android-ndk"
29+
ENV ANDROID_NDK_VERSION="16b"
30+
ENV ANDROID_NDK_HOME_V="${ANDROID_NDK_HOME}-r${ANDROID_NDK_VERSION}"
31+
32+
# get the latest version from https://developer.android.com/ndk/downloads/index.html
33+
ENV ANDROID_NDK_ARCHIVE="android-ndk-r${ANDROID_NDK_VERSION}-linux-x86_64.zip"
34+
ENV ANDROID_NDK_DL_URL="https://dl.google.com/android/repository/${ANDROID_NDK_ARCHIVE}"
35+
36+
# download and install Android NDK
37+
RUN curl --location --progress-bar --insecure \
38+
"${ANDROID_NDK_DL_URL}" \
39+
--output "${ANDROID_NDK_ARCHIVE}" \
40+
&& mkdir --parents "${ANDROID_NDK_HOME_V}" \
41+
&& unzip -q "${ANDROID_NDK_ARCHIVE}" -d "${ANDROID_HOME}" \
42+
&& ln -sfn "${ANDROID_NDK_HOME_V}" "${ANDROID_NDK_HOME}" \
43+
&& rm -rf "${ANDROID_NDK_ARCHIVE}"
44+
45+
46+
ENV ANDROID_SDK_HOME="${ANDROID_HOME}/android-sdk"
47+
48+
# get the latest version from https://developer.android.com/studio/index.html
49+
ENV ANDROID_SDK_TOOLS_VERSION="3859397"
50+
ENV ANDROID_SDK_TOOLS_ARCHIVE="sdk-tools-linux-${ANDROID_SDK_TOOLS_VERSION}.zip"
51+
ENV ANDROID_SDK_TOOLS_DL_URL="https://dl.google.com/android/repository/${ANDROID_SDK_TOOLS_ARCHIVE}"
52+
53+
# download and install Android SDK
54+
RUN curl --location --progress-bar --insecure \
55+
"${ANDROID_SDK_TOOLS_DL_URL}" \
56+
--output "${ANDROID_SDK_TOOLS_ARCHIVE}" \
57+
&& mkdir --parents "${ANDROID_SDK_HOME}" \
58+
&& unzip -q "${ANDROID_SDK_TOOLS_ARCHIVE}" -d "${ANDROID_SDK_HOME}" \
59+
&& rm -rf "${ANDROID_SDK_TOOLS_ARCHIVE}"
60+
61+
# update Android SDK, install Android API, Build Tools...
62+
RUN mkdir --parents "${ANDROID_SDK_HOME}/.android/" \
63+
&& echo '### User Sources for Android SDK Manager' \
64+
> "${ANDROID_SDK_HOME}/.android/repositories.cfg"
65+
66+
# accept Android licenses (JDK necessary!)
67+
RUN apt -y update -qq \
68+
&& apt -y install -qq --no-install-recommends openjdk-8-jdk \
69+
&& apt -y autoremove \
70+
&& apt -y clean
71+
RUN yes | "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" --licenses > /dev/null
72+
73+
# download platforms, API, build tools
74+
RUN "${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-19" && \
75+
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "platforms;android-27" && \
76+
"${ANDROID_SDK_HOME}/tools/bin/sdkmanager" "build-tools;26.0.2" && \
77+
chmod +x "${ANDROID_SDK_HOME}/tools/bin/avdmanager"
78+
79+
80+
ENV USER="user"
81+
ENV HOME_DIR="/home/${USER}"
82+
ENV WORK_DIR="${HOME_DIR}" \
83+
PATH="${HOME_DIR}/.local/bin:${PATH}"
84+
85+
# install system dependencies
86+
RUN apt -y update -qq \
87+
&& apt -y install -qq --no-install-recommends \
88+
python3 virtualenv python3-pip wget lbzip2 patch sudo \
89+
&& apt -y autoremove \
90+
&& apt -y clean
91+
92+
# build dependencies
93+
# https://buildozer.readthedocs.io/en/latest/installation.html#android-on-ubuntu-16-04-64bit
94+
RUN dpkg --add-architecture i386 \
95+
&& apt -y update -qq \
96+
&& apt -y install -qq --no-install-recommends \
97+
build-essential ccache git python3 python3-dev \
98+
libncurses5:i386 libstdc++6:i386 libgtk2.0-0:i386 \
99+
libpangox-1.0-0:i386 libpangoxft-1.0-0:i386 libidn11:i386 \
100+
zip zlib1g-dev zlib1g:i386 \
101+
&& apt -y autoremove \
102+
&& apt -y clean
103+
104+
# specific recipes dependencies (e.g. libffi requires autoreconf binary)
105+
RUN apt -y update -qq \
106+
&& apt -y install -qq --no-install-recommends \
107+
autoconf automake cmake gettext libltdl-dev libtool pkg-config \
108+
&& apt -y autoremove \
109+
&& apt -y clean
110+
111+
112+
# prepare non root env
113+
RUN useradd --create-home --shell /bin/bash ${USER}
114+
115+
# with sudo access and no password
116+
RUN usermod -append --groups sudo ${USER}
117+
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
118+
119+
120+
RUN pip3 install --upgrade cython==0.28.6
121+
122+
WORKDIR ${WORK_DIR}
123+
COPY . ${WORK_DIR}
124+
125+
# user needs ownership/write access to these directories
126+
RUN chown --recursive ${USER} ${WORK_DIR} ${ANDROID_SDK_HOME}
127+
USER ${USER}
128+
129+
# install python-for-android from current branch
130+
RUN virtualenv --python=python3 venv \
131+
&& . venv/bin/activate \
132+
&& pip3 install -e .

pythonforandroid/recipes/android/__init__.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
from __future__ import unicode_literals
12
from pythonforandroid.recipe import CythonRecipe, IncludedFilesBehaviour
23
from pythonforandroid.util import current_directory
34
from pythonforandroid.patching import will_build
45
from pythonforandroid import logger
56

67
from os.path import join
7-
from sys import version_info
88

99

1010
class AndroidRecipe(IncludedFilesBehaviour, CythonRecipe):
@@ -26,27 +26,35 @@ def get_recipe_env(self, arch):
2626

2727
def prebuild_arch(self, arch):
2828
super(AndroidRecipe, self).prebuild_arch(arch)
29+
ctx_bootstrap = self.ctx.bootstrap.name
2930

31+
# define macros for Cython, C, Python
3032
tpxi = 'DEF {} = {}\n'
3133
th = '#define {} {}\n'
3234
tpy = '{} = {}\n'
3335

34-
bootstrap = self.ctx.bootstrap.name
35-
bootstrap = bootstrap_name = bootstrap if version_info >= (3, ) else bootstrap.decode('utf-8')
36+
# make sure bootstrap name is in unicode
37+
if isinstance(ctx_bootstrap, bytes):
38+
ctx_bootstrap = ctx_bootstrap.decode('utf-8')
39+
bootstrap = bootstrap_name = ctx_bootstrap
40+
3641
is_sdl2 = bootstrap_name in ('sdl2', 'sdl2python3', 'sdl2_gradle')
3742
is_pygame = bootstrap_name in ('pygame',)
3843
is_webview = bootstrap_name in ('webview',)
3944

4045
if is_sdl2 or is_webview:
4146
if is_sdl2:
4247
bootstrap = 'sdl2'
43-
java_ns = u'org.kivy.android'
44-
jni_ns = u'org/kivy/android'
48+
java_ns = 'org.kivy.android'
49+
jni_ns = 'org/kivy/android'
4550
elif is_pygame:
46-
java_ns = 'org.renpy.android'
47-
jni_ns = 'org/renpy/android'
51+
java_ns = b'org.renpy.android'
52+
jni_ns = b'org/renpy/android'
4853
else:
49-
logger.error('unsupported bootstrap for android recipe: {}'.format(bootstrap_name))
54+
logger.error((
55+
'unsupported bootstrap for android recipe: {}'
56+
''.format(bootstrap_name)
57+
))
5058
exit(1)
5159

5260
config = {
@@ -58,22 +66,28 @@ def prebuild_arch(self, arch):
5866
'JNI_NAMESPACE': jni_ns,
5967
}
6068

69+
# create config files for Cython, C and Python
6170
with (
6271
current_directory(self.get_build_dir(arch.arch))), (
6372
open(join('android', 'config.pxi'), 'w')) as fpxi, (
6473
open(join('android', 'config.h'), 'w')) as fh, (
6574
open(join('android', 'config.py'), 'w')) as fpy:
75+
6676
for key, value in config.items():
6777
fpxi.write(tpxi.format(key, repr(value)))
6878
fpy.write(tpy.format(key, repr(value)))
69-
fh.write(th.format(key,
70-
value if isinstance(value, int)
71-
else '"{}"'.format(value)))
79+
80+
fh.write(th.format(
81+
key,
82+
value if isinstance(value, int) else '"{}"'.format(value)
83+
))
7284
self.config_env[key] = str(value)
7385

7486
if is_sdl2:
7587
fh.write('JNIEnv *SDL_AndroidGetJNIEnv(void);\n')
76-
fh.write('#define SDL_ANDROID_GetJNIEnv SDL_AndroidGetJNIEnv\n')
88+
fh.write(
89+
'#define SDL_ANDROID_GetJNIEnv SDL_AndroidGetJNIEnv\n'
90+
)
7791
elif is_pygame:
7892
fh.write('JNIEnv *SDL_ANDROID_GetJNIEnv(void);\n')
7993

0 commit comments

Comments
 (0)