Skip to content

Instantly share code, notes, and snippets.

@raphendyr
Last active December 9, 2023 12:20
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raphendyr/fc0e00a17a69c9054037b478fa4425c1 to your computer and use it in GitHub Desktop.
Save raphendyr/fc0e00a17a69c9054037b478fa4425c1 to your computer and use it in GitHub Desktop.
Google Play Services installation to Sailfish X

Google Play Services for Alien Dalvik in Sailfish OS

This guide is currently updated to Sailfish 4.5. Some things have changed, so check the history if you need guide for an older version.

Devices I have tested the guide:

  • Xperia 10 II
  • Currently not working: Xperia XA2

Requirements

You need unzip, wget and squashfs-tools.

If you have Debian/Ubuntu/etc. run the following:

sudo apt install unzip wget squashfs-tools

Instructions

In the host Linux machine

  • retrieve OGApps pico from: https://opengapps.org/

    • Xperia 10 II: ARM64 / Android 11.0
    • Xperia XA 2: ARM / Android 10.0 (interestingly, the hardware supports 64bit..)
  • download also md5 checksum

  • copy /opt/alien/system.img to a Linux, e.g.:

    scp defaultuser@PHONE_IP:/opt/alien/system.img system-original.img
  • extract required apps and support files, i.e, run the following in bash/zsh/ksh:

zip=$(ls open_gapps-arm*-*-pico-*.zip | sort -rn -k3,5 -t- | head -n1)
echo "USING PACKAGE $zip, CHECK THAT IT HAS CORRECT ANDROID VERSION\!"
if md5sum -c "$zip.md5"; then
  mkdir -p to-system/
  for f in \
      Core/defaultetc-common.tar.lz \
      Core/defaultframework-common.tar.lz \
      'Core/gmscore-*.tar.lz' \
      Core/googlebackuptransport-all.tar.lz \
      Core/googleonetimeinitializer-all.tar.lz \
      Core/googlepartnersetup-all.tar.lz \
      Core/gsfcore-all.tar.lz \
      'Core/setupwizarddefault-*.tar.lz' \
      'Core/vending-*.tar.lz' \
      ; do
    echo ".. $f:"
    unzip -p "$zip" "$f" | tar -vx --lzip --strip-components 2 -C to-system/
  done
  # remove execute from data files
  find to-system/ -type f -print0 | xargs -r0 chmod -x
  # extract shared libs
  if [ "$zip" = "${zip#*-arm64-}" ]; then native=armeabi-v7a; else native=arm64-v8a; fi
  for apk in to-system/priv-app/*/*.apk; do
    pushd "${apk%/*}"
    rm -rf lib/*/
    unzip -x "${apk##*/}" "lib/$native/*"
    [ -e lib/armeabi-v7a ] && mv lib/armeabi-v7a lib/arm
    popd
  done
