Skip to content

Commit

Permalink
android build: add own logic to calculate versionCode
Browse files Browse the repository at this point in the history
Use our own logic to go from ELECTRUM_VERSION to numeric android versionCode,
instead of using the default conversion done by python-for-android.

Even before this, we were already patching p4a to modify their logic (see [0]).
This commit changes that logic again, and moves it into a separate script in our repo.
- calculation change is due to the f-droid maintainers asking for the
  arch code to be in the least significant digits (instead of most sig digits) (see [1])

I have pushed and changed to a new p4a branch, which is just a copy of the previous one
with 3 commits related to versionCode calc squashed.

[0]: SomberNight/python-for-android@edb7e4f
[1]: #9210 (comment)
  • Loading branch information
SomberNight committed Oct 14, 2024
1 parent e72b7ca commit d415d2a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions contrib/android/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ RUN cd /opt \
&& git remote add sombernight https://github.com/SomberNight/python-for-android \
&& git remote add accumulator https://github.com/accumulator/python-for-android \
&& git fetch --all \
# commit: from branch sombernight/qt6-wip (note: careful with force-pushing! see #8162) \
&& git checkout "58d21ad89b6182c0d70289d647eb85eaa412412c^{commit}" \
# commit: from branch sombernight/electrum_20240930 (note: careful with force-pushing! see #8162) \
&& git checkout "7197c1c28409fbeebd8494093349a2bfd770526a^{commit}" \
&& /opt/venv/bin/python3 -m pip install --no-build-isolation --no-dependencies -e .

# build env vars
Expand Down
61 changes: 61 additions & 0 deletions contrib/android/get_apk_versioncode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python3

import importlib.util
import os
import sys

ARCH_DICT = {
"x86_64": "4",
"arm64-v8a": "3",
"armeabi-v7a": "2",
"x86": "1",
}


def get_electrum_version() -> str:
project_root = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
version_file_path = os.path.join(project_root, "electrum", "version.py")
# load version.py; needlessly complicated alternative to "imp.load_source":
version_spec = importlib.util.spec_from_file_location('version', version_file_path)
version_module = version = importlib.util.module_from_spec(version_spec)
version_spec.loader.exec_module(version_module)
return version.ELECTRUM_VERSION


def get_android_versioncode(*, arch_name: str) -> int:
version_code = 0
# add ELECTRUM_VERSION
app_version = get_electrum_version()
app_version_components = app_version.split('.')
assert len(app_version_components) == 3, f"version str expected to have 3 components, but got {app_version!r}"
for i in app_version_components:
version_code *= 100
version_code += int(i)
# add arch
arch_code = ARCH_DICT[arch_name]
assert len(arch_code) == 1
version_code *= 10
version_code += int(arch_code)
# compensate for legacy scheme
# note: up until version 4.5.5, we used a different scheme for version_code.
# 4_______________4_05_05_00
# ^ android arch, ^ app_version (4.5.5.0)
# This offset ensures that all new-scheme version codes are larger than the old-scheme version codes.
offset_due_to_legacy_scheme = 45_000_000
version_code += offset_due_to_legacy_scheme
return version_code


if __name__ == '__main__':
try:
android_arch = sys.argv[1]
except Exception:
print(f"usage: {os.path.basename(__file__)} <android_arch>", file=sys.stderr)
sys.exit(1)
if android_arch not in ARCH_DICT:
print(f"usage: {os.path.basename(__file__)} <android_arch>", file=sys.stderr)
print(f"error: unknown {android_arch=}", file=sys.stderr)
print(f" should be one of: {list(ARCH_DICT.keys())}", file=sys.stderr)
sys.exit(1)
version_code = get_android_versioncode(arch_name=android_arch)
print(version_code, file=sys.stdout)
6 changes: 4 additions & 2 deletions contrib/android/make_apk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ if [[ "$2" == "all" ]] ; then
# build all apks
# FIXME failures are not propagated out: we should fail the script if any arch build fails
export APP_ANDROID_ARCHS=armeabi-v7a
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
make $TARGET
export APP_ANDROID_ARCHS=arm64-v8a
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
make $TARGET
#export APP_ANDROID_ARCHS=x86
#make $TARGET
export APP_ANDROID_ARCHS=x86_64
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
make $TARGET
else
export APP_ANDROID_ARCHS=$2
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
make $TARGET
fi

Expand Down

0 comments on commit d415d2a

Please sign in to comment.