Proper way of building both PIE and non-PIE executables in Android Studio 3.0?

110 views
Skip to first unread message

Кирилл Лейфер

unread,
Dec 20, 2017, 11:27:17 AM12/20/17
to android-ndk
Hi everyone,

My Android project generates native executables as part of the build, and also it should support all devices starting from API 14 (Android 4.0).

As you know, Android 4.0 supports only non-PIE executables, while Android > 5.0 does support only PIE ones.So 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 hack 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 both PIE and non-PIE versions in one build is to create two identical Android library modules. They build same binaries, but one of them passes `-DANDROID_PIE=ON` argument to CMake. Then I include those project as my app dependencies. 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, each of them configuring builds for multiple architectures).

Any thoughts on solving this problem more nicely?

Dan Albert

unread,
Dec 20, 2017, 1:33:25 PM12/20/17
to android-ndk
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.

--
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/6f423175-2cf5-4a56-9cc2-03c0bdd89c07%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages