Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

在 Ubuntu 手機的 click 包中打包第三方函式庫

由於 Ubuntu 手機平台的安全限制,應用程式只能存取自己的空間。若需要的檔案不在應用沙箱內,就完全讀不到;應用只能使用系統提供的函式庫,一旦系統沒有你要的庫,就得自己動手。實務上有兩條路:

  1. 把第三方原始碼放進專案,編譯成 plugin(通常是 C++ 程式碼)供應用呼叫。
  2. 把預先編好的第三方庫打包進應用包,再由 plugin 呼叫。

Ubuntu SDK 提供了帶 C++ plugin 的標準模板可以套用。第二種做法對那些不在 SDK 標準庫清單中的元件特別有用——例如拿到 PDF reader 的庫原始碼自行編譯,再把 .so 一起打包安裝到手機。本文就針對第二種方式詳細說明。

1. 建立一個最小的 shared 庫

先取得範例程式碼:

$ git clone https://github.com/liu-xiao-guo/studentlib_shared

Student.h

#include <string>

class Student {
private:
    std::string name;
public:
    Student(std::string);
    virtual void display();
};

student.cpp

#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。

在 Ubuntu 手機的 click 包中打包第三方函式庫

在 Ubuntu 手機的 click 包中打包第三方函式庫

(click-ubuntu-sdk-15.04-armhf)liuxg@liuxg:~/qml/studentlib_shared/build$ ls
CMakeCache.txt  CMakeFiles  Makefile  cmake_install.cmake  libtestStudent.so

2. 建立帶 plugin 的基礎應用

選擇「QML App with C++ plugin (qmake)」模板建立 testplugin 專案。

在 Ubuntu 手機的 click 包中打包第三方函式庫

mytype.h

#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

mytype.cpp

#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

libs.pro

關鍵是把需要的 .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 的方法:

Main.qml

Button {
    anchors.centerIn: parent
    text: "Test lib"
    onClicked: {
        myType.testlib();
    }
}

編譯並部署到手機後:

在 Ubuntu 手機的 click 包中打包第三方函式庫

從桌面用 adb 進到手機,切到:

$ adb shell
/opt/click.ubuntu.com/testplugin.liu-xiao-guo/current/lib/arm-linux-gnueabihf/

可以看到所需的檔案已被成功安裝到該目錄:

在 Ubuntu 手機的 click 包中打包第三方函式庫

點擊應用中的「Test lib」按鈕,SDK IDE 就會印出:

在 Ubuntu 手機的 click 包中打包第三方函式庫

這行輸出正是來自:

Student student("johnddfsfdfdfds");
student.display();

完整專案原始碼:https://github.com/liu-xiao-guo/testplugin

Copyright © Linux教程網 All Rights Reserved