Minimal android ndk project

Hi. I create simple SDL2 android project.

  1. Copy dir “android-project” and input dir SDL to /jni
changed
AndroidManifest.xml

<uses-sdk android:minSdkVersion=“10” android:targetSdkVersion=“12” />
to
<uses-sdk android:minSdkVersion=“10” android:targetSdkVersion=“17” />

jni/Application.mk

APP_ABI := armeabi armeabi-v7a x86
to
APP_ABI := armeabi-v7a

jni/src/Android.mk

LOCAL_SRC_FILES := YourSourceHere.c
to
LOCAL_SRC_FILES :=main.c

  1. Create /jni/src/main.c
main.c
#include <SDL.h>
#include <SDL_opengles2.h>

int SDL_main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("Title", 0, 0, 320, 240, SDL_WINDOW_OPENGL);
    SDL_GLContext glContext = SDL_GL_CreateContext(window);

    glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
    while ( SDL_TRUE )
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
        }
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
  }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
  1. Create apk and run
    On string:
    SDL_Window *window = SDL_CreateWindow(“Title”, 0, 0, 320, 240, SDL_WINDOW_OPENGL);
    Application close with error:
    E/libEGL (17203): validate_display:188 error 3008 (EGL_BAD_DISPLAY)

Do you have this line in AndroidManifest.xml?

<!-- OpenGL ES 2.0 -->
<uses-feature android:glEsVersion="0x00020000"  />

Yes AndroidManifest.xml as in the original.

Summary
<?xml version="1.0" encoding="utf-8"?>
<!-- Replace org.libsdl.app with the identifier of your game below, e.g.
     com.gamemaker.game
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.libsdl.app"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="auto">

    <!-- Android 2.3.3 -->
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" />

    <!-- OpenGL ES 2.0 -->
    <uses-feature android:glEsVersion="0x00020000" />

    <!-- Allow writing to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- Allow access to the vibrator -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- if you want to capture audio, uncomment this. -->
    <!-- <uses-permission android:name="android.permission.RECORD_AUDIO" /> -->

    <!-- Create a Java class extending SDLActivity and place it in a
         directory under src matching the package, e.g.
         	src/com/gamemaker/game/MyGame.java

         then replace "SDLActivity" with the name of your class (e.g. "MyGame")
         in the XML below.

         An example Java class can be found in README-android.md
    -->
    <application android:label="@string/app_name"
                 android:icon="@drawable/ic_launcher"
                 android:allowBackup="true"
                 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                 android:hardwareAccelerated="true" >
		<meta-data android:name="android.app.lib_name" android:value="SDL2" />
        <activity android:name="SDLActivity"
                  android:label="@string/app_name"
                  android:configChanges="keyboardHidden|orientation|screenSize"
                  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- Drop file event -->
            <!--
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
            -->
        </activity>
    </application>

</manifest> 

I remember getting a similar error and the app still worked fine. Maybe it’s something else?

try something like this to get more info:

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL init failed: %s\n", SDL_GetError());
    SDL_ClearError();
    return -1;
}

SDL_Window *window = SDL_CreateWindow("Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_OPENGL);
if (!window) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Creating window failed: %s\n", SDL_GetError());
    SDL_ClearError();
    return -1;
}

SDL_GLContext glContext = SDL_GL_CreateContext(window);
if (!glContext) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Creating GL context failed: '%s'.\n", SDL_GetError());
    SDL_ClearError();
    return -1;
}

SDL_Log("SDL Initialized.\n");
main.c
#include <SDL.h>
#include <SDL_opengles2.h>

#include <android/log.h>

