Discussion:
Proper way of building both PIE and non-PIE executables in Android Studio 3.0?
Кирилл Лейфер
2017-12-20 10:18:13 UTC
Permalink
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?
--
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+***@googlegroups.com.
To post to this group, send email to android-***@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.
'Dan Albert' via android-ndk
2017-12-20 18:32:51 UTC
Permalink
Post by Кирилл Лейфер
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.
Post by Кирилл Лейфер
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
Post by Кирилл Лейфер
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
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?
--
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
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
<https://groups.google.com/d/msgid/android-ndk/6f423175-2cf5-4a56-9cc2-03c0bdd89c07%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to android-***@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/CAFVaGhtkNEP48wA%3DPhti4WNi60c9uUGXvN73s%2BzSsrpSg%2B4bGw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...