1. Description:
QT version: 5.15.2
Computer system: Win10 / Win11
JDK version: jdk
The first Android applet
2. Specific steps:
There are many descriptions about the installation of QT 5.15.2 on the Internet, so it will not be recorded here. Note that you should choose the Android suite when installing .
Before downloading JDK and SDK, it is recommended to create a new folder in your own drive letter, named in English, without spaces or messy characters, it is easy to make mistakes. When installing the SDK later, it will be installed in this newly created folder by default. As for the installation of JDK, it is recommended to use the default installation path.
2.1 Step 1: Download JDK
Click on the official website JAVA official website download address to enter, there are many versions of JAVA at present, the latest is Java19, but we don’t need a too high version, the Qt version and the JDK version need to be strictly matched, otherwise the environment cannot be built . We click on the java archive on the home page , and operate as shown in the figure below:
After the page jumps, a new page will open, slide the mouse down, and you can see various old versions of Java, here you can choose java SE 11 to download.
2.2 Step 2: Download the Android SDK
After entering the Android official website SDK download official website page, slide the mouse all the way down, find SDK Tools , and download the corresponding version, as shown in the figure below:
Note: There are also many tutorials on the Internet that use Android Studio to install. I personally don’t think this is the case. Very friendly, it is simpler to use the compressed package directly.
2.3 Step 3: Install JDK
(Here is the installation of JDK8 as an example, because my JDK11 is finally installed, and I dare not change it easily, so I will take JDK8 as an example to record it.) Select the JDK
installation file downloaded above, right-click to run as an administrator, Note that it is recommended to use the default installation path , click Next , as shown in the figure below:
After entering the next step, select the optional features to be installed, here is the default, do not need to change , just click Next , the installation path is also recommended not to be changed, as follows As shown in the picture:
After waiting for the installation, the interface for installing JRE will pop up (the installation of JDK8 will appear, and the installation of JDK11 will not appear), here is also the default installation path , just go to the next step, as shown in the figure below
: In the pop-up window below, click Close to complete the installation.
The JDK is installed on the C drive by default . This JDK file occupies a small amount of memory. Don’t worry about your own C drive memory problem. The subsequent SDK installation takes up a lot of memory.
2.4 Step 4: Install the SDK
Find the SDK compressed package downloaded above, after decompression is complete, find SDK Manager.exe in the folder , and run it as an administrator.
After waiting for the progress bar at the bottom of the pop-up interface to load, first cancel all selected items , then select only the first item , and click install in the small right corner . The operation steps are as shown in the figure below:
After clicking install , a pop-up window appears, select Accept License , Then click install , wait for the installation to complete, and close the page. Then enter the following path page: After the previous step, there will generally be a sdkmanager.bat
file in this folder , as shown in the figure below: If there is no file specified by the red arrow in the folder above, you can create a new one yourself . text, and then paste the following code into it, right-click the file after saving and rename it as sdkmanager .
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem sdkmanager startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%..
@rem Add default JVM options here. You can also use JAVA_OPTS and SDKMANAGER_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Dcom.android.sdklib.toolsdir=%~dp0\.."
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\lib\sdklib-25.3.0-dev.jar;%APP_HOME%\lib\layoutlib-api-25.3.0-dev.jar;%APP_HOME%\lib\dvlib-25.3.0-dev.jar;%APP_HOME%\lib\repository-25.3.0-dev.jar;%APP_HOME%\lib\gson-2.2.4.jar;%APP_HOME%\lib\commons-compress-1.8.1.jar;%APP_HOME%\lib\httpclient-4.1.1.jar;%APP_HOME%\lib\httpmime-4.1.jar;%APP_HOME%\lib\common-25.3.0-dev.jar;%APP_HOME%\lib\kxml2-2.3.0.jar;%APP_HOME%\lib\annotations-25.3.0-dev.jar;%APP_HOME%\lib\annotations-12.0.jar;%APP_HOME%\lib\jimfs-1.1.jar;%APP_HOME%\lib\httpcore-4.1.jar;%APP_HOME%\lib\commons-logging-1.1.1.jar;%APP_HOME%\lib\commons-codec-1.4.jar;%APP_HOME%\lib\guava-18.0.jar
@rem Execute sdkmanager
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %SDKMANAGER_OPTS% -classpath "%CLASSPATH%" com.android.sdklib.tool.SdkManagerCli %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable SDKMANAGER_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%SDKMANAGER_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
Then, open the CMD command terminal in this folder . You can hold down shift first , and then right-click to select the PowerShell window here . After opening, enter the command start cmd to open the command terminal, as shown in the figure below:
Open the CMD command terminal After that, enter the following commands in order to execute:
sdkmanager "build-tools;30.0.2" --sdk_root=<ANDROID_SDK_ROOT>
sdkmanager "ndk;21.3.6528147" --sdk_root=<ANDROID_SDK_ROOT>
sdkmanager "platform-tools" --sdk_root=<ANDROID_SDK_ROOT>
sdkmanager "platforms;android-30" --sdk_root=<ANDROID_SDK_ROOT>
sdkmanager "cmdline-tools;latest" --sdk_root=<ANDROID_SDK_ROOT>
sdkmanager "extras;google;usb_driver" --sdk_root=<ANDROID_SDK_ROOT>
Note: <ANDROID_SDK_ROOT> in the above code is the path where the SDK is installed, and should be replaced according to the respective installation paths. For example, my installation path is: D:\android222\android-sdk-windows, then each of the above commands should be replaced, namely: sdkmanager "build-tools;30.0.2" --sdk_root="D:\ android222\android-sdk-windows".
When executing the first command, it will ask whether to accept the agreement , just enter y on the keyboard . It will take a little longer to execute the following commands, please wait patiently. When done appears , execute the next command until the execution ends. After the above operations are completed, the installation of JDK and SDK
is completed.
3. Configure in QtCreator
When configuring, first open the environment configuration window in the menu bar ***Edit -> Preference…***, as shown in the following figure:
3.1 Create an Android project
Creating a project is the same as creating a desktop project normally, but it is best to use the QtQuick framework to build an android project. It is still very friendly to port the interface made by using the QML language to the Android system. Choose to use the Android kit in the Kit Selection step , as shown in the figure below: After the creation is complete, change the root node Window to ApplicationWindow in the main.qml file . This is suitable for Android development, and you need to import the file import QtQuick.Controls 2.15 . The main.qml in this case writes a simple code to click the button to change the background color . code show as below:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
width: 900
height: 1600
visible: true
Rectangle{
id:bg
anchors.fill: parent
color: Qt.rgba(0.0,0.0,0.0,0.5)
}
Text {
id: txt
anchors.centerIn: parent
text: qsTr("First Android App")
font.pixelSize: 30
}
Button{
id:btn
width: 200
height: 50
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: txt.top
anchors.bottomMargin: 5
text:"改变颜色"
font.pixelSize: 20
onClicked: {
bg.color = Qt.rgba(1.0,0.0,0.0,0.5)
}
}
}
The effect is as shown at the beginning of the article
3.2 Supplement: How to run the android project
Do not use the three buttons in the lower left corner of QtCreator here. If you use it, you must configure virtual devices for Qt. Here you can use an Android game emulator. After installing it on the computer, drag the ***.apk file generated by the build to the emulator to install the generated Android program and run it to view the effect.
Use the shortcut key Ctrl + B*** when building the project , or build the project in the menu bar Build --> Build Project
, as shown in the figure below: After the build is completed, find the directory path below in the compilation path of your project, I It is: D:\LearnQt\SelfAndroid\build-android2-Qt_5_15_2_Clang_Multi_Abi-Debug\android-build\build\outputs\apk\debug , there is a generated .apk file inside, as shown in
the figure: the . Drag and drop the apk file to the Android emulator to run it to view the effect. The Android emulator recommends the late night emulator .