How to enter a third-party app library in Ubuntu mobile Click Pack

Source: Internet
Author: User

Due to the security limitations of the Ubuntu mobile platform, we can only access our own space in the Ubuntu mobile app. If the file we need to access does not exist in the space where we use it, there is no way to access it. Our app only has access to the system-provided libraries. If the system does not have the required library, we can do this by the following methods:
    1. Put the required third-party source of the application into my application in my application and form plugin (usually C + + code) to be called by the application
    2. A third-party library is packaged into our application package in our application and called in my plugin (usually C + + code), which is then called by the application
We can choose the standard version of the Ubuntu SDK with a C + + plugin template to implement. For the second case, we also need to include the required files in our project and package them in our application. The second approach is useful for situations where the Ubuntu SDK does not exist in the standard library. For example, we can get the source code of the PDF Reader Library and compile it into our own library. Eventually we can pack these libraries together and install them on our phones.
In this article today, we describe the second approach in detail.

1) Create a minimal shared library
I have created a most basic shared library. Developers can download our code in the following ways:
$ git clone https://github.com/liu-xiao-guo/studentlib_shared

Our library is very simple:
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 {}void Student::d isplay () {cout << "A Student with Name" << this- >name << Endl;}

Next, we take ARMHF chroot as an example, and of course we can use the same method for i386. We compile our library through the ARMHF chroot that we have installed. First we enter into my chroot by the following way:
$ Click chroot-aarmhf-fubuntu-sdk-15.04 Run  

When we get into the chroot I bought, we can start compiling our application library. We go to the root of our project and enter the following commands
$ mkdir build$ CD build $ cmake. $ make

This will allow us to see the libraries we want in our build subdirectory.


(CLICK-UBUNTU-SDK-15.04-ARMHF) [Email protected]:~/qml/studentlib_shared/build$ lsCMakeCache.txt  cmakefiles  Makefile  cmake_ Install.cmake  


2) Create a basic application with plugin.
We chose " QML App with C + + plugin (qmake)Template to create one of the most basic testplugin applications. In this application, we will show how to use the shared library that we have created above.


Next, we modify our mytype.cpp and mytype.h files:
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 ();p rotected:    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 () {}

In the above code, we have used the display () method in our shared library. If we run our application at this time, we will find that our application is definitely wrong. This is because it cannot find the library it needs at all. Let's try to package our library into our project.
We first create a directory called Libs in our project and test our shared library into this directory. So the entire project directory is like:
[Email protected]:~/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├──t Estplugin│├──main.qml│├──testplugin.apparmor│├──testplugin.desktop│├──testplugin.png│├──testplugin.pro │└──tests│     ├──autopilot│     └──unit├──testplugin.pro└──testplugin.pro.user

In the above display, we can see in the Libs directory in addition to one we see the libteststudent.so and header file, there is a file called Libs.pro. Its content is as follows:
Libs.pro
TEMPLATE = Libload (ubuntu-click) filetypes = qml png svg js qmltheme jpg qmlproject desktop wav soother_files = FILETYPESFO R (filetype, filetypes) {  Other_files + = *.$ $filetype}other_files.path = $${ubuntu_click_plugin_path}other_ Files.files = $ $OTHER _filesinstalls + = Other_filesmessage (the Project1 would be is installed in $${ubuntu_click_plugin_path} )

In the above file, we can use the above method to copy the. So file we need into the directory we need.
Finally, we need to include our directory "Libs" in our "Testplugin.pro" file.
Subdirs + = Testplugin            backend/testplugin            Libs

In our "Testplugin.pro" we need to add the following sentences:
LIBS + =-l$ $PWD/.. /.. /libs/-lteststudentincludepath + + $ $PWD/.. /.. /libsdependpath + = $ $PWD/.. /.. /libs

In our QML file, here's how we call it:
Main.qml
        ...        button {            anchors.centerIn:parent            text: "Test Lib"            onclicked: {                mytype.testlib ();            }        }        ...

Finally, let's compile and deploy our app to our phone:


We can view the installation files in our phone. In our desktop, enter the following command:
$ adb shell

Go to the phone via the above command and enter the following directory:
/opt/click.ubuntu.com/testplugin.liu-xiao-guo/current/lib/arm-linux-gnueabihf/

We can see that the files we need have been successfully installed in this directory:



When we click on the button "Test Lib" in the app, we can see the output in our SDK IDE:


The output string above is what we use our library to output:
     Student Student ("Johnddfsfdfdfds");     Student.display ();

The source code of the whole project is Https://github.com/liu-xiao-guo/testplugin

How to enter a third-party app library in Ubuntu mobile Click Pack

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.