VS2019 C++ Android console program development

foreword

I have introduced the development of Android .so on VS before, and VS also provides an embedded project template, which is very simple. The only downside is that the developed .so has to be called with an APK for debugging, unlike our Developing PC applications is very different, and you cannot directly observe the program running results on the console.

Can I directly develop console applications on Android? The answer is yes. I have also commented on this to the VS team, hoping to supplement this project template. After all, Android has both C++ and Java applications, and C++ The .so and .a libraries are also available, but the console application is missing. But people speak lightly, and there is no progress at present, so I provide my own method for the development of Android C++ console applications.

Add another sentence: Console applications are only suitable for debugging purposes, not suitable for final product development. After debugging, .so or .a needs to be generated and integrated into the Android apk.

Prepare

Although the title is VS2019, it just shows my current VS version. What I actually need is the CMake project template of VS, which should be supported since VS2017.

In addition, since it is developing an Android console application, two things are needed:

1. NDK(https://developer.android.com/ndk/downloads)

2. ADB(https://developer.android.com/studio/releases/platform-tools)

Please download these two things yourself.

The version here is ndkVersion "21.0.6352462", and adb is used by Android Studio. The program location of adb in Android studio is: C:\Users\<your username>\AppData\Local\Android\Sdk \platform-tools


Here I assume that you have completed the download of these two tools. It should not need to be installed, just decompress it directly. My ndk and adb are as follows:

But in order for VS to know the location of these two things, we need to add them to the system environment variables:

1. Open the system environment variable:

2. Add my two paths above to Path:

Pay attention to the path, if you have the following three .exe in the corresponding path, it will be fine:

1. make.exe

2. clang.exe clang++.exe clang-cl.exe

3. adb.exe

 

Finally, by the way, VS2019 or 2017 needs to install the cmake extension, which was introduced in my previous article. If you are not clear about the Cmake process of VS, I suggest you read my previous article first.


The preparatory work is over here. By the way, I will summarize: Android C++ programming belongs to the Native language, and NDK (N for native) is required. This is the Android native development kit, which mainly includes two compilers: gcc and clang The latest Android does not support gcc, so it is recommended to use clang as the compiler. After the compiler is compiled, it needs to use the Unix makefile as the program generator. Under Windows, use the make.exe that comes with ndk to replace make. adb is used as a debugging tool for communication with Android devices, it can transfer files, and use the shell to start the corresponding console application to print out its console output to the PC.

start

1. Create a cmake project

2. First of all, the code part is the same as the Windows or Linux console, there is no difference:

3. CMakeLists also do not need to be changed:

4. The focus is on modifying the configuration:

a. Click Manage Configuration

b. Currently, VS only provides Linux and Windows configurations in this interface, and Android configurations need to be added by ourselves.

c. Modify the configuration name of Android-v8a-Debug

d. Clear the tool set, because here we are using an external tool set, which is not in the predefined VS:

"gcc-arm" in the figure below is changed to ""

ctrl+S save, then close the json file

e. Open the management configuration again, and check whether the toolset is empty

f. Fill in the cmake file location of the toolchain:

Fill in below:

g. Update Cmake cache

After success, it will also switch to our configuration name Android-v8a-Debug:

5. Generate all:

Successful console information:

6. Send to the Android device to run:

a. Find the corresponding console program (the file structure of Cmake is also detailed in my previous article):

b. In the current folder location, run the adb push command:

The full command is adb push AndroidTest /data/local/tmp

AndroidTest is the file location of the current PC, and /data/local/tmp is the target location of the Android terminal. Note that other folders may have permission issues, and /data/local/tmp is one of the good choices for debugging Android console programs.

c. Use the adb shell to run the application we pushed to the Android side:

The full command is adb shell "cd /data/local/tmp && chmod 777 AndroidTest && ./AndroidTest"

The string in "" behind the adb shell is the shell command on the Android side. && indicates that the three commands are executed in sequence: first switch the path to /data/local/tmp, and then change the permissions of the applications I pushed in the past. If permission denied appears, That should be related to this chmod 777. Finally, ./AndroiTest is to run our console application.


At this point, the entire process of VS generating the Android control application is shown. If you have the basis of my previous CMake project article, you should think it is very simple, just change the configuration file.

In addition, as for the debugging problem, I only found the RemoteLinux configuration under launch.vs.json, and there is no Android-related configuration, so I didn’t go any further. Because VS has another project that can realize real-device debugging of Android C++ Code, this is left for the next article.

Afterword

Finally, add three cmake parameters, some of which may be required.

-DANDROID_ABI=arm64-v8a -DANDROID_NATIVE_API_LEVEL=23  -DANDROID_TOOLCHAIN=clang

Among them, the first -DANDROID_ABI=arm64-v8a is very important. The program that generates 64-bit arm64-v8a or 32-bit armeabi-v7a depends on this macro.

The second -DANDROID_NATIVE_API_LEVEL=23 I haven't found a useful place yet

The third one -DANDROID_TOOLCHAIN=clang should not be necessary. You can choose between gcc and clang, but clang should be used now.

If it is a 32-bit application, change the above -DANDROID_ABI=arm64-v8a to -DANDROID_ABI=armeabi-v7a. Remember to save and update CmakeCache after the change.

There is also CmakeBuildType that can be selected here:

Through the combination of these two, you should be able to add Android-v7a-Release and other configurations.


It's terrible, I suddenly thought of writing this article at 11:50, and it was one o'clock after I finished writing. Let's go to rest early, and I have to go to the driving range tomorrow to practice my driver.


When closing the VS project, I suddenly remembered one more thing.

Make.exe is not used above. The above generator is Ninja. 

In fact, there is no problem with this, and many people use this as a generator now. But since we introduced make at the beginning, we will demonstrate how to use makefile as a generator at the end.

1. In the configuration file, expand Advanced Settings:

2. Select Unix Makefiles:

3. After modification, save and update CMake Cache. Run all builds again:

Now this directory is the makefile file.

You can also directly run the make process to generate in the current path:

Here is the same as under Linux, just run make directly after modifying the cpp file, it will automatically compile the modified cpp file and relink it to the program. 

As for whether to use the make command of the console or run all builds under VS, it depends on your personal preference.

Guess you like

Origin blog.csdn.net/luoyu510183/article/details/107969469