Let’s try to add some more chaos.
My gradle app file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.github.urho3d"
minSdkVersion 12
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang"
arguments "-DANDROID_STL=c++_static"
arguments "-DCMAKE_BUILD_TYPE=Debug"
arguments "-DCMAKE_CXX_FLAGS=-std=c++11"
arguments "-DANDROID_CPP_FEATURES=exceptions rtti"
abiFilters "armeabi-v7a"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
// prefer local.properties cmake path setup
// version "3.6.4111459"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dexOptions {
javaMaxHeapSize "4g"
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:23.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.1'
}
The CMakelist.txt file:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
set (CMAKE_VERBOSE_MAKEFILE "ON")
# set values from environment
set (URHO_DISTRO $ENV{URHO_DISTRO}) #usr/local/Urho/Urho3D
set (URHO_BUILD $ENV{URHO_BUILD}) #usr/local/Urho/Urho3D/build
# set values from gradle
# set by gradle in app/build.gradle file "abiFilters"
set (URHO_ABI ${ANDROID_ABI})
# as ANDROID_ABI comes from gradle build script, so it'd be ANDROID_BUILDTYPE (the flavor) but as 2.3 I wasn't able to
# find anything about that. Stackoverflow question here https://stackoverflow.com/questions/43395768/how-to-get-android-build-type-debug-release-as-a-variable-in-a-cmakelists-txt
# went unanswered.
# FIXME: take URHO_FLAVOR from gradle.build
set (URHO_FLAVOR $ENV{URHO_FLAVOR}) # Debug or Release
# log used vars. HAS FU*(&%$)IN CMAKE A BETTER LOG SYSTEM?
# TODO: human-readable log
set (VARS ${URHO_DISTRO} ${URHO_BUILD} ${URHO_FLAVOR} ${URHO_ABI})
file (WRITE VARS.txt)
file (WRITE VARS.txt ${VARS})
# creates sources list
file (GLOB_RECURSE HEADERS ${CMAKE_SOURCE_DIR}/src/main/cpp/src FOLLOW_SYMLINKS ${CMAKE_SOURCE_DIR}/src/main/cpp/src/*.h )
file (GLOB_RECURSE BASESOURCES ${CMAKE_SOURCE_DIR}/src/main/cpp/src FOLLOW_SYMLINKS ${CMAKE_SOURCE_DIR}/src/main/cpp/src/*.cpp )
file (GLOB_RECURSE EXAMPLES ${CMAKE_SOURCE_DIR}/src/main/cpp/src FOLLOW_SYMLINKS ${CMAKE_SOURCE_DIR}/src/main/cpp/src/*.inl )
# it seems it needs c library with JNI objs to avoid linking problems (android native_app_glue)
# when build single app with static libs
set (TARGET ${URHO_DISTRO}/Source/ThirdParty/SDL/src/main/android/SDL_android_main.c)
set (SOURCES ${HEADERS} ${BASESOURCES} ${EXAMPLES} ${TARGET})
# log used sources
file (WRITE SOURCES.txt)
file (WRITE SOURCES.txt ${SOURCES})
# URHO3D_HOME is Urho3D library dir
set (URHO3D_HOME ${URHO_BUILD}/android/${URHO_FLAVOR}/${URHO_ABI})
include_directories (BEFORE ${URHO3D_HOME}/include/
BEFORE ${URHO3D_HOME}/include/Urho3D/ThirdParty
BEFORE ${URHO3D_HOME}/include/Urho3D/ThirdParty/Bullet
BEFORE ${URHO3D_HOME}/include/Urho3D/ThirdParty/SDL
)
# create our app lib
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${SOURCES}
)
# add Urho3D static lib
add_library ( Urho3D
STATIC
IMPORTED
)
set_target_properties( # Specifies the target library.
Urho3D
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
# this hack is horrible...
# urho3d home must be redefined as library/android/debug_OR_release/abi
# to account for gradle managing both flavors
# URHO3D_HOME=$URHO_BUILD/$URHO_PLATFORM/$URHO_FLAVOR/$URHO_ABI
${URHO3D_HOME}/libs/${URHO_ABI}/libUrho3D.a
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
# log
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# this is needed if you want to change the build tipe to a specific opengl version
# opengl2 (level9)
find_library( gl2-lib
GLESv2
)
#native-window (sdl2) (level9)
find_library( android-lib
android
)
# collect libraries
set (LIBRARIES ${Urho3D} ${native-lib} ${log-lib} ${android-lib} ${gl2-lib})
# log used libraries
file (WRITE LIBRARIES.txt)
file (WRITE LIBRARIES.txt ${LIBRARIES})
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
${gl2-lib}
${android-lib}
Urho3D
)
At line 39 is the point where I link to the sdl c++ native glue file which is contained into the Urho sources.
Problem is this setup tries to load a static Urho3d library and it won’t start if you don’t patch a line in app/src/main/java/com/github/urho3d/Urho3D.java like this:
// All shared shared libraries must always be loaded if available, so exclude it from return result and all list operations below
int startIndex = libraryNames.indexOf("Urho3DPlayer");
// check for third-party libraries/runners different from Urho3D Player whose name we don't know,
// so taking the first element. This way, an empty library list is going to throw an exception anyway
if (startIndex==-1 && !libraryNames.isEmpty()) startIndex = 0;
// Determine the intention
Intent intent = getIntent();
String pickedLibrary = intent.getStringExtra(SampleLauncher.PICKED_LIBRARY);
(fu*%/&%in markup)
But if you use a shared Urho library, it probably starts without this last patch…