This is the repo for https://play.google.com/store/apps/details?id=atorch.statspuzzles.
Nearly all of the puzzles in this app can be solved by hand, with pen and paper. For code that checks some of the answers using simulations (Monte Carlo), see https://github.com/atorch/probability_puzzles_solutions.
Are you looking for the puzzles? They're defined in app/src/main/res/values/strings.xml.
Several contributors have translated the app into other languages:
- Thank you Roland Illig for translating the app into German
- Thank you Roberto Espiño for translating the app into Spanish
- Thank you Ahmad Alhour for translating the app into Arabic
Thank you Mariusz Gromada for publishing mXparser, which is used in SolvePuzzle.java.
Thank you Andrew Gelman for featuring the app on your blog, and thank you to everyone on stackoverflow who helped me figure out how to write an Android app, back in the dark ages before LLMs!
I first released this app in July 2014. The app has been rated 6,594 times as of October 2020, and has around 26,000 active users.
To run the unit tests for this project, execute the following command:
./gradlew test
To build the app and run all checks, including linting, execute the following command:
./gradlew build
This is the same command that is run in the GitHub Actions CI. Running this locally will help you find and fix issues before pushing your changes.
Running the instrumented tests (./gradlew connectedAndroidTest
) requires a connected Android device or an emulator. The following steps describe how to set up and run an Android emulator on Ubuntu entirely from the command line.
For better performance, the Android emulator should use KVM hardware acceleration.
First, check if your CPU supports virtualization:
sudo apt-get update
sudo apt-get install -y cpu-checker
kvm-ok
If the output includes KVM acceleration can be used
, you are good to go. Otherwise, you may need to enable virtualization (e.g., VT-x or AMD-V) in your computer's BIOS/UEFI settings.
Next, install required KVM packages and add your user to the kvm
group.
sudo apt-get install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
sudo adduser $USER kvm
You will need to reboot your system for the group change to take effect.
The Android command-line tools require a Java Development Kit.
sudo apt-get install -y openjdk-17-jdk
-
Download and set up the SDK:
# Download the latest command-line tools from Google wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip # Create the necessary directory structure and unzip the tools mkdir -p ~/android_sdk/cmdline-tools unzip commandlinetools-linux-*.zip -d ~/android_sdk/cmdline-tools mv ~/android_sdk/cmdline-tools/cmdline-tools ~/android_sdk/cmdline-tools/latest rm commandlinetools-linux-*.zip
-
Set environment variables: Add the following to your
~/.bashrc
or~/.zshrc
file:export ANDROID_HOME=$HOME/android_sdk export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin export PATH=$PATH:$ANDROID_HOME/platform-tools export PATH=$PATH:$ANDROID_HOME/emulator
Then, source the file to apply the changes (e.g.,
source ~/.bashrc
).
-
Accept SDK licenses:
sdkmanager --licenses
Accept all licenses by typing
y
and pressing Enter. -
Install the system image and other tools: The CI uses API level 33. We will install the corresponding system image.
sdkmanager "platform-tools" "build-tools;33.0.2" "system-images;android-33;default;x86_64" "emulator"
-
Create the Android Virtual Device (AVD):
avdmanager create avd -n pixel_33 -k "system-images;android-33;default;x86_64" --device "pixel_6"
This creates an AVD named
pixel_33
using the"pixel_6"
device profile, which simulates the screen size and characteristics of a Google Pixel 6 phone. The GitHub Actions CI is configured to use the same device profile to ensure that tests run in a consistent environment.
-
Start the emulator in the background:
emulator -avd pixel_33 -no-window &
Wait a minute or two for the emulator to fully boot up.
-
Run the connected tests:
./gradlew connectedAndroidTest
The Gradle script will automatically detect the running emulator and execute the tests on it.
To reproduce the CI environment for a specific locale, you need to start the emulator with the desired language settings. The CI runs tests against English, German, Spanish, and Arabic.
First, make sure the emulator is not running (adb emu kill
).
Then, start the emulator with the desired locale. For example, to start the emulator with the German locale, run:
emulator -avd pixel_33 -no-window -prop persist.sys.language=de -prop persist.sys.country=DE &
Wait for the emulator to boot up, and then run the tests:
./gradlew connectedAndroidTest
```bash
adb emu kill
```
You can also install and run the app on a physical Android device using the command line.
- On your Android device, open Settings.
- Go to About phone.
- Tap Build number seven times to enable Developer options.
- Go back to the main Settings menu, then go to System > Developer options.
- Enable USB debugging.
Connect your Android device to your computer using a USB cable. A prompt might appear on your device asking you to authorize the computer for debugging. Accept it.
-
Navigate to the project directory:
cd /home/adrian/probability_puzzles
-
Build the debug APK:
./gradlew assembleDebug
Note: Gradle uses an incremental build process. If you haven't made any code changes, Gradle may not rebuild the APK. To force a rebuild, you can run
./gradlew clean assembleDebug
. -
Install the APK on your device:
adb install -r app/build/outputs/apk/debug/app-debug.apk
The -r
flag reinstalls the app, keeping its data. After the command completes, the app will be installed on your device, and you can run it by tapping its icon.
You can also connect to your device wirelessly. This is especially useful if you have issues with USB drivers or cables.
- On your Android device, open Settings > System > Developer options.
- Enable Wireless debugging.
- Make sure your device and your computer are on the same Wi-Fi network.
You only need to pair your device once.
Option A: Pairing with QR Code (Recommended)
- In Android Studio, go to the Device Manager and select Pair devices using Wi-Fi.
- A QR code will be displayed.
- On your device, under Wireless debugging, select Pair device with QR code and scan the QR code on your computer.
Option B: Pairing with Command Line
- On your device, under Wireless debugging, select Pair device with pairing code.
- You will see a pairing code and an IP address with a port (e.g.,
192.168.1.100:41234
). - On your computer, run the following command, replacing the IP, port, and pairing code with the ones from your device:
adb pair 192.168.1.100:41234 123456
After pairing, you need to connect to your device.
- On your device, under Wireless debugging, you will see an IP address and port for the connection (e.g.,
192.168.1.100:38765
). This port is different from the pairing port. - On your computer, run the following command, replacing the IP and port with the ones from your device:
adb connect 192.168.1.100:38765
- You can verify the connection by running
adb devices
. You should see your device listed.
Wireless debugging can sometimes be tricky. Here are some common issues and how to solve them:
-
adb devices
showsoffline
:- On your device, go to Settings > System > Developer options and tap Revoke USB debugging authorizations.
- Toggle Wireless debugging off and on again.
- Reconnect to your device.
-
adb pair
fails withprotocol fault
:- This can be a network issue. Make sure you are not on a VPN.
- Check if your firewall is blocking the connection.
- Some routers have an "AP Isolation" feature that prevents devices from communicating. Make sure it's disabled.
- Restart the ADB server with
adb kill-server
.
-
Build fails with
IOException: Unable to delete directory
:- This is likely a file ownership issue caused by running Android Studio with
sudo
. Do not run Android Studio withsudo
! - To fix this, you need to change the ownership of the project files back to your user. Run the following command, replacing
your_user
with your username:sudo chown -R your_user:your_user /path/to/your/project
- You might also need to stop the Gradle daemon with
./gradlew --stop
.
- This is likely a file ownership issue caused by running Android Studio with
-
Installation fails with
INSTALL_FAILED_UPDATE_INCOMPATIBLE
:- This means the app is already installed with a different signature (e.g., from the Play Store).
- Uninstall the app from your device and try again. You can do this from the command line:
adb uninstall atorch.statspuzzles