Skip to content

Commit f94a49f

Browse files
committed
Added support for aab
1 parent a633193 commit f94a49f

File tree

27 files changed

+261
-190
lines changed

27 files changed

+261
-190
lines changed

pythonforandroid/archs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def get_env(self, with_flags_in_cc=True):
133133
ctx=self.ctx,
134134
command_prefix=self.command_prefix,
135135
python_includes=join(
136-
self.ctx.get_python_install_dir(),
136+
self.ctx.get_python_install_dir(self.arch),
137137
'include/python{}'.format(self.ctx.python_recipe.version[0:3]),
138138
),
139139
)

pythonforandroid/bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def strip_libraries(self, arch):
374374
if len(tokens) > 1:
375375
strip = strip.bake(tokens[1:])
376376

377-
libs_dir = join(self.dist_dir, '_python_bundle',
377+
libs_dir = join(self.dist_dir, f'_python_bundle__{arch.arch}',
378378
'_python_bundle', 'modules')
379379
filens = shprint(sh.find, libs_dir, join(self.dist_dir, 'libs'),
380380
'-iname', '*.so', _env=env).stdout.decode('utf-8')

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def make_package(args):
267267
# Package up the private data (public not supported).
268268
use_setup_py = get_dist_info_for("use_setup_py",
269269
error_if_missing=False) is True
270-
tar_dirs = [env_vars_tarpath]
270+
private_tar_dirs = [env_vars_tarpath]
271271
_temp_dirs_to_clean = []
272272
try:
273273
if args.private:
@@ -277,7 +277,7 @@ def make_package(args):
277277
):
278278
print('No setup.py/pyproject.toml used, copying '
279279
'full private data into .apk.')
280-
tar_dirs.append(args.private)
280+
private_tar_dirs.append(args.private)
281281
else:
282282
print("Copying main.py's ONLY, since other app data is "
283283
"expected in site-packages.")
@@ -309,10 +309,7 @@ def make_package(args):
309309
)
310310

311311
# Append directory with all main.py's to result apk paths:
312-
tar_dirs.append(main_py_only_dir)
313-
for python_bundle_dir in ('private', '_python_bundle'):
314-
if exists(python_bundle_dir):
315-
tar_dirs.append(python_bundle_dir)
312+
private_tar_dirs.append(main_py_only_dir)
316313
if get_bootstrap_name() == "webview":
317314
for asset in listdir('webview_includes'):
318315
shutil.copy(join('webview_includes', asset), join(assets_dir, asset))
@@ -326,8 +323,13 @@ def make_package(args):
326323
shutil.copytree(realpath(asset_src), join(assets_dir, asset_dest))
327324

328325
if args.private or args.launcher:
326+
for arch in get_dist_info_for("archs"):
327+
libs_dir = f"libs/{arch}"
328+
make_tar(
329+
join(libs_dir, 'libpybundle.so'), [f'_python_bundle__{arch}'], args.ignore_path,
330+
optimize_python=args.optimize_python)
329331
make_tar(
330-
join(assets_dir, 'private.mp3'), tar_dirs, args.ignore_path,
332+
join(assets_dir, 'private.tar'), private_tar_dirs, args.ignore_path,
331333
optimize_python=args.optimize_python)
332334
finally:
333335
for directory in _temp_dirs_to_clean:
@@ -358,8 +360,8 @@ def make_package(args):
358360
print("WARNING: Received an --icon_fg or an --icon_bg argument, but not both. "
359361
"Ignoring.")
360362

361-
if args.enable_androidx:
362-
shutil.copy('templates/gradle.properties', 'gradle.properties')
363+
# if args.enable_androidx:
364+
shutil.copy('templates/gradle.properties', 'gradle.properties')
363365

364366
if get_bootstrap_name() != "service_only":
365367
lottie_splashscreen = join(res_dir, 'raw/splashscreen.json')
@@ -410,7 +412,6 @@ def make_package(args):
410412
version_code = 0
411413
if not args.numeric_version:
412414
# Set version code in format (arch-minsdk-app_version)
413-
arch = get_dist_info_for("archs")[0]
414415
arch_dict = {"x86_64": "9", "arm64-v8a": "8", "armeabi-v7a": "7", "x86": "6"}
415416
arch_code = arch_dict.get(arch, '1')
416417
min_sdk = args.min_sdk_version

pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonUtil.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected static void recursiveDelete(File f) {
127127
f.delete();
128128
}
129129

