Commit 155087d106042a382badf531d6fb5e28cb662037

Sylvain Becker 2019-08-11T15:23:37

Fixed bug 3918 - CMake support for android project

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 14dc89c..ce8426d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -925,7 +925,9 @@ if(ANDROID)
     set(HAVE_SDL_VIDEO TRUE)
 
     # Core stuff
-    find_library(ANDROID_DL_LIBRARY dl)
+    # find_library(ANDROID_DL_LIBRARY dl)
+    # FIXME failing dlopen https://github.com/android-ndk/ndk/issues/929
+    find_library(ANDROID_DL_LIBRARY NAMES libdl.so dl)
     find_library(ANDROID_LOG_LIBRARY log)
     find_library(ANDROID_LIBRARY_LIBRARY android)
     list(APPEND EXTRA_LIBS ${ANDROID_DL_LIBRARY} ${ANDROID_LOG_LIBRARY} ${ANDROID_LIBRARY_LIBRARY})
@@ -1903,7 +1905,9 @@ if(ANDROID)
     set_target_properties(hidapi PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
     set_target_properties(hidapi PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
   endif()
-  target_link_libraries(hidapi log)
+  if(HAVE_HIDAPI)
+    target_link_libraries(hidapi log)
+  endif()
 endif()
 
 if(SDL_STATIC)
diff --git a/android-project/app/build.gradle b/android-project/app/build.gradle
index 7e238a5..bf3c35d 100644
--- a/android-project/app/build.gradle
+++ b/android-project/app/build.gradle
@@ -22,6 +22,11 @@ android {
                 arguments "APP_PLATFORM=android-16"
                 abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
             }
+            // cmake {
+            //     arguments "-DANDROID_APP_PLATFORM=android-16", "-DANDROID_STL=c++_static"
+            //     // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
+            //     abiFilters 'arm64-v8a'
+            // }
         }
     }
     buildTypes {
@@ -38,6 +43,9 @@ android {
             ndkBuild {
                 path 'jni/Android.mk'
             }
+            // cmake {
+            //     path 'jni/CMakeLists.txt'
+            // }
         }
        
     }
diff --git a/android-project/app/jni/CMakeLists.txt b/android-project/app/jni/CMakeLists.txt
new file mode 100644
index 0000000..17fcfd4
--- /dev/null
+++ b/android-project/app/jni/CMakeLists.txt
@@ -0,0 +1,18 @@
+cmake_minimum_required(VERSION 3.6)
+
+project(GAME)
+
+# Settings
+set(HIDAPI OFF CACHE BOOL "" FORCE)
+
+# armeabi-v7a requires cpufeatures library
+# include(AndroidNdkModules)
+# android_ndk_import_module_cpufeatures()
+
+
+# SDL sources are in a subfolder named "SDL"
+add_subdirectory(SDL)
+
+# Your game and its CMakeLists.txt are in a subfolder named "src"
+add_subdirectory(src)
+
diff --git a/android-project/app/jni/src/CMakeLists.txt b/android-project/app/jni/src/CMakeLists.txt
new file mode 100644
index 0000000..fb021f9
--- /dev/null
+++ b/android-project/app/jni/src/CMakeLists.txt
@@ -0,0 +1,13 @@
+cmake_minimum_required(VERSION 3.6)
+
+project(MY_APP)
+
+find_library(SDL2 SDL2)
+
+add_library(main SHARED)
+
+target_sources(main PRIVATE YourSourceHere.c)
+
+target_link_libraries(main SDL2)
+
+
diff --git a/docs/README-android.md b/docs/README-android.md
index 78dcdd4..fbd9f99 100644
--- a/docs/README-android.md
+++ b/docs/README-android.md
@@ -82,6 +82,23 @@ For more complex projects, follow these instructions:
 
 4b. If you want to build manually, run './gradlew installDebug' in the project directory. This compiles the .java, creates an .apk with the native code embedded, and installs it on any connected Android device
 
+
+If you already have a project that uses CMake, the instructions change somewhat:
+
+1. Do points 1 and 2 from the instruction above.
+2. Edit "<project>/app/build.gradle" to comment out or remove sections containing ndk-build
+   and uncomment the cmake sections. Add arguments to the CMake invocation as needed.
+3. Edit "<project>/app/jni/CMakeLists.txt" to include your project (it defaults to
+   adding the "src" subdirectory). Note that you'll have SDL2, SDL2main and SDL2-static
+   as targets in your project, so you should have "target_link_libraries(yourgame SDL2 SDL2main)"
+   in your CMakeLists.txt file. Also be aware that you should use add_library() instead of
+   add_executable() for the target containing your "main" function.
+
+If you wish to use Android Studio, you can skip the last step.
+
+4. Run './gradlew installDebug' or './gradlew installRelease' in the project directory. It will build and install your .apk on any
+   connected Android device
+
 Here's an explanation of the files in the Android project, so you can customize them:
 
     android-project/app
@@ -90,10 +107,12 @@ Here's an explanation of the files in the Android project, so you can customize 
         jni/			- directory holding native code
         jni/Application.mk	- Application JNI settings, including target platform and STL library
         jni/Android.mk		- Android makefile that can call recursively the Android.mk files in all subdirectories
+        jni/CMakeLists.txt	- Top-level CMake project that adds SDL as a subproject
         jni/SDL/		- (symlink to) directory holding the SDL library files
         jni/SDL/Android.mk	- Android makefile for creating the SDL shared library
         jni/src/		- directory holding your C/C++ source
         jni/src/Android.mk	- Android makefile that you should customize to include your source code and any library references
+        jni/src/CMakeLists.txt	- CMake file that you may customize to include your source code and any library references
         src/main/assets/	- directory holding asset files for your application
         src/main/res/		- directory holding resources for your application
         src/main/res/mipmap-*	- directories holding icons for different phone hardware