Skip to content

Commit f1f1571

Browse files
committed
more fixes to get gradle build working
1 parent 13b77da commit f1f1571

File tree

6 files changed

+78
-59
lines changed

6 files changed

+78
-59
lines changed

pythonforandroid/bootstrap.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import importlib
77

88
from pythonforandroid.logger import (warning, shprint, info, logger,
9-
debug)
9+
debug, error)
1010
from pythonforandroid.util import (current_directory, ensure_dir,
1111
temp_directory, which)
1212
from pythonforandroid.recipe import Recipe
@@ -272,6 +272,15 @@ def fry_eggs(self, sitepackages):
272272
shprint(sh.mv, '-t', sitepackages, *files)
273273
shprint(sh.rm, '-rf', d)
274274

275+
def apk(self, build_mode, env):
276+
try:
277+
ant = sh.Command('ant')
278+
except sh.CommandNotFound:
279+
error('Could not find ant binary, please install it and make '
280+
'sure it is in your $PATH.')
281+
exit(1)
282+
return shprint(ant, build_mode, _tail=20, _critical=True, _env=env)
283+
275284

276285
def expand_dependencies(recipes):
277286
recipe_lists = [[]]

pythonforandroid/bootstraps/sdl2_gradle/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
1212
TODO:
1313
- test with crystax
14+
- hardcoded -lpython2.7 in build/jni/src/Android.mk
15+
- Android.mk require both -I, otherwise it fail in different points
1416
1517
"""
1618

1719
from pythonforandroid.toolchain import (
18-
Bootstrap, shprint, current_directory, info, warning, ArchARM, info_main)
19-
from pythonforandroid.utils import ensure_dir
20+
Bootstrap, shprint, current_directory, info, info_main)
21+
from pythonforandroid.util import ensure_dir
2022
from os.path import join, exists, curdir, abspath
21-
from os import walk, makedirs
23+
from os import walk
2224
import glob
2325
import sh
2426

pythonforandroid/bootstraps/sdl2_gradle/build/build.py

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@
1212
import shutil
1313
from zipfile import ZipFile
1414
import sys
15-
import re
1615

1716
from fnmatch import fnmatch
1817

1918
import jinja2
2019

21-
if os.name == 'nt':
22-
ANDROID = 'android.bat'
23-
ANT = 'ant.bat'
24-
else:
25-
ANDROID = 'android'
26-
ANT = 'ant'
27-
2820
curdir = dirname(__file__)
2921

3022
# Try to find a host version of Python that matches our ARM version.
@@ -265,9 +257,9 @@ def make_package(args):
265257
# Prepare some variables for templating process
266258
default_icon = 'templates/kivy-icon.png'
267259
default_presplash = 'templates/kivy-presplash.jpg'
268-
shutil.copy(args.icon or default_icon, 'res/drawable/icon.png')
260+
shutil.copy(args.icon or default_icon, 'src/main/res/drawable/icon.png')
269261
shutil.copy(args.presplash or default_presplash,
270-
'res/drawable/presplash.jpg')
262+
'src/main/res/drawable/presplash.jpg')
271263

272264
# If extra Java jars were requested, copy them into the libs directory
273265
if args.add_jar:
@@ -334,53 +326,34 @@ def make_package(args):
334326
service_names.append(name)
335327
render(
336328
'Service.tmpl.java',
337-
'src/{}/Service{}.java'.format(args.package.replace(".", "/"), name.capitalize()),
329+
'src/main/java/{}/Service{}.java'.format(args.package.replace(".", "/"), name.capitalize()),
338330
name=name,
339331
entrypoint=entrypoint,
340332
args=args,
341333
foreground=foreground,
342334
sticky=sticky,
343-
service_id=sid + 1,
344-
)
335+
service_id=sid + 1)
345336

346337
render(
347338
'AndroidManifest.tmpl.xml',
348-
'AndroidManifest.xml',
339+
'src/main/AndroidManifest.xml',
349340
args=args,
350341
service=service,
351342
service_names=service_names,
352-
url_scheme=url_scheme,
353-
)
343+
url_scheme=url_scheme)
354344

355345
render(
356346
'build.tmpl.gradle',
357347
'build.gradle',
358348
args=args,
359-
aars=aars,
360-
versioned_name=versioned_name)
349+
aars=aars)
361350

362351
render(
363352
'strings.tmpl.xml',
364353
'src/main/res/values/strings.xml',
365354
args=args,
366355
url_scheme=url_scheme,
367-
)
368-
369-
if args.sign:
370-
render('build.properties', 'build.properties')
371-
else:
372-
if exists('build.properties'):
373-
os.remove('build.properties')
374-
375-
with open(join(dirname(__file__), 'res',
376-
'values', 'strings.xml')) as fileh:
377-
lines = fileh.read()
378-
379-
with open(join(dirname(__file__), 'res',
380-
'values', 'strings.xml'), 'w') as fileh:
381-
fileh.write(re.sub(r'"private_version">[0-9\.]*<',
382-
'"private_version">{}<'.format(
383-
str(time.time())), lines))
356+
private_version=str(time.time()))
384357

385358

386359
def parse_args(args=None):
@@ -476,8 +449,6 @@ def parse_args(args=None):
476449
'filename containing xml. The filename should be '
477450
'located relative to the python-for-android '
478451
'directory'))
479-
ap.add_argument('--with-billing', dest='billing_pubkey',
480-
help='If set, the billing service will be added (not implemented)')
481452
ap.add_argument('--service', dest='services', action='append',
482453
help='Declare a new service entrypoint: '
483454
'NAME:PATH_TO_PY[:foreground]')
@@ -497,10 +468,6 @@ def parse_args(args=None):
497468
if args.name and args.name[0] == '"' and args.name[-1] == '"':
498469
args.name = args.name[1:-1]
499470

500-
if args.billing_pubkey:
501-
print('Billing not yet supported in sdl2 bootstrap!')
502-
exit(1)
503-
504471
if args.sdk_version == -1:
505472
args.sdk_version = args.min_sdk_version
506473

@@ -538,5 +505,4 @@ def parse_args(args=None):
538505

539506

540507
if __name__ == "__main__":
541-
542508
parse_args()

pythonforandroid/bootstraps/sdl2_gradle/build/jni/src/Android.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
1212
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
1313
start.c
1414

15+
LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/include/python2.7 $(EXTRA_CFLAGS)
1516
LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../python-install/include/python2.7 $(EXTRA_CFLAGS)
1617

1718
LOCAL_SHARED_LIBRARIES := SDL2
1819

1920
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog -lpython2.7 $(EXTRA_LDLIBS)
2021

22+
LOCAL_LDFLAGS += -L$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/lib $(APPLICATION_ADDITIONAL_LDFLAGS)
2123
LOCAL_LDFLAGS += -L$(LOCAL_PATH)/../../python-install/lib $(APPLICATION_ADDITIONAL_LDFLAGS)
2224

2325
include $(BUILD_SHARED_LIBRARY)

pythonforandroid/bootstraps/sdl2_gradle/build/templates/build.tmlp.gradle renamed to pythonforandroid/bootstraps/sdl2_gradle/build/templates/build.tmpl.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ android {
2626
minSdkVersion {{ args.min_sdk_version }}
2727
targetSdkVersion {{ args.sdk_version }}
2828
versionCode {{ args.numeric_version }}
29-
versionName {{ args.version }}
29+
versionName '{{ args.version }}'
3030
ndk {
3131
abiFilters 'armeabi-v7a'
3232
}
@@ -60,7 +60,9 @@ dependencies {
6060
{%- for aar in aars %}
6161
compile(name: '{{ aar }}', ext: 'aar')
6262
{%- endfor -%}
63+
{%- if args.depends -%}
6364
{%- for depend in args.depends %}
6465
compile '{{ depend }}'
6566
{%- endfor %}
67+
{%- endif %}
6668
}

pythonforandroid/toolchain.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def check_python_dependencies():
6464

6565
import sys
6666
from sys import platform
67-
from os.path import (join, dirname, realpath, exists, expanduser)
67+
from os.path import (join, dirname, realpath, exists, expanduser, basename)
6868
import os
6969
import glob
7070
import shutil
@@ -591,7 +591,7 @@ def clean(self, args):
591591
'Asked to clean "{}" but this argument is not '
592592
'recognised'.format(component)))
593593
component_clean_methods[component](args)
594-
594+
595595

596596
def clean_all(self, args):
597597
'''Delete all build components; the package cache, package builds,
@@ -741,18 +741,48 @@ def apk(self, args):
741741
build = imp.load_source('build', join(dist.dist_dir, 'build.py'))
742742
with current_directory(dist.dist_dir):
743743
self.hook("before_apk_build")
744+
os.environ["ANDROID_API"] = str(self.ctx.android_api)
744745
build_args = build.parse_args(args.unknown_args)
745746
self.hook("after_apk_build")
746747
self.hook("before_apk_assemble")
747748

748-
try:
749-
ant = sh.Command('ant')
750-
except sh.CommandNotFound:
751-
error('Could not find ant binary, please install it and make '
752-
'sure it is in your $PATH.')
753-
exit(1)
749+
if exists(join(dist.dist_dir, "templates", "build.tmpl.gradle")):
750+
# gradle-based build
751+
env["ANDROID_NDK_HOME"] = self.ctx.ndk_dir
752+
env["ANDROID_HOME"] = self.ctx.sdk_dir
753+
754+
gradlew = sh.Command('./gradlew')
755+
if args.build_mode == "debug":
756+
gradle_task = "assembleDebug"
757+
elif args.build_mode == "release":
758+
gradle_task = "assembleRelease"
759+
else:
760+
error("Unknown build mode {} for apk()".format(
761+
args.build_mode))
762+
exit(1)
763+
output = shprint(gradlew, gradle_task, _tail=20,
764+
_critical=True, _env=env)
765+
766+
# gradle output apks somewhere else
767+
# and don't have version in file
768+
apk_dir = join(dist.dist_dir, "build", "outputs", "apk")
769+
apk_glob = "*-{}.apk"
770+
apk_add_version = True
771+
772+
else:
773+
# ant-based build
774+
try:
775+
ant = sh.Command('ant')
776+
except sh.CommandNotFound:
777+
error('Could not find ant binary, please install it '
778+
'and make sure it is in your $PATH.')
779+
exit(1)
780+
output = shprint(ant, args.build_mode, _tail=20,
781+
_critical=True, _env=env)
782+
apk_dir = join(dist.dist_dir, "bin")
783+
apk_glob = "*-*-{}.apk"
784+
apk_add_version = False
754785

755-
output = shprint(ant, args.build_mode, _tail=20, _critical=True, _env=env)
756786
self.hook("after_apk_assemble")
757787

758788
info_main('# Copying APK to current directory')
@@ -770,16 +800,24 @@ def apk(self, args):
770800
suffix = args.build_mode
771801
if suffix == 'release' and not args.keystore:
772802
suffix = suffix + '-unsigned'
773-
apks = glob.glob(join(dist.dist_dir, 'bin', '*-*-{}.apk'.format(suffix)))
803+
apks = glob.glob(join(apk_dir, apk_glob.format(suffix)))
774804
if len(apks) == 0:
775805
raise ValueError('Couldn\'t find the built APK')
776806
if len(apks) > 1:
777-
info('More than one built APK found...guessing you '
807+
info('More than one built APK found... guessing you '
778808
'just built {}'.format(apks[-1]))
779809
apk_file = apks[-1]
780810

781811
info_main('# Found APK file: {}'.format(apk_file))
782-
shprint(sh.cp, apk_file, './')
812+
if apk_add_version:
813+
info('# Add version number to APK')
814+
apk_name, apk_suffix = basename(apk_file).split("-", 1)
815+
apk_file_dest = "{}-{}-{}".format(
816+
apk_name, build_args.version, apk_suffix)
817+
info('# APK renamed to {}'.format(apk_file_dest))
818+
shprint(sh.cp, apk_file, apk_file_dest)
819+
else:
820+
shprint(sh.cp, apk_file, './')
783821

784822
@require_prebuilt_dist
785823
def create(self, args):

0 commit comments

Comments
 (0)