由於 Ubuntu 手機平台的安全限制,應用程式只能存取自己的空間。若需要的檔案不在應用沙箱內,就完全讀不到;應用只能使用系統提供的函式庫,一旦系統沒有你要的庫,就得自己動手。實務上有兩條路:
Ubuntu SDK 提供了帶 C++ plugin 的標準模板可以套用。第二種做法對那些不在 SDK 標準庫清單中的元件特別有用——例如拿到 PDF reader 的庫原始碼自行編譯,再把 .so 一起打包安裝到手機。本文就針對第二種方式詳細說明。
先取得範例程式碼:
$ git clone https://github.com/liu-xiao-guo/studentlib_shared
#include <string>
class Student {
private:
std::string name;
public:
Student(std::string);
virtual void display();
};
#include <iostream>
#include "Student.h"
using namespace std;
Student::Student(string name) : name(name) {}
void Student::display() {
cout << "A student with name " << this->name << endl;
}
以下以 armhf chroot 為例(i386 做法相同)。先進到 chroot:
$ click chroot -aarmhf -fubuntu-sdk-15.04 run
接著在專案根目錄編譯:
$ mkdir build $ cd build $ cmake .. $ make
完成後 build 子目錄下就能看到 libtestStudent.so。
(click-ubuntu-sdk-15.04-armhf)liuxg@liuxg:~/qml/studentlib_shared/build$ ls CMakeCache.txt CMakeFiles Makefile cmake_install.cmake libtestStudent.so
選擇「QML App with C++ plugin (qmake)」模板建立 testplugin 專案。
#ifndef MYTYPE_H
#define MYTYPE_H
#include <QObject>
class MyType : public QObject
{
Q_OBJECT
Q_PROPERTY(QString helloWorld READ helloWorld WRITE setHelloWorld NOTIFY helloWorldChanged)
public:
explicit MyType(QObject *parent = 0);
Q_INVOKABLE void testlib();
~MyType();
Q_SIGNALS:
void helloWorldChanged();
protected:
QString helloWorld() { return m_message; }
void setHelloWorld(QString msg) {
m_message = msg;
Q_EMIT helloWorldChanged();
}
QString m_message;
};
#endif // MYTYPE_H
#include "mytype.h"
#include "Student.h"
MyType::MyType(QObject *parent) : QObject(parent), m_message("") {}
void MyType::testlib()
{
Student student("johnddfsfdfdfds");
student.display();
}
MyType::~MyType() {}
這裡已經用上了 shared 庫的 display() 方法。此時直接執行應用一定會出錯,因為它找不到庫。接著把庫打包進來。
在專案中建立 libs 目錄,把 .so 與標頭檔複製進去,並新增一個 libs.pro。目錄結構如下:
liuxg@liuxg:~/qml/testplugin$ tree -L 3 . ├── backend │ ├── Testplugin │ │ ├── backend.cpp │ │ ├── backend.h │ │ ├── mytype.cpp │ │ ├── mytype.h │ │ ├── qmldir │ │ └── Testplugin.pro │ └── tests │ └── unit ├── libs │ ├── libs.pro │ ├── libtestStudent.so │ └── Student.h ├── manifest.json.in ├── po │ └── testplugin.liu-xiao-guo.pot ├── testplugin │ ├── Main.qml │ ├── testplugin.apparmor │ ├── testplugin.desktop │ ├── testplugin.png │ ├── testplugin.pro │ └── tests │ ├── autopilot │ └── unit ├── testplugin.pro └── testplugin.pro.user
關鍵是把需要的 .so 複製到部署路徑:
TEMPLATE = lib
load(ubuntu-click)
filetypes = qml png svg js qmltheme jpg qmlproject desktop wav so
OTHER_FILES = filetypes
for(filetype, filetypes) {
OTHER_FILES += *.$$filetype
}
other_files.path = $${UBUNTU_CLICK_PLUGIN_PATH}
other_files.files = $$OTHER_FILES
INSTALLS += other_files
message(The project1 will be installed in $${UBUNTU_CLICK_PLUGIN_PATH})
接著在專案總 testplugin.pro 中加入子目錄:
SUBDIRS += testplugin \
backend/Testplugin \
libs
在 Testplugin.pro 中補上連結與搜尋路徑:
LIBS += -L$$PWD/../../libs/ -ltestStudent INCLUDEPATH += $$PWD/../../libs DEPENDPATH += $$PWD/../../libs
QML 端只要觸發 plugin 的方法:
Button {
anchors.centerIn: parent
text: "Test lib"
onClicked: {
myType.testlib();
}
}
編譯並部署到手機後:
從桌面用 adb 進到手機,切到:
$ adb shell /opt/click.ubuntu.com/testplugin.liu-xiao-guo/current/lib/arm-linux-gnueabihf/
可以看到所需的檔案已被成功安裝到該目錄:
點擊應用中的「Test lib」按鈕,SDK IDE 就會印出:
這行輸出正是來自:
Student student("johnddfsfdfdfds");
student.display();
完整專案原始碼:https://github.com/liu-xiao-guo/testplugin