Qt on Android episode 6 (translation)

Source: Internet
Author: User

Original link: http://www.kdab.com/qt-android-episode-6/,Aprile by BogDan Vatra

Translator Foruok, reproduced please retain the source.

In Qt on Android Episode 5 we learned the basics of using QT in the way that we use JNI on Android. This time, I want to focus on tools that can help us improve productivity when we use JNI to extend QT on Android applications.

Using the external IDE to manage Java files

Unfortunately, Qt creator has very limited support for Java, so to be more efficient, we need an external IDE to make it easier to extend and debug the Java part of our application.

Android has two powerful Ides:

    • Android Studio
    • Eclipse (not recently recommended)

This article will focus only on Android Studio.

But before we discuss the tools, let's examine the Android-related files in our project.

As I said in the first article, the Qt on Android app has two major parts:

    • The native part, which is your C/C++/QT code, is compiled into so file
    • Android-related sections:
      • Android manifest file
      • Android Build System files
      • Two Java classes to load your app's dependent libraries, your app so files, and launch apps
      • Ministro services. aidl files and other resource files, such as assets, strings, images, etc.

In order to build your Android app package (learned in episode 3), all Android-related files are required. But where are these files? Why is it that when you click on the "Create Templates" button (reference) only a few files are copied to your source file directory?

Well, Java files, the. aidl files for the Ministro service, and some resource files are hidden in your QT SDK. We don't want to copy them to your source file directory tree Because of these two reasons:

    • When versions change, we may change them and break compatibility. If they are part of your project, then they can't be updated and your app will run abnormally (for example, the QT 5.4 Java file may be incompatible with QT 5.5).
    • We don't want you to change them because the code in these files is quite "advanced", difficult to interpret and understand, and if you change them, it can disrupt your program's behavior. If you don't change them, all your apps can share the same files, so there's no need to copy them.

Before QT 5.4, it is quite complicated to extend the Java portion of your application, mainly because you cannot gracefully use the features of an external IDE like eclipse, such as syntax highlighting, code completion, code refactoring, and so on. (Translator Note: I was on both sides of the copy ...) For these conveniences, you must manually copy all Java files from your QT SDK installation directory to your app's source directory, but as I said earlier, we don't want you to do that, the only way is to copy them locally and not add them to your SCM (Translator Note: Source control server) ...

But starting with Qt 5.4, this changed when I added Gradle support to QT 5.4 and QT Creator 3.3. (The translator's note: Nani, and this benefit!) Gradle is the newly recommended Android build system that Android Studio uses. With Gradle, we can provide all the convenience you want from a 21st century IDE, while continuing to hide all of these files. So, if you plan to extend the Java portion of your application, it is highly recommended to copy the Gradle file to the Android directory , as in:

In addition to the androidmanifest.xml and res/values/libs.xml files, the wizard also copies the following files:

    • Project files required by Build.gradle-android Studio
    • Gradlew[.bat]-Build scripts to build the Java section and create the final apk.
    • gradle/wrapper/*-Build Script encapsulation file

Build.gradle is very important and it can make it very convenient for you to add dependencies to your project.

For example, to add a playservices to your application, you only need to do this:

dependencies {  ...  compile ‘com.google.android.gms:play-services:7.0.0‘}

To learn more about Build.gradle, see this page.

How QT apps use Android Studio

Android Studio is only used to do these things:

    • Open Gradle Project
    • Create and edit Java files
    • Debugging the Java section

Android Studio can't be used to run your QT app, you still have to use QT creator to run your app!

Let's take a look at how to import the Java part of your QT application and how to debug it.

The core step is very simple: you only have to open the existing build.gradle project file. Reference:

warning : Users of QT 5.4.0 should update gradle files with QT 5.4.1 and later versions (click the "Create Templates" button again and overwrite only the Gradle file). This step is necessary because QT 5.4.0 uses an older Gradle plugin, and after we release 5.4.0, Google discards the old version of the Gradle plugin again, and Android Studio refuses to import the QT 5.4.0 project.

The last thing we need to learn today is how to debug the Java section with Android Studio. Follow these steps:

    • Set breakpoints
    • Additional Debugger (Run->attach debugger to Android process)
    • Running applications with QT Creator
    • Wait for the app to start (you should be able to attach the Debugger dialog box to see it)
    • Select an app
    • Click the OK button

At the end of the final step, your app will be stopped by Android studio when it executes to a breakpoint. When we want to debug the place very forward (the translator note: Very close to activity startup) will encounter some problems. To solve this problem, we can use the Debug.waitfordebugger () function.

Here are the onCreate functions we have customized:

@Overridepublic void onCreate(Bundle savedInstanceState){    Debug.waitForDebugger();    // ....    super.onCreate(savedInstanceState);}

Now the app waits to know that a debugger is attached to it before it runs.

Now that we have learned the basics of JNI and how to use external tools to create a sweet life (Rose Life), in the next article we will discuss the QT on Android application structure: How to extend the Java part of the QT application, Use a real-life Android example to demonstrate how to correctly invoke the Android UI safely from a QT thread (and the reverse side of it). (Translator note: The real welfare, this piece is the most annoying.) )

I translate QT on Android episode series articles:

    • Qt on Android Episode 1
    • Qt on Android Episode 2
    • Qt on Android Episode 3
    • Qt on Android Episode 4
    • Qt on Android Episode 5

I opened the subscription number " program Horizon ", attention can be the first time to see my original article and I recommend the wonderful article:

Qt on Android episode 6 (translation)

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.