Subject: Hard-coded version code is unsuitable for automated F-Droid Android package builds

From: Peter Oliver <[email-masked]>
Date: Fri, 24 Jan 2025 17:13:10 +0000 (GMT)

On Sun, 19 Jan 2025, Peter Oliver wrote:

> I notice that F-Droid is able to automatically build packages when a new Git 
> tag is made.  However, in order to do that, it needs to be given an integer 
> version number (called a version code; 
> https://developer.android.com/studio/publish/versioning#versioningsettings). 
> It can look for that either in the name of a Git tag, by searching in a file 
> within Git, or by fetching some data over HTTP 
> (https://f-droid.org/en/docs/Build_Metadata_Reference/#UpdateCheckData).
>
> Currently, this is hard-coded to 30 in java/AndroidManifest.xml.in, but 
> something like 300093000 would be needed (for 30.0.93).  Could we set that 
> for them?

Attached are two patches addressing this.

-- 
Peter Oliver

From 7002fdc6c64ccaf1a5cb2e8cb8ac698a74ea1b55 Mon Sep 17 00:00:00 2001
From: Peter Oliver <[email-masked]>
Date: Fri, 24 Jan 2025 16:43:34 +0000
Subject: [PATCH 1/2] Note that .apk packages for Android can be installed with
 adb

* java/INSTALL: Suggest the `adb install' command for installing Android
packages.
---
 java/INSTALL | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/java/INSTALL b/java/INSTALL
index 24f5e0c2ad8..d1ac29525dd 100644
--- a/java/INSTALL
+++ b/java/INSTALL
@@ -67,6 +67,10 @@ built for.
 The generated package can be uploaded onto an SD card (or similar
 medium) and installed on-device.
 
+Alternatively, the package can be uploaded to and installed on a device
+in debug mode using the `adb install' command
+(https://developer.android.com/tools/adb).
+
 
 LOCATING NECESSARY FILES
 
-- 
2.48.1

From a4fa082ab9de4265518c337ae1ea0b16b0342850 Mon Sep 17 00:00:00 2001
From: Peter Oliver <[email-masked]>
Date: Fri, 24 Jan 2025 17:05:09 +0000
Subject: [PATCH 2/2] Set android:versionCode as an integer derived from the
 Emacs version

The version code is intended to be an integer that increments for each
Android package release
(https://developer.android.com/studio/publish/versioning#versioningsettings).

If we keep this updated under version control, then F-Droid (a
third-party Android package repository), can watch for that, and use it
to automatically build Emacs packages for Android each time a new Emacs
release is tagged
(https://f-droid.org/en/docs/Build_Metadata_Reference/#UpdateCheckData).

* admin/admin.el (set-version): Update versionCode in
java/AndroidManifest.xml.in.
* java/AndroidManifest.xml.in: Bump versionCode to 300093000.
* java/INSTALL: Note that Android packages can be downgraded with the
`adb install -d' command.
---
 admin/admin.el              | 7 +++++++
 java/AndroidManifest.xml.in | 8 ++------
 java/INSTALL                | 3 ++-
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/admin/admin.el b/admin/admin.el
index d98b8243f25..57335f95d3a 100644
--- a/admin/admin.el
+++ b/admin/admin.el
@@ -116,6 +116,13 @@ set-version
 		       (rx (and "AC_INIT" (1+ (not (in ?,)))
                                 ?, (0+ space) ?\[
                                 (submatch (1+ (in "0-9."))))))
+  (set-version-in-file root "java/AndroidManifest.xml.in"
+                       (apply #'format "%02d%02d%02d000"
+                              (mapcar #'string-to-number
+                                      (split-string version "\\.")))
+		       (rx (and "android:versionCode=\""
+                                (submatch (1+ (in digit)))
+                                "\"")))
   (set-version-in-file root "nt/README.W32" version
 		       (rx (and "version" (1+ space)
 				(submatch (1+ (in "0-9."))))))
diff --git a/java/AndroidManifest.xml.in b/java/AndroidManifest.xml.in
index 6243c5e13ba..e4874b5209b 100644
--- a/java/AndroidManifest.xml.in
+++ b/java/AndroidManifest.xml.in
@@ -18,11 +18,7 @@ You should have received a copy of the GNU General Public License
 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>. -->
 
 <!-- targetSandboxVersion must be 1, for otherwise fascist security
-     restrictions prevent Emacs from making HTTP connections.
-
-     Furthermore, versionCode should remain constant, rather than increase
-     with each major release, because it is impossible to downgrade an
-     installation with a greater value to an older version of Emacs.  -->
+     restrictions prevent Emacs from making HTTP connections. -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 	  package="org.gnu.emacs"
@@ -33,7 +29,7 @@ along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>. -->
 	  android:appCategory="productivity"
 	  @ANDROID_SHARED_USER_ID@
 	  @ANDROID_SHARED_USER_NAME@
-	  android:versionCode="30"
+	  android:versionCode="300093000"
 	  android:versionName="@version@">
 
   <!-- Paste in every permission in existence so Emacs can do
diff --git a/java/INSTALL b/java/INSTALL
index d1ac29525dd..f0015bd230b 100644
--- a/java/INSTALL
+++ b/java/INSTALL
@@ -69,7 +69,8 @@ medium) and installed on-device.
 
 Alternatively, the package can be uploaded to and installed on a device
 in debug mode using the `adb install' command
-(https://developer.android.com/tools/adb).
+(https://developer.android.com/tools/adb).  Use `adb install -d' if you
+wish to downgrade to an earlier package version.
 
 
 LOCATING NECESSARY FILES
-- 
2.48.1