Gradle skips Install-phase of ndkBuild

363 views
Skip to first unread message

mic _

unread,
Apr 24, 2017, 5:13:11 AM4/24/17
to andro...@googlegroups.com
Environment:

Windows 10 Pro 64-bit (also tested on Windows 7 64-bit with the same results)
Android Studio 2.3.1 (build 162.3871768)
Gradle plugin 2.3.1 (Gradle 3.3)

I created a new project with C++ in Android studio, then replaced all the cmake stuff with ndkBuild, i.e:

        externalNativeBuild {
            ndkBuild {
                abiFilters "armeabi-v7a"
                arguments "APP_STL:=gnustl_static"
                cppFlags "-std=c++11","-frtti","-fexceptions"
            }
        }

    externalNativeBuild {
        ndkBuild {
            path "Android.mk"
        }
    }

Android.mk contents:

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)

    LOCAL_MODULE := native-lib
    LOCAL_SRC_FILES := src/main/cpp/native-lib.cpp
    include $(BUILD_SHARED_LIBRARY)
    $(info Building goal $(MAKECMDGOALS) with local_installed $(LOCAL_INSTALLED))

This gives me the following ndkBuild_build_output.txt in .externalNativeBuild/ndkBuild/debug/armeabi-v7a after the Gradle sync:
Building goal  with local_installed C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/libnative-lib.so
md "C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib\armeabi-v7a" >NUL 2>NUL || rem
C:/Android/android-sdk/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe [armeabi-v7a] "Gdbserver      ": "[arm-linux-androideabi] C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdbserver"
copy /b/y "C:\Android\android-sdk\ndk-bundle\build\\..\prebuilt\android-arm\gdbserver\gdbserver" "C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib\armeabi-v7a\gdbserver" > NUL
C:/Android/android-sdk/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe [armeabi-v7a] "Gdbsetup       ": "C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdb.setup"
C:/Android/android-sdk/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe "set solib-search-path C:/Android/apps/NdkBuildTest/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a" > C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdb.setup
C:/Android/android-sdk/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe "directory C:/Android/android-sdk/ndk-bundle/build//../platforms/android-21/arch-arm C:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++ C:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++/4.9/include C:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include C:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++/4.9/include/backward C:/Android/apps/NdkBuildTest/app" >> C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/gdb.setup
md "C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\obj\local\armeabi-v7a\objs-debug\native-lib\src\main\cpp" >NUL 2>NUL || rem
C:/Android/android-sdk/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe [armeabi-v7a] "Compile++ thumb": "native-lib <= native-lib.cpp"
C:/Android/android-sdk/ndk-bundle/build//../toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -MMD -MP -MF C:/Android/apps/NdkBuildTest/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/objs-debug/native-lib/src/main/cpp/native-lib.o.d -gcc-toolchain C:/Android/android-sdk/ndk-bundle/build//../toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 -fpic -ffunction-sections -funwind-tables -fstack-protector-strong -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes -fno-integrated-as -g -target armv7-none-linux-androideabi -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-exceptions -fno-rtti -mthumb -O0 -UNDEBUG -fno-limit-debug-info  -IC:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++/4.9/include -IC:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -IC:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++/4.9/include/backward -IC:/Android/apps/NdkBuildTest/app -DANDROID  -Wa,--noexecstack -Wformat -Werror=format-security    -std=c++11 -frtti -fexceptions  --sysroot C:/Android/android-sdk/ndk-bundle/build//../platforms/android-21/arch-arm  -c  C:/Android/apps/NdkBuildTest/app/src/main/cpp/native-lib.cpp -o C:/Android/apps/NdkBuildTest/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/objs-debug/native-lib/src/main/cpp/native-lib.o 
md "C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\obj\local\armeabi-v7a" >NUL 2>NUL || rem
md "C:\Android\android-sdk\ndk-bundle\build\\..\sources\cxx-stl\gnu-libstdc++\4.9\libs\armeabi-v7a" >NUL 2>NUL || rem
C:/Android/android-sdk/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe [armeabi-v7a] "SharedLibrary  ": "libnative-lib.so"
C:/Android/android-sdk/ndk-bundle/build//../toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -Wl,-soname,libnative-lib.so -shared --sysroot=C:/Android/android-sdk/ndk-bundle/build//../platforms/android-21/arch-arm C:/Android/apps/NdkBuildTest/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/objs-debug/native-lib/src/main/cpp/native-lib.o C:/Android/android-sdk/ndk-bundle/build//../sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a -lgcc  -gcc-toolchain C:/Android/android-sdk/ndk-bundle/build//../toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 -no-canonical-prefixes -target armv7-none-linux-androideabi -Wl,--fix-cortex-a8  -Wl,--build-id -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings    -lc -lm -o C:/Android/apps/NdkBuildTest/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/libnative-lib.so
C:/Android/android-sdk/ndk-bundle/build//../prebuilt/windows-x86_64/bin/echo.exe [armeabi-v7a] "Install        ": "libnative-lib.so => C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/libnative-lib.so"
copy /b/y "C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\obj\local\armeabi-v7a\libnative-lib.so" "C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib\armeabi-v7a\libnative-lib.so" > NUL
C:/Android/android-sdk/ndk-bundle/build//../toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-strip --strip-unneeded  C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/libnative-lib.so