fi
  • optionally, you can include Aurora Store as system app at this point (fork of YalpStore)

    Google Play store works with Xperia 10 II, so if you prefer that, no need to install this.

    mkdir -p to-system/priv-app/AuroraStore
    wget \
      -U 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36' \
      -O to-system/priv-app/AuroraStore/AuroraStore.apk \
      'https://files.auroraoss.com/AuroraStore/Stable/AuroraStore_4.1.1.apk'
    echo "0c4a74b84c94deec64f68d75fd18e73fbaa8393fb324945e6c25cd8943418507 to-system/priv-app/AuroraStore/AuroraStore.apk" | sha256sum -c

    If the last line, doesn't say OK, then there was a problem with the download, do not use the image! .

  • open squashfs image, fix, copy apps and repackage (requries squashfs-tools)

    NOTE: If you find a better method to keep file owners and permissions than sudo, let me know!

    sudo bash -c "
      # unpack
      root=/dev/shm/sailfish-android
      unsquashfs -d \$root system-original.img
      # copy
      cp -vr to-system/* \$root/system/
      # fix ownership of new files
      find to-system -type f | while read f; do chown 500000:500000 \"\$root/system/\${f#*/}\"; done
      # repackage
      mksquashfs \$root/ system.img -comp lz4 -Xhc -noappend -no-exports -no-duplicates -no-fragments
      # clean
      chown $USER:$(id -ng) system.img
      rm -rf \$root
    "
  • upload image scp system.img defaultuser@PHONE_IP:/tmp/

  • cleaup rm -rf to-system system.img (note: let's keep original-system.img as a backup)

In your phone, e.g. ssh defaultuser@PHONE_IP. Remember to be a root devel-su.

  • Install the new image:

    # stop alien
    systemctl stop aliendalvik
    # backup system.img
    cp /opt/alien/system.img /home/alien-system.img
    # replace it
    cp /tmp/system.img /opt/alien/system.img
    rm /tmp/system.img
    # clear system app cache
    rm -vrf /home/.android/data/system/package_cache/*/Settings-*
    # remove updated google apps
    rm -vrf /home/.android/data/app/com.android.vending-*
    rm -vrf /home/.android/data/app/com.google.android.gms-*
    # fix android bootup with Sailfish 4.1
    rm -vrf /home/.android/data/misc/audioserver/
    # force rerunning permission and storage migrations
    rm /home/defaultuser/android_storage/.privilege_migration_done
    rm /home/defaultuser/android_storage/.storage_migration_done
    # you can start alien now
    systemctl start aliendalvik
  • Note that startup after the installation will take a long time as the Android is caching and compiling files. You can follow the progress with the following command. However, it will show a lot of errors and warnings, so it might be alarming, even though everything is ok.

    appsupport-attach /system/bin/logcat

When you install google play services for the first time:

  • open google play and sign in

  • retrieve GSF ID:

    Sadly, neither Sailfish 4 or Android contains sqlite3 anymore, so you need to do some manual work. Either copy the database to your computer or install sqlite3 to your phone.

    Copy:

    # on sailfish after devel-su
    cp /home/.android/data/data/com.google.android.gsf/databases/gservices.db /tmp/
    chmod a+r /tmp/gservices.db
    # on your computer
    scp defaultuser@PHONE_IP:/tmp/gservices.db
    sqlite gservices.db 'select * from main where name = "android_id";'
    # copy the id from the output
    rm gservices.db
    # on sailfish after devel-su
    rm /tmp/gservices.db

    Or Install:

    # on sailfish after devel-su
    pkcon install python3-sqlite
    python3 -c 'import sqlite3;
      c = sqlite3.connect("/home/.android/data/data/com.google.android.gsf/databases/gservices.db").cursor();
      c.execute("select value from main where name=\"android_id\";");
      print(c.fetchone()[0])'
  • Register your device: https://www.google.com/android/uncertified/

Known issues:

  • Android/apps believe the device is rooted (well, it is, but they shouldn't know that).
  • Fast location doesn't work with wi-fi polling
  • Google Backup Transport doesn't seem to work (requires investication)
  • Xperia XA2:
    • Google Play Store can't install applications. Seems android biometric interface is not implemented (at least up to SFOS 4.1, works in Xperia 10 II SFOS 4.2)

Troubleshooting:

  • for location service, disable google wi-fi checking, it doesn't work. This means that accurate location will take some time, of course (e.g. Jodel will take few minutes to login).

  • when aliendalvik is running, you can stream android logs with

    appsupport-attach /system/bin/logcat
  • If google backup is disabled, you can re-enable it:

    # enter Android shell
    appsupport-attach /system/bin/sh
    
    # in Android system
    bmgr transport com.google.android.backup/.BackupTransportService
@lotheac
Copy link

lotheac commented May 18, 2022

copying gservices.db off device is unnecessary, you can do this instead:

# pkcon install python3-sqlite
# python3 -c 'import sqlite3; c = sqlite3.connect("/home/.android/data/data/com.google.android.gsf/databases/gservices.db").cursor(); c.execute("select value from main where name=\"android_id\";"); print(c.fetchone()[0])'

@CosmicDuck
Copy link

Is there allready noticed with last update to SFOS 4.5 it looks like that Android 11 is installed? What must been changed because as hopefully understood correctly SFOS has moved the directory storage for android?

@raphendyr
Copy link
Author

Is there allready noticed with last update to SFOS 4.5 it looks like that Android 11 is installed? What must been changed because as hopefully understood correctly SFOS has moved the directory storage for android?

I have not yet installed the update (I know, I'm bad). I hope to do that on the weekend. I like to reserve some time without need for my phone, as this process sometimes requires some engineering.

Sadly, I have only one phone currently as the XA 2 didn't work without battery.

@raphendyr
Copy link
Author

raphendyr commented Mar 13, 2023

@CosmicDuck Not much was needed to change to get 4.5 "installed", however Google Play Store is unable to install packages. Aurora Store was able to update apps. EDIT: I updated a fix for the storage.

@CosmicDuck
Copy link

CosmicDuck commented Mar 13, 2023

PlayStore Icon is not found and the database gservices.db also not. But allready installed half year ago chrome and other google apps are now working again. Any idea how I can start play store?
The step up to "start play store" are running well.

@raphendyr
Copy link
Author

Hmm.. I didn't check the first time case. I would love to have another device for that (I could reset the Android). However, I did check that the db file is there in my phone, but it's hard to say if that is in use or left from before. So, if you get the Store working, but you won't find the file, then I or you need to do some debugging.

Any how, the Play Store icon should be visible as long as the correct image is in use. Every (or most) OS updates do reset the /opt/alien/system.img to the original version from Jolla, so the above steps need to be done after every update (well, not the first time part, but creating the image part).

A script would be nice, but making it work for everyone is bit too much work. In addition, I prefer not to install too many packages in the phone. However, there exists some in forums (seems to attempt to work for everyone quite well), which you can check out. It seems to do mostly the same things. Didn't have # force rerunning permission and storage migrations step at the moment.

Any how, my guess is that you don't have the correct image in use. Thus repeat from the start and try to keep track of files. Make extra copies or write notes where needed.

@CosmicDuck
Copy link

Hmm, I reinstalled complete aliendavik and appsupport-system-unprivileged to recreate the /opt/alien/system.img.
Then restarted from scratch the steps above. By the way I also restarting the phone after that my old installed apps like whatsapp are running, but no google play store available. Is it possible I must install it first but where it is? Maybe can I simply use install the apk from the pico image but whats the name of the apk?
I tried also to reinit my android storage by moving /home/.android to another place. But then no play icon appears on sfos app screen. But installing aptoide and fdroid app from sf store is working well.
I think there is a mistake in my brain, hopefully you can find my fault.

@CosmicDuck
Copy link

Now I used a modified script from here so it can be used for aarch64/arm64 Xperia10III now.
All Gapps like Google, Chrome also GPlayStore are now installed after reboot an waiting some minutes so also my older instaled apps could now been updated with the Google Play Store App.
I thing it is a good idea to find the differences between your and my modified installations.

@raphendyr
Copy link
Author

The Google Play Store etc. go into the priv-app inside the squasfs image, hence it requires modification of that rootfs. I think there is some mistake in my guid or it's bit vague of something.

It's also possible that some parts of the code don't work with some terminal (e.g. fish or even bash). I have been using zsh, so I only have verified it with that. And this is actually really good reason to use a script, as that would define the shell.

If I find motivation, I could look how complicated it would be to create a Sailfish app out of this process. Or like a button to settings or such.

I have spend little bit going through how AppSupport starts the Android container, but there doesn't seem to be nice way to integrate Google Play services without modifying the squashfs. Well, we would beed to extract those few paths out of the image and mount them with lxc (latter is doable, former is error prone). I wish lxc had overlayfs or such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment