2

This question is about loading HTML content from the assets folder into a WebView component inside of an Android Dynamic Feature Module when the app is loaded from Google Play. How can you do that? Right now I am getting a 'file not found' error when my code uses WebView.loadUrl.

Please note that this question is not about how to load HTML content into a WebView inside of the main Android 'app' module. It is also not about how to get this to run locally on the emulator - loading HTML content works fine when loaded on the emulator in the dynamic feature module.

My project has the following structure:

Top
   - app (parent module)
   | - src
     | - ...
   - module 1 (child module)
   | - src
     | - main
       | - assets (html is under this folder)
       | - java 
         | - org.example.fragment
           | - WebFragment

So module 1 is an Android feature module with a Fragment which loads a android.webkit.WebView which should load the assets. But loading of the HTML content does not work when the app is loaded from Google Play.

Below is the code of the Fragment in module 1 which is supposed to load the HTML:

public class WebFragment extends Fragment {

    protected static final String DEFAULT_LOCATION = "file:///android_asset/";

    protected WebView gameView;

    private static final String TAG = "MindGymWebFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        SplitCompat.install(getContext());
        View v = inflater.inflate(R.layout.fragment_web_mindgym, container, false);
        try {
            String gameToOpen = this.getArguments().getString("gameToOpen");
            gameView = v.findViewById(R.id.memory_game_view);
            setupSettings();
            boolean isMemory = "Memory".equals(gameToOpen);
            boolean isDestress = "DeStress".equals(gameToOpen);
            if (isMemory) {
                gameView.loadUrl(DEFAULT_LOCATION + "memory/memory.html");
            } else if (isDestress) {
                gameView.loadUrl(DEFAULT_LOCATION + "breathe/breathing.html");
            }
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.unselectHomeTab();
            if (isMemory || isDestress) {
                mainActivity.setDailyOnABar(getString(org.brahmakumaris.beezone.R.string.mind_gym));
            }

        } catch (Exception e) {
            Log.e(TAG, "Cannot initializa game", e);
        }
        return v;
    }

    protected void setupSettings() {
        WebSettings settings = gameView.getSettings();
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setBlockNetworkImage(false);
        settings.setBlockNetworkLoads(false);
        settings.setLoadsImagesAutomatically(true);
        settings.setDomStorageEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setMediaPlaybackRequiresUserGesture(false);
    }

    @Override
    public void onDestroy() {
        gameView.destroy();
        gameView = null;
        super.onDestroy();
    }

}

So: How can you load HTML from a dynamic feature module which is served from Google Play?

Update 1: the code above will work if the html assets are copied to the app/src/main/assets folder - but that is obviously not what we want. The webview of module 1 should be able to access the assets folders of module 1 when the module is downloaded from Google Play.

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76

1 Answers1

3

After loads of research I found this on the internet:

https://chromium.googlesource.com/chromium/src.git/+/master/docs/android_dynamic_feature_modules.md

Limitations DFMs have the following limitations:

WebView: We don't support DFMs for WebView. If your feature is used by WebView you cannot put it into a DFM.

So you cannot use a WebView in a dynamic feature module.

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76