How to run the development app on an emulator

#1

Hi all,

I am trying to follow the guide Running the development app on device. I intend to run the app on an emulator. This is what I have so far:

# Start the emulator in the background. 
$HOME/Library/Android/sdk/emulator/emulator -avd Pixel_3a_API_30 >& /dev/null &

# Wait for the emulator to start
$HOME/Library/Android/sdk/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'

# Check whether we have the latest dmengine.apk
LATEST_SHA1=`curl http://d.defold.com/stable/info.json | jq -r .sha1`
LOCAL_SHA1=`cat android/local_dmengine_sha1.txt`

if [ "$LATEST_SHA1" != "$LOCAL_SHA1" ]
then
    echo "Local dmengine.apk out of date, downloading latest"
    curl "http://d.defold.com/archive/stable/$LATEST_SHA1/engine/armv7-android/dmengine.apk" --output android/dmengine.apk
    echo "$LATEST_SHA1" > android/local_dmengine_sha1.txt
fi

# Install dmengine in the emulator.
$HOME/Library/Android/sdk/platform-tools/adb install "android/dmengine.apk"

This all goes fine, and I can run dmengine happily on the emulator. However, when I open Defold and go to Project -> Target, I do not see anything there but “New Local Engine”.

Is there something I’m missing to enable Defold to see the emulator?

Thanks!

0 Likes

#2

I should say that I also tried building the apk of my actual game and installing that on the emulator. This works and I am able to run it, but I can’t see debug output so I can’t figure out why certain things don’t work (e.g. websocket issues). When doing this, I also still don’t see anything in the Target dropdown.

0 Likes

#3

Are you creating a debug build? Release builds will have all logging disabled.

0 Likes

#4

I’m creating a debug build, but I’m copying it to the device and then running it, so I have no place to look to see the output. Is it secretly being written to a file out of the emulator or something?

Regardless, I feel like the other approach, where I can build from the editor and deploy to the emulator, would be much better, since I can hot reload, see the output right there in the editor, etc.

0 Likes

#5

Can’t you view the console output using logcat when running from an emulator?

1 Like

#6

That indeed did the trick! Here is the full script I have now for anyone who comes across this later:

# Start the emulator in the background. 
$HOME/Library/Android/sdk/emulator/emulator -avd Pixel_3a_API_30 >& /dev/null &

# Wait for the emulator to start
$HOME/Library/Android/sdk/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'

# Build the game.
bob --archive --platform=armv7-android build bundle --variant debug --bundle-format apk

# Uninstall the previous installation of the game.
$HOME/Library/Android/sdk/platform-tools/adb uninstall com.example.mygame >& /dev/null

# Install the game in the emulator.
$HOME/Library/Android/sdk/platform-tools/adb install "../build/default/mygame/mygame.apk"

# Run the game.
adb shell am start -a android.intent.action.MAIN -n com.exameple.mygame/com.dynamo.android.DefoldActivity

# Get the PID of the app
while true
do
    APP_PID=`adb shell ps | grep com.example.mygame | tr -s [:space:] ' ' | cut -d' ' -f2`
    if [ -z $APP_PID ]
    then
        sleep 0.5
    else
        break
    fi
done

# Tail the logs of the app.
adb logcat --pid $APP_PID

I still wonder why the Defold editor can’t find dmengine running on the emulator though.

1 Like

#7

I seem to recall that you need to do some kind of port forwarding or something like that. It was a long time ago that I tried so I could be wrong.

1 Like