int SDL_main(int argc, char **argv)
{
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); 
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); 
	__android_log_print(ANDROID_LOG_INFO, "TAG" , "GOOD_1");
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
		__android_log_print(ANDROID_LOG_INFO, "TAG" , "BAD_1");
        __android_log_print(ANDROID_LOG_VERBOSE, "TEST_VERBOSE", "SDL_Init: %s\n", SDL_GetError());
        return 1;
    }
	__android_log_print(ANDROID_LOG_INFO, "TAG" , "GOOD_2");
    SDL_Window *window = SDL_CreateWindow("Title", 0, 0, 320, 240,  SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
	if (window == 0){
		__android_log_print(ANDROID_LOG_INFO, "TAG" , "BAD_2");
        __android_log_print(ANDROID_LOG_ERROR, "TEST_ERROR_1", "SDL_CreateWindow: %s\n", SDL_GetError());
        return 1;
    }
	__android_log_print(ANDROID_LOG_INFO, "TAG" , "GOOD_3");
    SDL_GLContext glContext = SDL_GL_CreateContext(window);
	if (glContext == NULL){
		__android_log_print(ANDROID_LOG_INFO, "TAG" , "BAD_3");
        __android_log_print(ANDROID_LOG_ERROR, "TEST_ERROR_2", "SDL_CreateRenderer: %s\n", SDL_GetError());
        return 1;
    }
	__android_log_print(ANDROID_LOG_INFO, "TAG" , "GOOD_4");
    glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
	while ( SDL_TRUE )
	{
        SDL_Event event;
		while (SDL_PollEvent(&event))
		{
		}
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
	}

	SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
logcat

I/TAG (18240): GOOD_1

I/InputDispatcher( 247): setInputWindows

I/InputDispatcher( 247): setInputWindows

I/InputDispatcher( 247): setInputWindows

V/SDLControllerManager(18240): Input device mt6575-kpd is a dpad.

I/InputDispatcher( 247): setInputWindows

I/TAG (18240): GOOD_2

E/libEGL (18240): validate_display:188 error 3008 (EGL_BAD_DISPLAY)

W/dalvikvm(18240): Invalid indirect reference 0x414ef830 in decodeIndirectRef

I/InputDispatcher( 247): setInputWindows

E/dalvikvm(18240): VM aborting

F/libc (18240): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)

Which device/emulator are you using?

Device:
B3000
Android 4.0.4

Is there a problem with device/emulator? Does this code 100% work?

I tried your code on a api level 25 emulator and it worked. Could still be something else like manifest/build system or your device. Try with an emulator

Thank you very much. I will try.

I tried highscreen alpha rage and emulator api-17. Same: close application.
Problem manifest or build (maybe build-tools version or jre version or anything).
I build without gradle or ant. Only sdk and ndk.

Compile.bat
@rem COMPILE NDK
call c:/Programs/android-sdk/ndk-bundle/ndk-build.cmd NDK_LIBS_OUT=lib

@rem SET Dirs
@set JAVA_HOME=c:/"Program Files"/Java/jdk1.7.0_80
@set ANDROID_HOME=c:/Programs/android-sdk
@set DEV_HOME=%CD%

@set AAPT_PATH=%ANDROID_HOME%/build-tools/27.0.1/aapt.exe
@set DX_PATH=%ANDROID_HOME%/build-tools/27.0.1/dx.bat
@set ANDROID_JAR=%ANDROID_HOME%/platforms/android-17/android.jar

@set PACKAGE_PATH=org/libsdl/app
@set PACKAGE=org.libsdl.app
@set MAIN_CLASS=SDLActivity

@rem Create R.java
call %AAPT_PATH% package -f -m -S %DEV_HOME%/res -J %DEV_HOME%/src -M %DEV_HOME%/AndroidManifest.xml -I %ANDROID_JAR%

@rem Compile class files
call %JAVA_HOME%/bin/javac -d %DEV_HOME%/obj -cp %ANDROID_JAR% -sourcepath %DEV_HOME%/src %DEV_HOME%/src/%PACKAGE_PATH%/*.java

@rem Convert class to dex
call %DX_PATH% --dex --output=%DEV_HOME%/bin/classes.dex %DEV_HOME%/obj

@rem Package dex abd resources to unsigned apk
call %AAPT_PATH% package -f -M %DEV_HOME%/AndroidManifest.xml -S %DEV_HOME%/res -I %ANDROID_JAR% -F %DEV_HOME%/bin/AndroidTest.unsigned.apk %DEV_HOME%/bin

@rem ADD Libs
@set UNSIGNED_PATH=bin/AndroidTest.unsigned.apk
call  %AAPT_PATH% add %UNSIGNED_PATH% lib/armeabi-v7a/libmain.so
call  %AAPT_PATH% add %UNSIGNED_PATH% lib/armeabi-v7a/libSDL2.so

@rem Create signed apk
call %JAVA_HOME%/bin/keytool -genkey -validity 10000 -dname "CN=AndroidDebug, O=Android, C=US" -keystore %DEV_HOME%/AndroidTest.keystore -storepass android -keypass android -alias androiddebugkey -keyalg RSA -v -keysize 2048
call %JAVA_HOME%/bin/jarsigner -sigalg SHA1withRSA -digestalg SHA1 -keystore %DEV_HOME%/AndroidTest.keystore -storepass android -keypass android -signedjar %DEV_HOME%/bin/AndroidTest.signed.apk %DEV_HOME%/bin/AndroidTest.unsigned.apk androiddebugkey

I can’t really help you with that. Maybe you should try if it works with gradle.