Opening and displaying HTML page/game on Android

I’m trying to create the APK build with webview and ready html5-game inside it.

I tested first with simple loading page online and it works:

local request_id = webview.open(webview_id, "http://www.defold.com")

I managed how to place the game to APK via 2 steps (thanks @AGulev )

[project] 
bundle_resources = /res

and by creating the direcory in root:

\res\android\assets\www

and we can see that the web-direcory is attached now inside APK namegame.apk/assets/www

I’m trying to open via webview:

local feedback_html = sys.load_resource("file:///android_asset/www/index.html")
webview.open_raw(webview_id, feedback_html)

and nothing happens, does any know what I missed?

P.S.
ready project with simple web-game on phaser (swap to move the hero)

2 Likes

sys.load_resource loads a file from the “custom resources”, which are resources that you defined to be included into the “.arcd” file via the “project.custom_resources” setting.

These resources are “bundled resources”, and are only copied directly as-is into the .apk.

We don’t have a special way for you to read that data, but you should be able to use the the Lua “io” module I believe. You might need the sys.get_application_path() in order to get the app dir.

2 Likes

Additional documentation about file access here: https://defold.com/manuals/file-access/

2 Likes

Your example works if you replace

local feedback_html = sys.load_resource("file:///android_asset/www/index.html")
webview.open_raw(webview_id, feedback_html)

with

webview.open(webview_id, "file:///android_asset/www/index.html")

Everything you put into asset folder in apk (in Defold we use bundle_resources for that) is available by file:///android_asset/

For example if you try

webview.open(webview_id, "file:///android_asset/game.projectc")

You will see your game.project file =)

4 Likes

I made a pull request with a small fix for webview NE and now it works:

32.apk.zip (2.0 MB)

64.apk.zip (2.1 MB)

and x64 after Defold App Manifest generator tool (I exclude everything. All checkboxes are on):
testWebview.apk.zip (1.7 MB)

3 Likes

thanks a lot @AGulev !

1 Like