And the following output in the Gradle console when doing Make Project:

    :app:externalNativeBuildDebug
    Build native-lib armeabi-v7a
    Building goal C:/Android/apps/NdkBuildTest/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/libnative-lib.so with local_installed C:\Android\apps\NdkBuildTest\app\build\intermediates\ndkBuild\debug\lib/armeabi-v7a/libnative-lib.so
    [armeabi-v7a] Compile++ thumb: native-lib <= native-lib.cpp
    [armeabi-v7a] SharedLibrary  : libnative-lib.so
    :app:compileDebugSources

..looking for any resulting .so files:
    $ find . -name *.so
    ./app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/libnative-lib.so

It's as if the native build stops right before the "Install" phase. Is this a bug, or the expected behavior? If it's the expected behavior, how do I get the behavior I want (i.e. that install/strip is performed when I make/rebuild my project from the IDE or assemble from the command line)?
Note that even an explicit host-cp of $(LOCAL_BUILT_MODULE) in the makefile doesn't appear to do anything (while it used to work fine when I was building with Ant).

/Michael
 

Alex Cohn

unread,
May 9, 2017, 5:27:45 AM5/9/17
to android-ndk
I believe that this is the way the gradle plugin works. It invokes ndk-build with parameter (target) C:/Android/apps/NdkBuildTest/app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/libnative-lib.so, and 

Later, Android Studio merges all JniLibs together and strips them in a separate 'transformation':

-rwxr-xr-x  62220 11:02 app/build/intermediates/ndkBuild/debug/obj/local/armeabi/libhello-jni.so

-rw-r--r--  62220 11:04 app/build/intermediates/transforms/mergeJniLibs/debug/folders/2000/1f/main/lib/armeabi/libhello-jni.so

-rwxr-xr-x  13724 11:04 app/build/intermediates/transforms/stripDebugSymbol/debug/folders/2000/1f/main/lib/armeabi/libhello-jni.so


BR,

Alex Cohn

mic _

unread,
May 9, 2017, 6:14:42 AM5/9/17
to andro...@googlegroups.com
Do you happen to know how to override that transformation stage in my gradle script, if that's possible? I'd like to inspect and move the stripped .so as part of the build process.

/Michael

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/c62e1e49-6279-4ba9-8a63-a49bbdaeeeff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Cohn

unread,
May 21, 2017, 6:39:53 AM5/21/17
to android-ndk
Sorry didn't see your reply before.

I don't understand what your goal is, what do you want to inspect and move, and why. We always use command-line `ndk-build` for release, but mainly for historical reasons: the people who maintain the build servers are (as they should be) very conservative.

BR,
Alex
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

mic _

unread,
May 22, 2017, 3:40:05 AM5/22/17
to andro...@googlegroups.com
I apply some 3rd party tools while building my native library, and I have automated the process of verifying that those tools have been applied correctly on the library that will go into production. With Ant this is relatively easy, because I can just override the "-dex" target and inspect the stripped .so file from there, and fail the build before an APK has been generated if necessary.
I also package the library in a non-standard way because I've seen crashes in Google Play in the past where .so files weren't installed correctly if they were packaged/loaded in the standard way. This involves copying the stripped library to another directory, which is easy to do from my Android.mk if I can rely on $(LOCAL_INSTALLED) actually having been generated.

/Michael

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Alex Cohn

unread,
May 24, 2017, 2:24:51 PM5/24/17
to android-ndk
From your explanation, you don't benefit from ndk-build integration into Android Studio. If you still want to have ndk-build run with Ctrl-F9 key, you can add a custom buildNative task to your gradle script (see https://stackoverflow.com/a/32640823/192373 for inspiration), so that JavaCompileXXX depends on buildNative.

To disable the integrated build, the common practice is to set jni.srcDirs to [] or some non-existent path. But I prefer to set jni.srcDirs correctly and disable the integrated compileXXXNdk task:

tasks.all {

    task ->

        if (task.name.startsWith('compile') && task.name.endsWith('Ndk')) {

            task.enabled = false

        }

}


BR,
Alex Cohn
Reply all
Reply to author
Forward
0 new messages