Android NDK Environment Configuration

Source: Internet
Author: User

Introduction NDK full name is native development KIT,NDK offers a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. The NDK integrates a cross compiler (a cross compiler requires a UNIX or Linux system environment), and provides the corresponding MK file isolation CPU, platform, ABI, and other differences, developers simply need to modify the Mk file ("which files need to compile", "compile feature requirements", etc.), you can create a so.
the full name of JNI is Java Native Interface, which provides a number of APIs that enable the communication of Java and other languages (mainly C and C + +). Contact and difference:Why use the NDK?1, the protection of the Code. Because the Java layer Code of the APK is very easy to decompile, the C + + library is more difficult to reverse. 2. It is easy to use the existing open Source library. Most of the existing open source libraries are written in C + + code. 3, improve the execution efficiency of the procedure. Use c development for application logic that requires high performance to improve application execution efficiency. 4, easy to transplant. The library can be used in other embedded platforms easily. Why use JNI? The purpose of JNI is to enable Java methods to invoke some of the functions implemented by C. So it can be seen from here that the NDK was developed first, and then the JNI call was made.
Environment
    • Host: WIN10
    • Development environment: Android Studio2.2.2
The first thing to do is install the NDK environment on your computer, if you don't have it installed in studio

Configuring Environment variables

    • Add an entry: Ndk_root, such as: D:\android\sdk\ndk-bundle (this is the path to the SDK)
    • Add%ndk_root% to Path

New HELLO-JNI.C 
/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include <string.h> #include <jni.h>/* This was a trivial JNI example where we use a native method * to Retu RN a new VM String. See the corresponding Java source * file located at: * * APPS/SAMPLES/HELLO-JNI/PROJECT/SRC/COM/EXAMPLE/HELLOJNI/HELLOJN                                                  I.java *///jstring//java_com_bazhangkeji_mainactivity_stringfromjni (jnienv* env,//Jobject thiz)//{//return (*env)->newstringutf (env, "Hello from JNI!"); /}jniexport jstring Jnicalljava_com_xzh_ndkdemo_mainactivity_stringfromjni (jnienv *env, jobject instance) {//Return (    *ENV)->newstringutf (env, returnvalue); Return (*ENV)->newstringutf (env, "Hello from JNI!");}
# Copyright (C) The Android Open Source project## Licensed under the Apache License, Version 2.0 (the "License"); # yo U may not use this file except in compliance with the license.# your may obtain a copy of the License at## http://www.a      pache.org/licenses/license-2.0## unless required by applicable law or agreed to in writing, software# distributed under th E License is distributed on a "as is" basis,# without warranties OR CONDITIONS of any KIND, either express or implied.# S EE the License for the specific language governing permissions and# limitations under the License. #LOCAL_PATH: = $ (Call my -DIR) include $ (clear_vars) local_module    : = hello_jnilocal_src_files: = Hello_jni.cinclude $ (build_shared_library )

Note that the. c notation must follow the following rules: The function should be named according to the rule: Java_ Package name Class name Method name
Local_module    : = hello_jnilocal_src_files: = hello_jni.c
must correspond to the. c file, and then we add the following statement to the Build.gradle:
Externalnativebuild {        ndkbuild {            path file ("Src\\main\\jni\\hello.mk")        }    }
Then rebuild.Test Activity
public class Mainactivity extends Appcompatactivity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Init ();    }    private void Init () {        System.out.println ("Hello Ndkdemo" +stringfromjni ());    }    Public native String Stringfromjni ();    static {        system.loadlibrary ("Hello_jni");}    }



If we have a. So file, you need to configure the path to the local NDK in the Local.properties file.
Ndk.dir=/users/cinba/library/android-ndk-r10esdk.dir=/users/cinba/library/android-sdk
Then build again and you'll see the. so file now



Android NDK Environment Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.