How to test Android native Java code in Qt project?
-
I got a project with some native java code in
./android/src/<package name>/XXX.java
and imported it in./CMakeLists.txt
. Now I can run it in my android device.
Now I wonder how to test the native code. It seems to be difficult to open the./android
directory because there are some qt defined variables in the top-levelbuild.gradle
. How do you do that? Are there some open source projects to refenence? Thank you! -
You can test native Java code in a Qt Android project by creating a separate Android Studio project for unit testing.
Here’s a general approach:
Copy the ./android/src/<package> Java files into a standalone Android Studio project.
Mock any Qt or JNI dependencies if needed.
Use JUnit or Robolectric for testing.
Once verified, integrate back into the Qt project.
This way, you avoid issues with Qt’s custom Gradle setup and can test Java logic independently.
For reference, check out Qt Android Extras or Qt's QtAndroid class to understand how native bindings are typically handled.
-
You can test native Java code in a Qt Android project by creating a separate Android Studio project for unit testing.
Here’s a general approach:
Copy the ./android/src/<package> Java files into a standalone Android Studio project.
Mock any Qt or JNI dependencies if needed.
Use JUnit or Robolectric for testing.
Once verified, integrate back into the Qt project.
This way, you avoid issues with Qt’s custom Gradle setup and can test Java logic independently.
For reference, check out Qt Android Extras or Qt's QtAndroid class to understand how native bindings are typically handled.
@danish777 Thank you! But I noticed that the same code appear twice in two git repository. (or may be the sub Android Studio project is a temporary one so does not need a repository?) And additionally, the gradle files is not identity so
reproducibility might be a problem. Is this the commonly used approch in Qt for Android projects?