130-
public static void unpackData(
130+
public static void unpackAsset(
131131
Context ctx,
132132
final String resource,
133133
File target,
@@ -170,7 +170,74 @@ public static void unpackData(
170170
target.mkdirs();
171171

172172
AssetExtract ae = new AssetExtract(ctx);
173-
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
173+
if (!ae.extractTar(resource + ".tar", target.getAbsolutePath(), "private")) {
174+
String msg = "Could not extract " + resource + " data.";
175+
if (ctx instanceof Activity) {
176+
toastError((Activity)ctx, msg);
177+
} else {
178+
Log.v(TAG, msg);
179+
}
180+
}
181+
182+
try {
183+
// Write .nomedia.
184+
new File(target, ".nomedia").createNewFile();
185+
186+
// Write version file.
187+
FileOutputStream os = new FileOutputStream(diskVersionFn);
188+
os.write(dataVersion.getBytes());
189+
os.close();
190+
} catch (Exception e) {
191+
Log.w("python", e);
192+
}
193+
}
194+
}
195+
196+
public static void unpackPyBundle(
197+
Context ctx,
198+
final String resource,
199+
File target,
200+
boolean cleanup_on_version_update) {
201+
202+
Log.v(TAG, "Unpacking " + resource + " " + target.getName());
203+
204+
// The version of data in memory and on disk.
205+
String dataVersion = "p4aisawesome"; // FIXME: Assets method is not usable for fake .so files bundled as a library.
206+
String diskVersion = null;
207+
208+
Log.v(TAG, "Data version is " + dataVersion);
209+
210+
// If no version, no unpacking is necessary.
211+
if (dataVersion == null) {
212+
return;
213+
}
214+
215+
// Check the current disk version, if any.
216+
String filesDir = target.getAbsolutePath();
217+
String diskVersionFn = filesDir + "/" + resource + ".version";
218+
219+
// FIXME: Keeping that for later. Now it is surely failing.
220+
try {
221+
byte buf[] = new byte[64];
222+
InputStream is = new FileInputStream(diskVersionFn);
223+
int len = is.read(buf);
224+
diskVersion = new String(buf, 0, len);
225+
is.close();
226+
} catch (Exception e) {
227+
diskVersion = "";
228+
}
229+
230+
// If the disk data is out of date, extract it and write the version file.
231+
if (! dataVersion.equals(diskVersion)) {
232+
Log.v(TAG, "Extracting " + resource + " assets.");
233+
234+
if (cleanup_on_version_update) {
235+
recursiveDelete(target);
236+
}
237+
target.mkdirs();
238+
239+
AssetExtract ae = new AssetExtract(ctx);
240+
if (!ae.extractTar(resource + ".so", target.getAbsolutePath(), "pybundle")) {
174241
String msg = "Could not extract " + resource + " data.";
175242
if (ctx instanceof Activity) {
176243
toastError((Activity)ctx, msg);

pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/AssetExtract.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.FileOutputStream;
1414
import java.io.FileNotFoundException;
1515
import java.io.File;
16+
import java.io.FileInputStream;
1617

1718
import java.util.zip.GZIPInputStream;
1819

@@ -28,15 +29,20 @@ public AssetExtract(Context context) {
2829
mAssetManager = context.getAssets();
2930
}
3031

31-
public boolean extractTar(String asset, String target) {
32+
public boolean extractTar(String asset, String target, String method) {
3233

3334
byte buf[] = new byte[1024 * 1024];
3435

3536
InputStream assetStream = null;
3637
TarInputStream tis = null;
3738

3839
try {
39-
assetStream = mAssetManager.open(asset, AssetManager.ACCESS_STREAMING);
40+
if(method == "private"){
41+
assetStream = mAssetManager.open(asset, AssetManager.ACCESS_STREAMING);
42+
} else if (method == "pybundle") {
43+
assetStream = new FileInputStream(asset);
44+
}
45+
4046
tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new BufferedInputStream(assetStream, 8192)), 8192));
4147
} catch (IOException e) {
4248
Log.e("python", "opening up extract tar", e);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
android.useAndroidX=true
22
android.enableJetifier=true
3+
android.bundle.enableUncompressedNativeLibs=false

pythonforandroid/bootstraps/sdl2/__init__.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ class SDL2GradleBootstrap(Bootstrap):
1515
def assemble_distribution(self):
1616
info_main("# Creating Android project ({})".format(self.name))
1717

18-
arch = self.ctx.archs[0]
19-
20-
if len(self.ctx.archs) > 1:
21-
raise ValueError("SDL2/gradle support only one arch")
22-
23-
info("Copying SDL2/gradle build for {}".format(arch))
18+
info("Copying SDL2/gradle build")
2419
shprint(sh.rm, "-rf", self.dist_dir)
2520
shprint(sh.cp, "-r", self.build_dir, self.dist_dir)
2621

@@ -33,23 +28,24 @@ def assemble_distribution(self):
3328
with current_directory(self.dist_dir):
3429
info("Copying Python distribution")
3530

36-
python_bundle_dir = join('_python_bundle', '_python_bundle')
37-
38-
self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)])
3931
self.distribute_javaclasses(self.ctx.javaclass_dir,
4032
dest_dir=join("src", "main", "java"))
4133

42-
ensure_dir(python_bundle_dir)
43-
site_packages_dir = self.ctx.python_recipe.create_python_bundle(
44-
join(self.dist_dir, python_bundle_dir), arch)
34+
for arch in self.ctx.archs:
35+
python_bundle_dir = join(f'_python_bundle__{arch.arch}', '_python_bundle')
36+
ensure_dir(python_bundle_dir)
37+
38+
self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)])
39+
site_packages_dir = self.ctx.python_recipe.create_python_bundle(
40+
join(self.dist_dir, python_bundle_dir), arch)
41+
if not self.ctx.with_debug_symbols:
42+
self.strip_libraries(arch)
43+
self.fry_eggs(site_packages_dir)
4544

4645
if 'sqlite3' not in self.ctx.recipe_build_order:
4746
with open('blacklist.txt', 'a') as fileh:
4847
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')
4948

50-
if not self.ctx.with_debug_symbols:
51-
self.strip_libraries(arch)
52-
self.fry_eggs(site_packages_dir)
5349
super().assemble_distribution()
5450

5551

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ private class UnpackFilesTask extends AsyncTask<String, Void, String> {
104104
protected String doInBackground(String... params) {
105105
File app_root_file = new File(params[0]);
106106
Log.v(TAG, "Ready to unpack");
107-
PythonUtil.unpackData(mActivity, "private", app_root_file, true);
107+
PythonUtil.unpackAsset(mActivity, "private", app_root_file, true);
108+
PythonUtil.unpackPyBundle(mActivity, getApplicationInfo().nativeLibraryDir + "/" + "libpybundle", app_root_file, false);
108109
return null;
109110
}
110111

0 commit comments

Comments
 (0)