Skip to content

Latest commit

 

History

History
255 lines (186 loc) · 10.5 KB

README-Android.asciidoc

File metadata and controls

255 lines (186 loc) · 10.5 KB

Compile Retroshare for Android

Compiling an application for Android is not as easy as one would imagine, expecially one like RetroShare that has a big codebase and is not well documented. This document is aimed to empower the reader so she can hopefully succed or at least have a significant help in compiling her own RetroShare APK installable on Android.

Preparing The Environement

First of all setup your Qt for Android development environement following the guide on the Qt for android web site. At this point you should have Android SDK, Android NDK, and Qt for Android working fine, and you should be capable of executing on an Android emulator or on your Android phone Qt for Android examples.

But RetroShare is not as simple to compile as those examples. The good news is that Android NDK ships all the necessary to build a custom toolchain that is suitable to build RetroShare. In order to build the toolchain with needed libraries RetroShare provides the android-prepare-toolchain.sh script; before you execute it you should define some variables the script cannot determine in an easy and reliable manner by itself in your terminal.

## The path where Android NDK is installed in your system
export ANDROID_NDK_PATH="/opt/android-ndk/"

## The path where your fresh compiled toolchain will be installed, make sure
## the parent exists
export NATIVE_LIBS_TOOLCHAIN_PATH="${HOME}/Builds/android-toolchains/retroshare-android/"

## The CPU architecture of the Android device you want to target
export ANDROID_NDK_ARCH="arm"

## The Android API level the Android device you want to target
export ANDROID_PLATFORM_VER="19"

## The number of core that yout host CPU have (just to speed up compilation) set
## it to 1 if unsure
export HOST_NUM_CPU=1

./android-prepare-toolchain.sh

Preparing Qt Creator

Now that your environement is set up you should configure Qt Creator for Android following the official guide. At the end of this step your Qt Creator should recognize the Android compiler and the Qt for Android kit.

Your Kit is now ready to use. Now you can open RetroShare as a Qt Creator project and in the Projects left menu add the newly created kit if not already present, so you can select it on the build type selection button down on the left.

As we use a custom toolchain one more step is needed

_Qt Creator left pane → Projects → Build and Run → Android SOMESTUFF kit → Build Environement → Add

Variable: NATIVE_LIBS_TOOLCHAIN_PATH Value: Same value as NATIVE_LIBS_TOOLCHAIN_PATH in Preparing The Environement step

Some of RetroShare modules like retroshare-gui and WebUI are not available on Android so to be able to compile RetroShare without errors you will have to go to

Qt Creator left pane → Projects → Build and Run → Android SOMESTUFF kit → Build Steps → qmake → Additional arguments

and add the following configurations

CONFIG+=no_retroshare_gui CONFIG+=no_retroshare_nogui CONFIG+=no_retroshare_plugins CONFIG+=retroshare_android_service CONFIG+=libresapilocalserver CONFIG+=no_libresapihttpserver CONFIG+=retroshare_qml_app
Tip
Some versions of QtCreator try to find the Android SDK in /opt/android/sdk. A workaround to this is to make a symbolic link there pointing to your SDK installation path, like mkdir -p /opt/android/sdk && ln -s /home/user/android-sdk-linux /opt/android/sdk

Quircks

Protected Apps

On some Android devices like Huawei ALE-L21 background applications are killed when screen is turned off unless they are in the protected app list. At moment seems apps developers don’t have a way to have the application protected by default, unless the phone vendor decide the app is popular so the user have to enable protection for RetroShare manually on those mobile phones.


To enable enable protection: Android menu → Settings → Privacy & security → Protected apps → RetroShare

Other devices may offer similar features please report them.

APK signature mismatch

If you try to install a RetroShare APK that comes from a different source (eg: if you try to upgrade from F-Droid when you originally installed an APK build by yourself) Android will prevent that from happening. In that case the only solution is to uninstall the app and then install the new APK but if you do it also the application data and your precious cryptographic keys, friend list etc. will be lost forever. To avoid that you can attempt to manually backup and then restore from the command-line (adb backup seems not working either) to change the app source without erasing the appliation data.

Caution
Following steps require root access on your Android device
Backup RetroShare Android application data
export ORIG_DIR="/data/data/org.retroshare.android.qml_app"
export BACKUP_DIR="org.retroshare.android.qml_app.backup"

adb root

adb shell am force-stop org.retroshare.android.qml_app
sleep 1s

mkdir ${BACKUP_DIR}

# Avoid adb pull failing
adb shell rm ${ORIG_DIR}/files/.retroshare/libresapi.sock
adb pull ${ORIG_DIR}/files/ ${BACKUP_DIR}/files/

After this you should be able to uninstall the old APK with your preferred method, as example from the command-line.

Uninstall RetroShare Android from the command-line
adb uninstall org.retroshare.android.qml_app

Now you can install a different signature APK and then restore the application data with the following commands.

export ORIG_DIR="/data/data/org.retroshare.android.qml_app"
export BACKUP_DIR="org.retroshare.android.qml_app.backup"

adb root

## Launch the app to make sure the parent directory exists and has proper owner
adb shell monkey -p org.retroshare.android.qml_app -c android.intent.category.LAUNCHER 1
sleep 1s

adb shell am force-stop org.retroshare.android.qml_app
sleep 1s


APP_OWNER="$(adb shell busybox ls -lnd ${ORIG_DIR} | awk '{print $3":"$4}')"
adb shell rm -rf ${ORIG_DIR}/files
adb push ${BACKUP_DIR}/files/ ${ORIG_DIR}/files/
adb shell busybox chown -R ${APP_OWNER} ${ORIG_DIR}/files/

Opening RetroShare android app now should show your old profile.

Debugging with GDB

QtCreator actually support debugging only for the foreground activity, so to debug what’s happening in the core extra trickery is needed.

  • Run the App in Debug mode from QtCreator "Start Debugging" button

  • Enable QtCreator debugger console view Menu→Window→Debugger Log

  • Run show solib-search-path in the QtCreator GDB console

  • Take note of the output you get in the right pane of the console

  • Thanks https://stackoverflow.com/a/31164313 for the idea

Tip
QtCreator GDB console seems a bit buggy and easly trigger timeout message when a command is run, in that case attempt to run the command while the debugging is paused or at breakpoint, or with other similar tricks.
Caution
Following steps require root access on your Android device

Now on your phone yuo need to authorize root access to applications, then once you plug your sacrifical Android phone run this commands

Run gdbserver as root on Android phone
## Open a shell from your workstation on the connected Android phone
adb shell

## take the note of the phone IP address
ip address show

## Gain root permissions on the shell
su

## Attach with gdbserver and listen on one TCP port
gdbserver :4567 --attach $(pgrep org.retroshare.android.qml_app:rs)
Prepare and run Android NDK GDB on your workstation
## Setup some convenience variables
NDK_GDB="${ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gdb"
RS_BUILD_PATH="${HOME}/Builds/RetroShare-Android_for_armeabi_v7a_GCC_4_9_Qt_5_9_2_android_armv7-Debug/"

## Start Android NDK GDB of your phone architecture passing the executable
$NDK_GDB $RS_BUILD_PATH/retroshare-android-service/src/libretroshare-android-service.so

## Instruct GDB how and where to find debugging symbols
(gdb) set auto-solib-add on
(gdb) set solib-search-path THE:BIG:LIST:OF:DIRECTORIES:YOU:TAKE:NOTE:BEFORE

## Connect to the gdbserver running on the phone
(gdb) target remote $PHONE_IP:4567

## Have fun debugging
(gdb)
Tip
Some time WiFi power saving on Android mess with the GDB connection, to prevent that from appening open another adb shell and live ping toward your work-station running

Furter Readings