How to build a SDL2 application for Android using command line

Hi,

My steps:

  • I installed Android SDK, NDK, JDK, and Gradle
  • I installed these packages using SDK Manager:
    image
    image
  • I created ANDROID_HOME, ANDROID_NDK, and JAVA_HOME variables:
    image
  • I added the Gradle path to the Path variable
  • I downloaded this folder (from release 2.28.5): https://github.com/libsdl-org/SDL/tree/main/android-project
  • I opened the project folder in CMD and typed this command: "./gradlew" build

But I have these errors. Please wrote what I can try. I don’t want to use Android Studio. It is too slow on my laptop. I want to use CMD and Sublime Text 4.

E:\_Projects\SDL\android\android-project>"./gradlew" build
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :app:configureNdkBuildDebug[arm64-v8a] FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:configureNdkBuildDebug[arm64-v8a]'.
> [CXX1429] error when building with ndkBuild using E:\_Projects\SDL\android\android-project\app\jni\Android.mk: C++ build system [configure] failed while executing:
      @echo off
      "E:\\AppData\\Android\\SDK\\ndk\\25.1.8937393\\ndk-build.cmd" ^
        "NDK_PROJECT_PATH=null" ^
        "APP_BUILD_SCRIPT=E:\\_Projects\\SDL\\android\\android-project\\app\\jni\\Android.mk" ^
        "NDK_APPLICATION_MK=E:\\_Projects\\SDL\\android\\android-project\\app\\jni\\Application.mk" ^
        "APP_ABI=arm64-v8a" ^
        "NDK_ALL_ABIS=arm64-v8a" ^
        "NDK_DEBUG=1" ^
        "NDK_OUT=E:\\_Projects\\SDL\\android\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/obj" ^
        "NDK_LIBS_OUT=E:\\_Projects\\SDL\\android\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/lib" ^
        "APP_PLATFORM=android-19" ^
        "APP_SHORT_COMMANDS=false" ^
        "LOCAL_SHORT_COMMANDS=false" ^
        -B ^
        -n
    from E:\_Projects\SDL\android\android-project\app
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:586: Android NDK: Module main depends on undefined modules: SDL2
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:599: *** Android NDK: Note that old versions of ndk-build silently ignored this error case. If your project worked on those versions, the missing libraries were not needed and you can remove those dependencies from the module to fix your build. Alternatively, set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies.    .  Stop.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 12s
15 actionable tasks: 2 executed, 13 up-to-date

android-project is a project template. To create a buildable project you should use androidbuild.sh (you have to use bash or port it to batch file) script or perform the same actions manually. See SDL2/Android - SDL Wiki
You can also prebuild SDL as a library to skip source-copy-build step for future projects.

1 Like

Thank! I see that the manual has a link to this step-by-step manual: https://github.com/libsdl-org/SDL/blob/main/docs/README-android.md I opened the SDL2 source code folder and I found the androidbuild.sh inside the build-scripts folder. I think I should to go to the the build-scripts folder and run the androidbuild.sh script from it. The manual says:

for example if you are in the build-scripts directory and want to create the testgles.c test, you’ll run:

