5

From what I understand, native code on Android is code that works directly with the processor of a specific device. So if I wanted to take advantage of a certain processor I would use native code.

But what happens if I want to make an app that contains native code, but targets multiple processors?

Do I have to make multiple apps, one for each architecture? Or is there a way to put multiple version of the native code in one app picking the one matching the processor of the device it runs on?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
JumpJump
  • 115
  • 1
  • 7
  • I've rephrased your question to make is clearer what you're asking. If I misunderstood you, feel free to revert. – CodesInChaos May 01 '14 at 17:14
  • Meta discussion [here](http://meta.stackoverflow.com/questions/253125/when-a-moderator-closes-a-question-they-misundertsand). – Robert Harvey May 01 '14 at 17:21

3 Answers3

4

The Android Native Development Kit is a suite of cross compilers and support libraries which can be used to produce shared object (.so) files targeting one or more of the officially supported Android architectures.

The Android application package (.apk) specialized zip file format allows inclusion of distinct native libraries for more than one architecture.

If you refer to the NDK documentation, you will see that there is a project configuration file which you can use to specify which architecture(s) your native code should be compiled to support.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
0

This is usually referred to as cross compiling. Ie, you need a compiler than compiles X, your current code, for Y. It generates code for CPU Y, not X as is the usual case.

Totoro
  • 615
  • 5
  • 11
  • Perhaps you misunderstood the question. This has nothing to do with cross-compiling, it's about targeting multiple CPU architectures. See Chris's answer. – Kevin May 01 '14 at 17:20
  • 2
    @Kevin - no, this answer is right on in what it says, though a bit brief. Cross-compilation is the *pragmatic* form of the *necessary* per-target compilation step required to support multiple native architectures from a single source. The Android NDK is a suite of cross compilers allowing a desktop operating system to cross compile for for a variety of Android architectures. – Chris Stratton May 01 '14 at 17:46
  • @ChrisStratton Yes, the answer states a technically correct fact, but it does not say anything about the question, namely how code compiled *for* one architecture can run on another. – Kevin May 01 '14 at 17:51
  • 1
    @Kevin - that is not something the question ever asked. Right from the start, the question has been quite explicitly about supporting multiple native architectures (with the meaning of "native" clearly stated). That requires multiple compilers, and pragmatically those will be cross-compilers. – Chris Stratton May 01 '14 at 17:53
  • "the question has been quite explicitly about supporting multiple native architectures" Precisely. This answer is about compiling for a different architecture, not running on one. – Kevin May 01 '14 at 17:55
  • 1
    @Kevin Pretty much all android development is "cross compilation" because a desktop is normally used to build code targeting a mobile architecture (or the unique DVM) which substantially differs from the building system. The "different" in this question clearly refers not to that difference, but to the variety of native architectures ("processor of a specific device" to quote the original) currently present in the Android ecosystem (two varieties of ARM, MIPS, x86, etc). Each of these will require its own (cross)-compiler to produce appropriate native code. – Chris Stratton May 01 '14 at 18:00
  • @ChrisStratton I understand cross-compiling and how it is used with Android development. By your own words, "the "different" in this question clearly refers not to that difference," but this answer does (and only to that difference, which the question is not about). – Kevin May 01 '14 at 18:23
  • 1
    @kevin - I can see your point, I'm just not getting too tripped up by the difference between needing a cross compiler and needing several - especially as it happens that in this case the several are distributed together in the same NDK toolchain package. I would agree the content of this answer isn't really sufficient though. – Chris Stratton May 01 '14 at 18:47
0

You need to target multiple CPU architecture only if you are developing a NDK application. Create a file named "Application.mk" under the jni folder. Add this parameter APP_ABI= Example : APP_ABI:=x86 armv7eabi mips or you can all do this APP_ABI:=all ( in which it would create the apk for all supported architecture) but doing this you would generate a FAT binary and google play will take care of filtering the corresponding apk for different underlying architecture when the user installs your app.

G3M
  • 1,023
  • 8
  • 19