Skip to content

Make app for Android

WangBin edited this page Jun 19, 2014 · 6 revisions

Tested on linux and OSX

Requirements:

  • Qt (>= 5.2)
  • ffmpeg (>= 2.2)
  • QtCreator (3.1.0)
  • QtAV (1.3.4 or last git)

Here we're skipping the standard steps for installation of Linux, SDK, NDK and Ant. Don't forget to update SDK Tools, Platform-Tools, Build-Tools.

I placed all these in /opt: SDK - /opt/Android/sdk/ NDK - /opt/Android/ndk/ Ant - /opt/apache-ant

Qt:

Get the sources: Qt,(I build Qt 5.2.1) Note that Qt should be build for platform 19 unless it wouldn't be built. Subsequently, however, your custom applications can be built for any API version. Unpack qt-everywhere-opensource-src-5.2.1.tar.xz , go to the sources tree and launch the following script to build Qt itself for Android arm platform.

The script is:

#!/bin/sh

unset QTDIR

NDK_TOOLCHAIN_VERSION=4.8
PLATFORM=19

export JAVA_HOME=/usr/lib/jdk1.6.0_45
PATH=$JAVA_HOME/bin:$PATH

export ANDROID_SDK_ROOT=/opt/Android/sdk
export ANDROID_NDK_ROOT=/opt/Android/ndk
export ANDROID_API_VERSION=android-$PLATFORM
export ANDROID_PLATFORM=$PLATFORM
export ANDROID_NDK_PLATFORM=android-$PLATFORM


./configure -v \
  -prefix /opt/qt5/arm \
  -xplatform android-g++ \
  -android-ndk /opt/Android/ndk \
  -android-sdk /opt/Android/sdk \
  -android-ndk-host linux-x86 \
  -android-toolchain-version $NDK_TOOLCHAIN_VERSION \
  -no-warnings-are-errors \
  -confirm-license \
  -opensource \
  -skip qttranslations \
  -skip qtwebkit \
  -skip qtserialport \
  -skip qtwebkit-examples \
  -nomake tests \
  -nomake examples

make
make install

If all went ok, the '/opt/qt5/arm' directory will be created. There will be the Qt built for Android there.

ffmpeg:

Get the sources: ffmpeg source I used version 2.2.1 Unpack the archive, and run the following build script. Configuration switches can be corrected, for your system can differ from mine. For example, I have Linux-32bit, so the config file contains linux-x86. For 64-bit system you should specify linux-x86_64.

#!/bin/bash

NDK=/opt/Android/ndk
SYSROOT=$NDK/platforms/android-14/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86

function build_one
{
./configure.ffmpeg-Android2 \
   --prefix=$PREFIX \
   --enable-shared \
   --disable-static \
   --disable-doc \
   --disable-ffmpeg \
   --disable-ffplay \
   --disable-ffprobe \
   --disable-ffserver \
   --disable-doc \
   --disable-symver \
   --disable-encoders \
   --enable-avdevice \
   --enable-avresample \
   --enable-runtime-cpudetect \
   --enable-memalign-hack \
   --enable-hwaccels \
   --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
   --target-os=linux \
   --arch=arm \
   --enable-cross-compile \
   --sysroot=$SYSROOT \
   --extra-cflags="-Os -fpic $ADDI_CFLAGS" \
   --extra-ldflags="$ADDI_LDFLAGS" $ADDITIONAL_CONFIGURE_FLAG

  make clean
  make
  make install
}

CPU=arm
PREFIX=/opt/qt5/arm
ADDI_CFLAGS="-marm"

build_one

This script runs another external third-party script configure.ffmpeg-Android2. The issue is that Android does not support shared libraries with names like libblablabla.so.55, so that we name libraries this way: libblablabla-55.so

Here is the patch for configure:

--- ./configure.orig    2014-03-24 02:08:02.000000000 +0400
+++ ./configure.ffmpeg-Android2 2014-05-19 13:48:16.000000000 +0400
@@ -2516,10 +2516,19 @@
 SLIBSUF=".so"
 SLIBNAME='$(SLIBPREF)$(FULLNAME)$(SLIBSUF)'
 SLIBNAME_WITH_VERSION='$(SLIBNAME).$(LIBVERSION)'
-SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
+
+# ORIG
+#SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
+#LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
+#SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
+#SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'
+
+# NEW
+SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
 LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
-SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
-SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'
+SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
+SLIB_INSTALL_LINKS='$(SLIBNAME)'
+
 
 asflags_filter=echo
 cflags_filter=echo

After the build process the headers and libraries of ffmpeg for Android arm will be located in the same directory: '/opt/qt5/arm'


QtCreator

Download the binary archive: QtCreator I used version 3.1.0

Unpack it where you like, e.g. '/opt/qtcreator'

The QtCreator customization is quite minimal. Only paths to sdk, ndk, ant should be specified. And also path to qmake, e.g. /opt/qt5/arm/bin/qmake

There many articles with screenshots explaining this on the Internet.

QtAV

Now the main things!

Get QtAV:

git clone https://github.com/wang-bin/QtAV

Then launch QtCreator and open the project QtAV ( QtAV.pro). Add Android kit for armeabi-v7a and build the project with the button with a hammer The directory will appear like this: build-QtAV-Android_armeabi_v7a_GCC_4_8_Qt_5_2_1-Release

You would have different dependently on you compiler or if you have specified other name. So, we have two directories:

Run sdk_install.sh in build dir. This will copy QtAV libraries to Qt dir to make sure we can create apk correctly.

Now try create an application:

qtcreator -> new project -> App -> App Qt Quick Choose, for example Qt Quick 2.2 Check Android kit in the checkbox.

An empty project will be created.

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    visible: true
    width: 360
    height: 360

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
}

Change the above into the following:

import QtQuick 2.2
import QtQuick.Window 2.1
import QtAV 1.3

Window {
    id: window1
    visible: true
    width: 360
    height: 360

    Text {
        height: 16
        text: qsTr("Qt+QtAV+FFmpeg")
        anchors.right: parent.right
        anchors.rightMargin: 13
        anchors.top: parent.top
        anchors.topMargin: 8
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.horizontalCenterOffset: 68
    }

    VideoOutput {
                anchors.right: parent.right
                anchors.rightMargin: 13
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                anchors.top: parent.top
                anchors.bottomMargin: 15
                anchors.leftMargin: 14
                anchors.topMargin: 60
                source: player
    }

    AVPlayer {
                id: player
                source: "http://blablabla/video/test.mp4"
    }

    MouseArea {
                anchors.rightMargin: 0
                anchors.bottomMargin: 0
                anchors.leftMargin: 0
                anchors.topMargin: 0
                anchors.fill: parent
                onClicked: {
                    player.play();
                }
    }
}

Create AndroidManifest with QtCreator. The default settings are acceptable.

Add the following lines to the project file *.pro:

QT += av

Now we can launch it. There will appear the virtual device selection. We can choose any compatible device from the list, or create a new one.

After that there will be the APK-file in the build directory

build-XXXXXX-Android_armeabi_v7a_GCC_4_8_Qt_5_2_1-Release/android-build/bin/QtApp-debug.apk

Clone this wiki locally