Android ndk build linux

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

This is a basic tutorial repo demonstrating a simple Java-to-C/C++ building on Android via the «ndk-build» process on Android Development Studio 4

License

ravenholm/android_ndk_build_tutorial

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Create an Android NDK build with a basic C/C++ method

This is a basic tutorial repo demonstrating a simple Java-to-C/C++ building on Android via the "ndk-build" process on Android Development Studio 4 

Downloading, configuring and the installing Android NDK

  • Android NDK can be download in a couple of ways.
  1. Method 1: Go to [NDK Downloads] (https://developer.android.com/ndk/downloads) and download the latest version
  • Extract the files at a suitable place and
  1. Method 2: Install via Android Development Studio’s SDK manager
  • Go to the SDK Manager, SDK Tools and then select the NDK Side-by-side option
  • This will install the latest version of the NDK in Linux to the «/home/ubuntu/Android/Sdk/ndk/» location

Regardless of the method given above, you’ll have to ensure the Android NDK is selected in your native project (explained later on)

  • Now create an Android project with an empty activity MainActivity.java
  • Go to File->Project Structure-> SDK Location and make sure the NDK location is selected
  • This can also be changed by going to Project view -> local.properties and checking the following example entry is visible:
ndk.dir=/home/ubuntu/Android/Sdk/ndk/21.3.6528147

In your Project view, add a jni folder

Читайте также:  How to change timezone linux

#includejni.h> #includestring.h> jstring Java_com_example_androidndktutorial_MainActivity_helloWorld(JNIEnv* env, jobject obj)< return (*env)->NewStringUTF(env, "Hello World 123"); >

Code explanation of ndktest.c: The native C function showing in this method has the following crucial elements:

  • The method’s name is basically the complete Java package of MainActivity.java start from the Java folder including the actual helloWorld method that we’ll be calling
  • This makes the entire route to be Java->com.example.androidndktutorial->MainActivity->helloWorld
  • Hence, in ndktest.c, the method transforms to Java_com_example_androidndktutorial_MainActivity_helloWorld
  • The method simply returns/sets a point to a string (*env)->NewStringUTF(env, «Hello World 123»)
  • This string is then mapped to the text property of the TextView control in the activity_main.xml file
  • Create an Android.mk file as follows: The file has a very simple setup indicating the ndktest.c source file name which is to be built by the ndk-build command
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := ndktest LOCAL_SRC_FILES := ndktest.c include $(BUILD_SHARED_LIBRARY)
APP_ABI := all APP_PLATFORM := android-16 
  • Go to the root folder of the Android project and enter the ndk-build command. This generates an output somewhat similar to the following:
[arm64-v8a] Compile : ndktest ndktest.c [arm64-v8a] SharedLibrary : libndktest.so [arm64-v8a] Install : libndktest.so => libs/arm64-v8a/libndktest.so [x86_64] Compile : ndktest ndktest.c [x86_64] SharedLibrary : libndktest.so [x86_64] Install : libndktest.so => libs/x86_64/libndktest.so [armeabi-v7a] Compile thumb : ndktest ndktest.c [armeabi-v7a] SharedLibrary : libndktest.so [armeabi-v7a] Install : libndktest.so => libs/armeabi-v7a/libndktest.so [x86] Compile : ndktest ndktest.c [x86] SharedLibrary : libndktest.so [x86] Install : libndktest.so => libs/x86/libndktest.so 
  • Now switch to the Project view and create a jniLibs folder in app/java/ location
  • Copy the entire set of folders shown above (i.e. arm64-v8a, x86_64, armeabi-v7a and x86 to this jniLibs folder)

Preparing the MainActivity.java

public native String helloWorld(); static < System.loadLibrary("ndktest"); >

This loads the related ndktest library as per our most recently moved build.

TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_text_at_present" android:id="@+id/text_hello_world" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
((TextView) findViewById(R.id.text_hello_world)).setText(helloWorld());

The code simply calls the native helloWorld() method which returns the hard-coded «Hello World» string reference which is then displayed in the TextView

  • Finally, we are ready to test our native Android C/C++ method by running our code which will eventually display the «Hello World» string the in the TextView control of the activity_main.xml file

About

This is a basic tutorial repo demonstrating a simple Java-to-C/C++ building on Android via the «ndk-build» process on Android Development Studio 4

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

How to build for Android on Linux (2.3)

Clone this wiki locally

How to build and setup for Android on Linux (2.3+)

Note: if you are on Windows, we now have a NuGet package! Go straight to Use on Android to get started.

First, follow all the instructions on Setup and Build on Linux. While some steps could be skipped if you only want to build for Android, it is useful to diagnose problems with a desktop build first.

Second, while our dependencies require a Linux system to build, once they are built you can transfer the products to a Windows computer and use them with your existing Android project under Eclipse. More information on this is provided in the relevant step. The C++ REST SDK depends on Boost, libiconv, and OpenSSL when used on Android. The script below will compile all of these dependencies for you.

For this walkthrough, we assume you are working within the Build_Android directory of the C++ REST SDK.

git clone https://github.com/Microsoft/cpprestsdk.git casablanca cd casablanca/Build_android

The following instructions are written and tested against the android NDK version r10. Future and past versions may or may not work. The android NDK is available at http://developer.android.com/tools/sdk/ndk/index.html. If version r10 is not available for download, hard links are provided below.

For this tutorial, we assume it is installed at ~/android-ndk-r10/ .

mkdir build cd build ../configure.sh --ndk ~/android-ndk-r10

This will build armv7 binaries under the build.armv7.debug/Binaries and build.armv7.release/Binaries folders.

If you want to run the tests for Android we have a test runner project that can used. All the tests can be built by running msbuild from the command line. For example the following setups up the Visual Studio tools and builds x86 Debug for the simulator.

.\setup_ps_env_VS2015.ps1 cd Release msbuild /p:Platform=x86 /p:Configuration=Debug

Once the build completes open the following androidproj file in Visual Studio, which packages the tests.

Release\tests\common\TestRunner\vs14.android\TestRunner.android.Packaging\TestRunner.android.Packaging.androidproj

Make sure the configuration is x86 Debug and then start debugging with F5. This will launch the Android emulator and start running the tests. Output of the tests goes to Logcat, which can be brought up the Visual Studio menu with Tools->Android Tools->Logcat. Look for messages with the tag ‘UnitTestpp’, in particular at the end of running the tests there are several ‘— Flush buffer —‘ messages in a row.

For reference, the older documentation is available at Setup and Build on Android 2.2.

Источник

How to install Android NDK on Ubuntu Linux ?

The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

To install and configure the NDK, follow these steps:

Download android ndk Linux 64 bit from http://developer.android.com/ndk/downloads/index.html , when we written this post, the latest Android version is R23B, which we downloaded and copied to /home/myuser/Android directory. Now, Lets extract this zip as,

$ unzip android-ndk-r23b-linux.zip

This will extract as “Extracting android-ndk-r23b” into folder “android-ndk-r23b” in ~/Android directory. When uncompressed, the NDK files are contained in a directory called android-ndk- . You can rename the NDK directory if necessary and you can move it to any location on your computer.

Export the NDK path into environment variables,

$ export PATH=$PATH:/home/myuser/Android/android-ndk-r23b

OR You can also edit your ~/.bashrc file and append above line to add NDK permanently to your path to avoid typing above command always.

Check if ndk-build is added to environment,

$ which ndk-build /home/myuser/Android/android-ndk-r23b/ndk-build

Sometimes, you may need to set NDK_HOME evnvironment variable which you can do the same way as above,

$ export NDK_HOME=/home/myuser/Android/android-ndk-r23b

Источник

Оцените статью
Adblock
detector