./androidbuild.sh org.libsdl.testgles ../test/testgles.c
  • I am inside of the the build-scripts folder:
    image
  • I executed this command: "./androidbuild.sh" org.libsdl.testgles ../test/testgles.c
  • I don’t see any output messages
  • I go to the test folder and I see a lot of files there:
  • I opened the testgles.c file and I see a code from OpenGL 1.1. It is a desktop version of OpenGL that will not work on Android:
        glViewport(0, 0, state->window_w, state->window_h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        glShadeModel(GL_SMOOTH);

It is strange for me. Or does OpenGL 1.1 work on Android?

I don’t understand what is the next step in this manual: https://github.com/libsdl-org/SDL/blob/main/docs/README-android.md But I see the next step in this one: SDL2/Android - SDL Wiki

  • Follow the instructions to install on your device:
cd /usr/src/SDL2/build/org.libsdl.testgles/
ant debug install       # SDL <= 2.0.7
./gradlew installDebug  # SDL >= 2.0.8

But there is not such folder SDL2/build/org.libsdl.testgles:
image

image

image

image

cd /usr/src/SDL2/build/org.libsdl.testgles/
ant debug install       # SDL <= 2.0.7
./gradlew installDebug  # SDL >= 2.0.8

In my case I use SDL >= 2.0.8. So I should use ./gradlew installDebug. I see that the SDL-release-2.28.5 has only one gradlew file inside of SDL-release-2.28.5\android-project:
image

  • I try to go to the SDL-release-2.28.5\android-project folder and execute of this command "./gradlew" installDebug But I again get the same error as in the first message:
E:\_Projects\SDL\android\SDL-release-2.28.5\android-project>"./gradlew" installDebug
> Task :app:configureNdkBuildDebug[arm64-v8a] FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:configureNdkBuildDebug[arm64-v8a]'.
> [CXX1429] error when building with ndkBuild using E:\_Projects\SDL\android\SDL-release-2.28.5\android-project\app\jni\Android.mk: C++ build system [configure] failed while executing:
      @echo off
      "E:\\AppData\\Android\\SDK\\ndk\\25.1.8937393\\ndk-build.cmd" ^
        "NDK_PROJECT_PATH=null" ^
        "APP_BUILD_SCRIPT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\jni\\Android.mk" ^
        "NDK_APPLICATION_MK=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\jni\\Application.mk" ^
        "APP_ABI=arm64-v8a" ^
        "NDK_ALL_ABIS=arm64-v8a" ^
        "NDK_DEBUG=1" ^
        "NDK_OUT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/obj" ^
        "NDK_LIBS_OUT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/lib" ^
        "APP_PLATFORM=android-19" ^
        "APP_SHORT_COMMANDS=false" ^
        "LOCAL_SHORT_COMMANDS=false" ^
        -B ^
        -n
    from E:\_Projects\SDL\android\SDL-release-2.28.5\android-project\app
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:586: Android NDK: Module main depends on undefined modules: SDL2
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:599: *** Android NDK: Note that old versions of ndk-build silently ignored this error case. If your project worked on those versions, the missing libraries were not needed and you can remove those dependencies from the module to fix your build. Alternatively, set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies.    .  Stop.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 11s
14 actionable tasks: 14 executed

Are you sure that scripts runs at all?

These are commands to install the application on the device/emulator.

You should execute the script to create a project or do it manually by copying SDL sources to the jni folder.

Yes, because the test directory was generated. And I did not see any error messages.

image

I just follow the manual. It says:

  1. Go to the build-scripts director. I did it.
  2. Compile a sample app (calls ndk-build) with the command: ./androidbuild.sh org.libsdl.testgles ../test/testgles.c. I did it. The test directory was generated.
  3. Go to the SDL2/build/org.libsdl.testgles and execute ./gradlew installDebug. But I didn’t find this directory. There is no the build directory in the root of the SDL folder.

What script?

1 Like

The test directory was generated here:

image

Where to create the jni folder? Inside the SDL-release-2.28.5\android-project?

  • I created the empty jni folder inside SDL-release-2.28.5\android-project:

image

  • I copied the content of the SDL-release-2.28.5\src folder to the SDL-release-2.28.5\android-project\jni:

image

  • I go to the SDL-release-2.28.5\android-project folder and execute the "./gradlew" build command:
E:\_Projects\SDL\android\SDL-release-2.28.5\android-project>"./gradlew" build
> Task :app:configureNdkBuildDebug[arm64-v8a] FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:configureNdkBuildDebug[arm64-v8a]'.
> [CXX1429] error when building with ndkBuild using E:\_Projects\SDL\android\SDL-release-2.28.5\android-project\app\jni\Android.mk: C++ build system [configure] failed while executing:
      @echo off
      "E:\\AppData\\Android\\SDK\\ndk\\25.1.8937393\\ndk-build.cmd" ^
        "NDK_PROJECT_PATH=null" ^
        "APP_BUILD_SCRIPT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\jni\\Android.mk" ^
        "NDK_APPLICATION_MK=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\jni\\Application.mk" ^
        "APP_ABI=arm64-v8a" ^
        "NDK_ALL_ABIS=arm64-v8a" ^
        "NDK_DEBUG=1" ^
        "NDK_OUT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/obj" ^
        "NDK_LIBS_OUT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/lib" ^
        "APP_PLATFORM=android-19" ^
        "APP_SHORT_COMMANDS=false" ^
        "LOCAL_SHORT_COMMANDS=false" ^
        -B ^
        -n
    from E:\_Projects\SDL\android\SDL-release-2.28.5\android-project\app
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:586: Android NDK: Module main depends on undefined modules: SDL2
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:599: *** Android NDK: Note that old versions of ndk-build silently ignored this error case. If your project worked on those versions, the missing libraries were not needed and you can remove those dependencies from the module to fix your build. Alternatively, set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies.    .  Stop.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 46s
15 actionable tasks: 2 executed, 13 up-to-date

I tried to execute this script again after coping source files to jni: "./androidbuild.sh" org.libsdl.testgles ../test/testgles.c

I ran this script in the android-project folder: "./gradlew" build. I have the same errors:

E:\_Projects\SDL\android\SDL-release-2.28.5\android-project>"./gradlew" build

> Task :app:compileDebugJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

> Task :app:configureNdkBuildDebug[arm64-v8a] FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:configureNdkBuildDebug[arm64-v8a]'.
> [CXX1429] error when building with ndkBuild using E:\_Projects\SDL\android\SDL-release-2.28.5\android-project\app\jni\Android.mk: C++ build system [configure] failed while executing:
      @echo off
      "E:\\AppData\\Android\\SDK\\ndk\\25.1.8937393\\ndk-build.cmd" ^
        "NDK_PROJECT_PATH=null" ^
        "APP_BUILD_SCRIPT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\jni\\Android.mk" ^
        "NDK_APPLICATION_MK=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\jni\\Application.mk" ^
        "APP_ABI=arm64-v8a" ^
        "NDK_ALL_ABIS=arm64-v8a" ^
        "NDK_DEBUG=1" ^
        "NDK_OUT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/obj" ^
        "NDK_LIBS_OUT=E:\\_Projects\\SDL\\android\\SDL-release-2.28.5\\android-project\\app\\build\\intermediates\\cxx\\Debug\\29445a3u/lib" ^
        "APP_PLATFORM=android-19" ^
        "APP_SHORT_COMMANDS=false" ^
        "LOCAL_SHORT_COMMANDS=false" ^
        -B ^
        -n
    from E:\_Projects\SDL\android\SDL-release-2.28.5\android-project\app
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:586: Android NDK: Module main depends on undefined modules: SDL2
  E:/AppData/Android/SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:599: *** Android NDK: Note that old versions of ndk-build silently ignored this error case. If your project worked on those versions, the missing libraries were not needed and you can remove those dependencies from the module to fix your build. Alternatively, set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies.    .  Stop.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
16 actionable tasks: 2 executed, 14 up-to-date

Yes, OpenGL 1.1 works on Android. I rely on it in my app.

1 Like

I solved the problem. @Captain helped me on SDL-discord server. He recommended this manual and helped when I had problems:

Note. This manual works for Android. Except “Ninja” app that must be installed (and added to Path, I used one from Qt: C:\Qt\Tools\Ninja ) and manually changing of CMake version in the build.gradle script.

I use scrcpy to show an Android screen on Laptop to make screenshots. I connected a phone using USB.

image

1 Like