BOOtak
BOOtak

Reputation: 124

Build PIE and non-PIE executables in Android Studio 3.0

My Android project should generate native executable files as part of the build and support all devices from API 14 (Android 4.0). The thing is that 4.0 supports only non-pie executables, and Android > 5.0 supports only pie executables. It means that I should generate both pie and non-pie executables, and I can't find any suitable way to achieve that on my current setup (Android Studio 3.0, NDK r16, llvm-5.0). I was using the following workaround for Android Studio 2.3 and NDK r15 in my CMakeLists.txt file:

add_executable(hello
    src/main/cpp/main.cpp
    )

add_executable(hello-nonpie
    src/main/cpp/main.cpp
    )

target_compile_definitions(hello
    PRIVATE
    -DANDROID_PIE=ON
    )

target_compile_definitions(hello-nonpie
    PRIVATE
    -DANDROID_PIE=OFF
    )

This hack does not work anymore on AS 3.0 and NDK r16 - it produces non-pie binaries only as my minSdkVersion is set to 14, or pie binaries only if I explicitly pass -DANDROID_PIE=ON argument to cmake.

The only way I found to embed pie and non-pie versions in one build was to create two identical Android library modules, with different values of -DANDROID_PIE argument in their build.gradle files and make app module depend on them. It does work, but slows down configure & build time more than twice as now I have not one but three projects (app itself and two modules). Any thoughts on solving this problem would be appreciated.

Upvotes: 0

Views: 816

Answers (1)

Dan Albert
Dan Albert

Reputation: 10509

I answered on our mailing list, but including here for completeness:

This hack does not work anymore on AS 3.0 and NDK r16 - it produces non-PIE binaries only as my minSdkVersion is set to 14, or PIE binaries only if I explicitly pass -DANDROID_PIE=ON argument to CMake.

I don't really understand how your approach ever worked. -DANDROID_PIE as a preprocessor definition changes nothing. What you would want to do is set the -fPIE cflag and -pie ldflag for the with-pie executable.

Alternatively, you could bump your minSdkVersion up to 16. There are barely any ICS devices left these days, and I suspect that a good many of those that remain are just being used as alarm clocks and TV remotes.

Upvotes: 1

Related Questions