Logonomic Companion for Android: Java relink kit
================================================

This kit rebuilds the Android package of the Logonomic Companion with a
modified Qt Java layer, a modified Qt, or modified application Java, and
installs the result under your own signing key.

What the kit contains
  app/, settings.gradle, gradle.properties
      The Companion's Gradle project: Java sources, resources, assets, the
      generated manifest and build.gradle, and the native libraries Logonomic
      builds itself (the application library and its non-Qt dependencies,
      including OpenSSL).
  modification/
      Logonomic's modification of Qt's QtWindow and QtInputDelegate classes:
      the original files, the modified files and the patch. app/src carries
      the modified files that Gradle compiles.
  relink-manifest.json
      Every input that belongs to Qt or to the Android NDK, by path and
      SHA-256. These inputs are not carried; see step 3.
  tools/
      fill_qt_inputs.py, which installs the recorded Qt and NDK inputs, and
      prepare_android_qt_keyboard.py, the producer of the filtered Qt runtime
      jar.
  legal/
      The license texts and a short notice.

What you provide
  - The stock Qt Android arm64-v8a kit named by "qt_version" in
    relink-manifest.json (see "Getting the stock Qt kit" below), or your own
    modified build of the same layout.
  - The Android SDK packages that app/build.gradle and relink-manifest.json
    name: platforms;android-<compileSdk>, build-tools;<buildToolsVersion>,
    ndk;<ndk_revision>, and platform-tools for adb (step 7). For this release
    of the Companion they are platforms;android-36, build-tools;36.0.0 and
    ndk;27.2.12479018.
  - JDK 17 or newer, and Python 3.8 or newer.
  - Network access for Gradle to resolve the Android Gradle plugin and
    androidx from Google's Maven repository and Maven Central.

Getting the stock Qt kit
  The Qt online installer provides it after you sign in with a Qt account.
  Without an account, download it from Qt's public online repository over
  HTTPS. Its directory for the kit is

      https://download.qt.io/online/qtsdkrepository/all_os/android/qt6_<V>/qt6_<V>_arm64_v8a/

  where <V> is qt_version without dots (6.12.0 gives 6120). Updates.xml in
  that directory lists its packages: each PackageUpdate has a Name, a Version
  and DownloadableArchives. Archive <A> of package <P> is
  <directory>/<P>/<Version><A>, and the same URL followed by .sha256 holds
  its published SHA-256. Take the archives whose names begin with qtbase,
  qtdeclarative and qtsvg from package qt.qt6.<V>.android_arm64_v8a, and the
  archives of packages qt.qt6.<V>.addons.qtmultimedia.android_arm64_v8a and
  qt.qt6.<V>.addons.qtwebsockets.android_arm64_v8a. Check each against its
  .sha256 file and extract them all into one directory, for example
  <Qt>/<version>/android_arm64_v8a; each archive holds part of that
  directory's contents (bin, jar, lib, plugins, qml, src).

  Step 3 checks every Qt file the kit uses against the SHA-256 recorded in
  relink-manifest.json, so any source that provides identical files will do.

Steps

1. Extract this kit into an empty directory with a short path.
   Windows rejects paths beyond 260 characters.

2. Install the Qt kit (above) and the Android SDK packages with Google's
   command-line tools into an SDK directory <sdk>, and accept the SDK
   licences. With sdkmanager, as in command-line tools 19:

       sdkmanager --sdk_root=<sdk> --licenses
       sdkmanager --sdk_root=<sdk> "platforms;android-36" "build-tools;36.0.0" "ndk;27.2.12479018" platform-tools

   Command-line tools 23 replace sdkmanager by the android program in the
   same directory, which shows the SDK terms when it first runs and names
   packages with "/":

       android --sdk=<sdk> sdk install platforms/android-36 build-tools/36.0.0 ndk/27.2.12479018 platform-tools

3. Install the recorded Qt and NDK inputs:

       python tools/fill_qt_inputs.py --qt-kit <Qt>/<version>/android_arm64_v8a --ndk <sdk>/ndk/<revision>

   The tool lists each recorded file that differs from the Qt kit or NDK you
   named as "modified" and copies it anyway; with the stock kit, the summary
   reports zero modified files in every class. A recorded file that is
   missing stops the tool before anything is copied. The tool overwrites the
   files it installs in this tree, including any edits you made to them since
   the last run, and it refuses a destination that is or passes through a
   symbolic link or junction.

4. Tell Gradle where the SDK is: write a file local.properties in the kit
   root containing the line

       sdk.dir=<path to the Android SDK>

   or set ANDROID_HOME.

5. Build the unsigned release APK:

       gradlew.bat --max-workers=<n> assembleRelease

   Use ./gradlew on Linux and macOS. Gradle runs tasks in parallel because
   gradle.properties enables it, and it uses every processor unless told
   otherwise. Pass --max-workers equal to the number of CPU slots you hold
   when your builds run under a build queue, for example
   queued-build --slots 2 -- gradlew.bat --max-workers=2 assembleRelease.
   The APK is app/build/outputs/apk/release/app-release-unsigned.apk.

6. From the kit root, align and sign the APK with a key you control. To
   create one, use keytool from the JDK; it asks for the keystore password:

       keytool -genkeypair -keystore my-release.p12 -storetype PKCS12 -alias my-key -keyalg RSA -keysize 3072 -validity 10000 -dname "CN=<your name>"

   Then:

       zipalign -P 16 -f -v 4 app/build/outputs/apk/release/app-release-unsigned.apk aligned.apk
       apksigner sign --ks my-release.p12 --out logonomic-companion.apk aligned.apk
       apksigner verify --verbose logonomic-companion.apk
       zipalign -c -P 16 -v 4 logonomic-companion.apk
       aapt2 dump badging logonomic-companion.apk

   apksigner asks for the keystore password, or reads it from an environment
   variable with --ks-pass env:<VARIABLE>. zipalign, apksigner and aapt2 are
   in the SDK build tools. The badging output shows the native code ABI
   (arm64-v8a only) and the minimum SDK level.

7. Install on an arm64 device or emulator with adb from the SDK's
   platform-tools. adb devices lists the serial number of each connected
   device and running emulator; name the one to install on:

       adb devices
       adb -s <serial> install logonomic-companion.apk

   Android rejects an update signed by a different key than the installed
   application, so uninstall the Logonomic-signed application first, or change
   applicationId in app/build.gradle so that both can be installed.

Changing the Java

  Edit the Java files under app/src (the application and the modified Qt
  classes) or under app/qt-java (Qt's stock Java sources and resources, as
  installed by step 3), then run step 5 again. Gradle tracks app/src and
  app/qt-java as source sets, so every Java or resource change needs a Gradle
  run. Repeat steps 6 and 7 for the new package.

  To build against a modified Qt, point step 3 at your modified kit.
  Native Qt libraries are replaced in app/libs/arm64-v8a, and
  app/libs/Qt6Android.jar is produced from the jar of that kit.

versionCode in app/build.gradle is 1 in this kit. Raise it for a package that
must install over an earlier build signed with the same key.
