2

I took the android native activity sample from Android Studio and replaced the app glue implementation by my own implementation of native activity.

So here is my Cmake file which almost the same as in the sample:

cmake_minimum_required(VERSION 3.4.1)

#my implementation
add_library(android-impl STATIC
            C:/android_libs/native-impl/Activity.cpp
            )

# now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

add_library(my-app SHARED
            main.cpp
            )

#add include directories

target_include_directories(my-app PRIVATE
                           C:/android_libs/native-impl/)

# add lib dependencies
target_link_libraries(my-app
                      android
                      android-impl
                      EGL
                      GLESv1_CM
                      log)

And I also specified in the Android manifest file the name of my shared lib like this:

    <!-- Tell NativeActivity the name of our .so -->
    <meta-data android:name="android.app.lib_name"
        android:value="my-app" />

It all compiles, but when I run on my device it instantly throws an error:

Caused by: java.lang.IllegalArgumentException: Unable to load native library: /data/app/com.nativetest.myapp-1/lib/arm/libmy-app.so

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nativetest.myapp/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/app/com.nativetest.myapp-2/lib/arm/libmy-app.so

I don't understand because I have copied the same Cmake file from the sample provided for native activity and just replaced app glue with my implementation, and used different name for my shared library.

Here are the files from the sample that I used: Cmakelist.txt AndroidManifest.xml

Is there something else that I need to modify ?

thp9
  • 376
  • 2
  • 15
  • How do you build your Android application? In case you use Android Studio, make sure your .so file is where Android Studio can find it and package into .apk file. Check http://stackoverflow.com/questions/24357687/how-to-include-so-library-in-android-studio – ivan_onys Jan 14 '17 at 17:13
  • Yes I am using android studio. I don't understand much about building process and etc.. but I do not have any .so files. How do I generate it ? I was expecting that it would be created by the cmake file – thp9 Jan 14 '17 at 17:20
  • The .so file is generated well and Android Studio finds it, it just can't load it. – thp9 Jan 14 '17 at 17:32
  • In what folder is generated .so file (relative to Android project root)? How do you know, that Android Studio finds it? – ivan_onys Jan 14 '17 at 18:14
  • in 'app\build\intermediates\transforms\stripDebugSymbol\debug\folders\2000\1f\main\lib\(arm, x86 etc..)' Error says 'cannot load' and not 'cannot find' so I guess it founds, and it works with the sample project so I guess there is a problem related to native activity and my implementation, trying to figure it out... – thp9 Jan 14 '17 at 18:26
  • OK ... so I figured out, but someone needs to explain that to me because it seems so weird. https://github.com/googlesamples/android-ndk/blob/master/native-activity/app/src/main/cpp/main.cpp , line 263, call to app_dummy which is an empty void function, if you remove that from the sample code you got the same exact error. So I simply added an empty function just like in the sample, call it and now it works. Weird ! – thp9 Jan 14 '17 at 18:41

1 Answers1

2

After hours of comparing my code to the sample code I have finally figured out what was wrong, which was caused by a lack of .. one line of code.

The problem seems to be related to the native activity implementation, which uses callbacks, since I am not well understanding what's going on, I'll link to the only explanation I've found: http://blog.beuc.net/posts/Make_sure_glue_isn__39__t_stripped/

So the solution is simply to have an empty function or whatever in your native activity implementation (which is app glue in the sample code), and you need to call it through your shared lib, your main code.

If someone would like to explain more in detail, or give a nicer alternative to this workaround it would be welcome.

thp9
  • 376
  • 2
  • 15
  • I assume your **Activity.cpp** is a modified version of [**android_native_app_glue.c**](https://github.com/chicken-mobile/android-ndk/blob/master/android-native-app/android_native_app_glue.c). In this case, you could simply add this file to the list of sources for **my-app** SHARED library, and remove all references to the **android-impl** STATIC library. I don't know why Google sample uses a static library here. The problem with static lib is that its parts will be 'optimized' (wiped from the resulting SHARED library) if the linker does not understand that these parts are necessary. – Alex Cohn Jan 15 '17 at 12:12