Fixed bug 3918 - CMake support for android project
authorSylvain Becker <sylvain.becker@gmail.com>
Sun, 11 Aug 2019 15:23:37 +0200
changeset 130064a410f099040
parent 13005 ff2e36039fd2
child 13007 e18a8781bbc7
Fixed bug 3918 - CMake support for android project
CMakeLists.txt
android-project/app/build.gradle
android-project/app/jni/CMakeLists.txt
android-project/app/jni/src/CMakeLists.txt
docs/README-android.md
     1.1 --- a/CMakeLists.txt	Thu Aug 08 23:35:02 2019 +0300
     1.2 +++ b/CMakeLists.txt	Sun Aug 11 15:23:37 2019 +0200
     1.3 @@ -925,7 +925,9 @@
     1.4      set(HAVE_SDL_VIDEO TRUE)
     1.5  
     1.6      # Core stuff
     1.7 -    find_library(ANDROID_DL_LIBRARY dl)
     1.8 +    # find_library(ANDROID_DL_LIBRARY dl)
     1.9 +    # FIXME failing dlopen https://github.com/android-ndk/ndk/issues/929
    1.10 +    find_library(ANDROID_DL_LIBRARY NAMES libdl.so dl)
    1.11      find_library(ANDROID_LOG_LIBRARY log)
    1.12      find_library(ANDROID_LIBRARY_LIBRARY android)
    1.13      list(APPEND EXTRA_LIBS ${ANDROID_DL_LIBRARY} ${ANDROID_LOG_LIBRARY} ${ANDROID_LIBRARY_LIBRARY})
    1.14 @@ -1903,7 +1905,9 @@
    1.15      set_target_properties(hidapi PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
    1.16      set_target_properties(hidapi PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
    1.17    endif()
    1.18 -  target_link_libraries(hidapi log)
    1.19 +  if(HAVE_HIDAPI)
    1.20 +    target_link_libraries(hidapi log)
    1.21 +  endif()
    1.22  endif()
    1.23  
    1.24  if(SDL_STATIC)
     2.1 --- a/android-project/app/build.gradle	Thu Aug 08 23:35:02 2019 +0300
     2.2 +++ b/android-project/app/build.gradle	Sun Aug 11 15:23:37 2019 +0200
     2.3 @@ -22,6 +22,11 @@
     2.4                  arguments "APP_PLATFORM=android-16"
     2.5                  abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
     2.6              }
     2.7 +            // cmake {
     2.8 +            //     arguments "-DANDROID_APP_PLATFORM=android-16", "-DANDROID_STL=c++_static"
     2.9 +            //     // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
    2.10 +            //     abiFilters 'arm64-v8a'
    2.11 +            // }
    2.12          }
    2.13      }
    2.14      buildTypes {
    2.15 @@ -38,6 +43,9 @@
    2.16              ndkBuild {
    2.17                  path 'jni/Android.mk'
    2.18              }
    2.19 +            // cmake {
    2.20 +            //     path 'jni/CMakeLists.txt'
    2.21 +            // }
    2.22          }
    2.23         
    2.24      }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/android-project/app/jni/CMakeLists.txt	Sun Aug 11 15:23:37 2019 +0200
     3.3 @@ -0,0 +1,18 @@
     3.4 +cmake_minimum_required(VERSION 3.6)
     3.5 +
     3.6 +project(GAME)
     3.7 +
     3.8 +# Settings
     3.9 +set(HIDAPI OFF CACHE BOOL "" FORCE)
    3.10 +
    3.11 +# armeabi-v7a requires cpufeatures library
    3.12 +# include(AndroidNdkModules)
    3.13 +# android_ndk_import_module_cpufeatures()
    3.14 +
    3.15 +
    3.16 +# SDL sources are in a subfolder named "SDL"
    3.17 +add_subdirectory(SDL)
    3.18 +
    3.19 +# Your game and its CMakeLists.txt are in a subfolder named "src"
    3.20 +add_subdirectory(src)
    3.21 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/android-project/app/jni/src/CMakeLists.txt	Sun Aug 11 15:23:37 2019 +0200
     4.3 @@ -0,0 +1,13 @@
     4.4 +cmake_minimum_required(VERSION 3.6)
     4.5 +
     4.6 +project(MY_APP)
     4.7 +
     4.8 +find_library(SDL2 SDL2)
     4.9 +
    4.10 +add_library(main SHARED)
    4.11 +
    4.12 +target_sources(main PRIVATE YourSourceHere.c)
    4.13 +
    4.14 +target_link_libraries(main SDL2)
    4.15 +
    4.16 +
     5.1 --- a/docs/README-android.md	Thu Aug 08 23:35:02 2019 +0300
     5.2 +++ b/docs/README-android.md	Sun Aug 11 15:23:37 2019 +0200
     5.3 @@ -82,6 +82,23 @@
     5.4  
     5.5  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
     5.6  
     5.7 +
     5.8 +If you already have a project that uses CMake, the instructions change somewhat:
     5.9 +
    5.10 +1. Do points 1 and 2 from the instruction above.
    5.11 +2. Edit "<project>/app/build.gradle" to comment out or remove sections containing ndk-build
    5.12 +   and uncomment the cmake sections. Add arguments to the CMake invocation as needed.
    5.13 +3. Edit "<project>/app/jni/CMakeLists.txt" to include your project (it defaults to
    5.14 +   adding the "src" subdirectory). Note that you'll have SDL2, SDL2main and SDL2-static
    5.15 +   as targets in your project, so you should have "target_link_libraries(yourgame SDL2 SDL2main)"
    5.16 +   in your CMakeLists.txt file. Also be aware that you should use add_library() instead of
    5.17 +   add_executable() for the target containing your "main" function.
    5.18 +
    5.19 +If you wish to use Android Studio, you can skip the last step.
    5.20 +
    5.21 +4. Run './gradlew installDebug' or './gradlew installRelease' in the project directory. It will build and install your .apk on any
    5.22 +   connected Android device
    5.23 +
    5.24  Here's an explanation of the files in the Android project, so you can customize them:
    5.25  
    5.26      android-project/app
    5.27 @@ -90,10 +107,12 @@
    5.28          jni/			- directory holding native code
    5.29          jni/Application.mk	- Application JNI settings, including target platform and STL library
    5.30          jni/Android.mk		- Android makefile that can call recursively the Android.mk files in all subdirectories
    5.31 +        jni/CMakeLists.txt	- Top-level CMake project that adds SDL as a subproject
    5.32          jni/SDL/		- (symlink to) directory holding the SDL library files
    5.33          jni/SDL/Android.mk	- Android makefile for creating the SDL shared library
    5.34          jni/src/		- directory holding your C/C++ source
    5.35          jni/src/Android.mk	- Android makefile that you should customize to include your source code and any library references
    5.36 +        jni/src/CMakeLists.txt	- CMake file that you may customize to include your source code and any library references
    5.37          src/main/assets/	- directory holding asset files for your application
    5.38          src/main/res/		- directory holding resources for your application
    5.39          src/main/res/mipmap-*	- directories holding icons for different phone hardware