Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-102944

[Reg 5.15 -> 6.2] Relative QML import does not work from Android assets

XMLWordPrintable

    • Android
    • f5514e682b (qt/qtdeclarative/dev) 58d7d8bd82 (qt/qtdeclarative/6.3) 58d7d8bd82 (qt/tqtc-qtdeclarative/6.3) dc25d739b6 (qt/tqtc-qtdeclarative/6.2)

      Relative QML import does not work when deploying and loading QML via an android asset URL. ie. QML files are deployed to the android-build/assets folder and loaded via assets:/ URL.

      Cmake:

      file(GLOB_RECURSE QmlFolder RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} qml/*.qml)
      install(FILES ${QmlFolder} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/android-build/assets")

      main.cpp:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      int main(int argc, char *argv[])
      {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
      
        engine.load("assets:/qml/main.qml");
        return app.exec();
      }
      

      qml/main.qml:

      import QtQuick
      
      import "pages"
      
      Window {
        visible: true
      
        MainPage { }
      }
      

      qml/pages/MainPage.qml:

      import QtQuick
      
      Rectangle {
        color: "white"
        anchors.fill: parent
      
        Text {
          text: "Qt 6"
        }
      
        Image {
          source: "../../images/logo.png"
        }
      }
      

      Gives the error message:

      file:assets:/qml/main.qml:3:1: import "pages" has no qmldir and no namespace

      This worked with Qt 5.

      Note: when switching the import statement to:

      import "assets:/qml/pages"

      It works like expected.

      Note how the URL starts with assets:/ here and not file:assets:/. Maybe that is the source of the problem?

      The relative lookup of the image placed in assets/images/logo.png also works fine.

      The use case here is to directly deploy the QML files in the android assets instead of in QRC resources during development.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            ulherman Ulf Hermann
            Chrisu Christian Bartsch
            Votes:
            5 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes