📜 ⬆️ ⬇️

Creating APK x86 and ARM APK packages using the Intel® and GNU gcc compiler

There are Android devices on processors with ARM or x86 instruction set (ISA) architectures. Different instruction set architectures do not have binary compatibility, so an application containing native code must contain native libraries for each architecture. One of the mechanisms for the distribution of such applications are the so-called “thick” Android application packages (“thick” APK).

This article contains step-by-step instructions for creating such a thick APK package, which includes architecture-independent files for the Dalvik virtual machine (Dalvik, 2013), as well as libraries for different architectures. This article describes how to build a native x86 application library using the Intel Integrated Native Developer Experience (INDE).

For the demonstration, use the hello-jni example from the NDK distribution kit r10 (included in the Intel INDE installation).

Preparing the build environment


The following software is required. Install them following the instructions given.
1. Install:
Microsoft .NET Framework ,
Java Development Kit 7u67 must be installed in [jdk7-dir].
2. Add this folder to the PATH environment variable:
the [jdk7-dir] \ jre \ bin directory for using the java virtual machine. This is required for ant.
For the Windows environment, do not forget to specify the short folder name (type PROGRA ~ 2 ) for the program files (x86) .
3. Create a new environment variable:
JAVA_HOME = [jdk7-dir] .
4. Install:
Intel Integrated Native Developer Experience should be installed in [inde-dir].
5. Add this folder to the PATH environment variable:
[ant-dir] \ bin directory to use ant tool (default location of [ant-dir] : [inde-dir] \ IDEintegration )
')

Command line build


This section contains instructions for creating APK packages for supporting ARM and x86 Android devices in the command line build environment.
Open a command prompt window or terminal window in Linux; go to the Android NDK sample folder "hello-jni" [ndk-dir] \ samples \ hello-jni ; follow these steps.
Note. All of the tools listed in the section above should be available from this window.

1. Setting up the application
Run the following command in the hello-jni folder to create the build.xml file, which will later be used to build the example.
$ android update project --target android-19 --name HelloJni --path. --subprojects 


Note. --target android-19 corresponds to the version of Android 4.4 (KITKAT).

2. Cleaning the application workspace
Use the following command to clean the entire workspace, including libraries, for all ISA architectures.
  $ ant clean $ ndk-build V=1 APP_ABI=all clean 


Note : V = 1 : print all commands as they are executed. APP_ABI = all : force cleaning of all intermediate files for all target platforms. If you do not specify this option, only the file for the target platform armeabi will be cleared.

3. Build Binary Files for ARM Architecture
Use the following command to build the application's binary for the ARM architecture:
  $ ndk-build APP_ABI=armeabi-v7a V=1 NDK_TOOLCHAIN=arm-linux-androideabi-4.8 


Note : APP_ABI = armeabi-v7a : compile setting for target ARM platform. NDK_TOOLCHAIN ​​= arm-linux-androideabi-4.8 : instead of the default GNU gcc 4.6, use gcc 4.8.
The application binary file (libhello-jni.so) is created at . \ Libs \ armeabi-v7a \ libhello-jni.so .

4. Build binaries for x86 architecture using Intel compiler
Use the following command to build the x86 application binary using the Intel compiler.
  $ ndk-build APP_ABI=x86 V=1 NDK_TOOLCHAIN=x86-icc NDK_APP.local.cleaned_binaries=true 


Note : APP_ABI = x86 : compile setting for x86 architecture. NDK_TOOLCHAIN ​​= x86-icc : replacing the default compiler gcc 4.6 with the compiler Intel C / C ++ for Android. NDK_APP.local.cleaned_binaries = true : cancel deletion of the library in the libs / armeabi directory. See notes below.
The application binary file (libhello-jni.so) is created at . \ Libs \ x86 \ libhello-jni.so .

5. Preparing the application package (APK)
After building all the binaries for each target platform on Android, make sure that the libs \ [targetabi] folder contains the required libraries for each target architecture.
To simplify package creation and avoid signing it, use the following command to create a debug package.
  $ ant debug 


After that, the new HelloJni-debug.zip package will appear in the . \ Bin folder . It can be run in x86 or ARM simulators that support API level 19 or higher contained in the Android SDK.
The HelloJni-debug.zip package contains libraries for two target platforms: ARM EABI v7a and x86.

Build in the Eclipse IDE


1. Open Eclipse and download the [inde-dir] / IDEintegration / NDK / samples / hello-jni sample .
From the menu, select File> New> Project> Android .
Click the Android Project from Existing Code button.
In the Root Directory field, click the Browse ... button and select the [ndk-dir] / samples folder.
Then click Deselect All and select only the project you need: hello-jni
Uncheck the Copy projects into workspace checkbox.
Click Finish .

2. Add native code support to the project.
Right-click the project name and select Android Tools> Add Native Support ...
Click Finish .
Note. If after this step the Unable to launch cygpath error message appears on the console, you can skip this step, which will not affect the project.

3. Create a separate build configuration for each target platform, set the build command.
Right-click the project and select Project properties -> C / C ++ Build -> Manage configurations ...
Click New ... to add new configurations based on the default configuration.
Name: x86_icc Description: for x86 target platform using Intel icc compiler
Name: arm_gcc Description: For ARM target platform using the GNU gcc compiler

In the same project properties window, set the Configuration parameter to x86_icc.
Uncheck the box Use default build command .
Add the following text to the Build command field: ndk-build APP_ABI = x86 NDK_TOOLCHAIN ​​= x86-icc .
Click Apply .
In the same project properties window, set the Configuration parameter to arm_gcc.
Uncheck the box Use default build command .
Add the following text to the Build command field: ndk-build APP_ABI = armeabi-v7a NDK_TOOLCHAIN ​​= arm-linux-androideabi-4.8 .
Click Apply , then click OK .

4. Cleaning the entire application workspace
If the Application.mk file exists in the [eclipse-workspace-dir] \ HelloJni \ jni folder , make sure that it does not contain the following line.
NDK_APP.local.cleaned_binaries = true
Right-click the project and select Build configurations> Clean all ....

5. Create an Application.mk file in the [eclipse-workspace-dir] \ HelloJni \ jni folder , if it does not exist, and make sure that it contains the following line.
NDK_APP.local.cleaned_binaries = true

6. Build the binary for the ARM * target architecture using gcc and the x86 target architecture using the Intel compiler.
Right-click the project and select Build Configurations> Build Selected ...
Select arm_gcc and x86_icc.
Click OK.
Check the output binaries at the following addresses.
[eclipse-workspace-dir] \ HelloJni \ libs \ armeabi-v7a \ libhello-jni.so
[eclipse-workspace-dir] \ HelloJni \ libs \ x86 \ libhello-jni.so

7. Creating unsigned application packages
Right-click the project and select Android Tools> Export Unsigned Application Package ...

A note on the failure to clean binary files in. \ Libs \ [targetabi]


The NDK_APP.local.cleaned_binaries = true parameter to disable deleting previously collected libraries may not work in future versions of the NDK. See [ndk-dir] /build/core/setup-app.mk for information on how to disable uninstall actions for installed binaries.

Other related articles and resources




For more information about Intel for Android developers, see the Intel Developer Zone for Android .

Source: https://habr.com/ru/post/252883/


All Articles