"Android Development"-a simple example of NDK Jni development under Android Studio

Source: Internet
Author: User

Android has been developing for some time, on the one hand, feeling nothing left. A little sorry for myself,

On the other hand, good memory is inferior to bad writing, in order to later can look back, as a note, decided to start blogging. Don't say much nonsense!

Today I want to do the NDK and JNI, and now start writing a simple demo

1. Create a new project


2. Create a new class Jnitext.java click Build--make Project

Select Project Click F4 Key SDK location in Android NDK location Select Configure NDK path
No downloads available to download here: http://wear.techbrood.com/tools/sdk/ndk/


On the safe side. Click to view Local.properties file
ndk.dir=d\:\\ndk\\android-ndk-r10e
Sdk.dir=c\:\\users\\administrator\\appdata\\local\\android\\sdk
Ok so good next


View E:\text\YipJniDemo\app\build\intermediates\classes\debug in the project directory generates a class file
Next click on the Terminal window CD below to the current project directory
E:\TEXT\YIPJNIDEMO>CD E:\text\YipJniDemo\app\build\intermediates\classes\debug


using Javah to generate header files

E:\text\yipjnidemo\app\build\intermediates\classes\debug>javah-jni Com.yip.yipjnidemo.JniText


In E:\text\YipJniDemo\app\build\intermediates\classes\debug
You can see the generated com_yip_yipjnidemo_jnitext.h

1 /*Do not EDIT this file-it are machine generated*/2#include <jni.h>3 /*Header for Class Com_yip_yipjnidemo_jnitext*/4 5 #ifndef _included_com_yip_yipjnidemo_jnitext6 #define_included_com_yip_yipjnidemo_jnitext7 #ifdef __cplusplus8 extern "C" {9 #endifTen /* One * Class:com_yip_yipjnidemo_jnitext A * method:getclangstring - * Signature: () ljava/lang/string; -  */ the jniexport jstring jnicall java_com_yip_yipjnidemo_jnitext_getclangstring -(JNIENV *, jobject); -  - #ifdef __cplusplus + } - #endif + #endif


Create Jni folder under Project src file Note (right-click Main-new-folder-jnifolder)
cut the resulting file com_yip_yipjnidemo_jnitext.h into the Jni folder



Create a com_yip_yipjnidemo_jnitext.c file under the JNI directory

1#include"com_yip_yipjnidemo_jnitext.h"2 /*3 * Class:io_github_yanbober_ndkapplication_ndkjniutils4 * method:getclanguagestring5 * Signature: () ljava/lang/string;6  */7Jniexport jstring jnicall java_com_yip_yipjnidemo_jnitext_getclangstring (jnienv *env, Jobject obj)8 {9   return(*env)->newstringutf (env,"This just a test for Android Studio NDK JNI developer!");Ten}


Next, in the app's Build.gradle;

1 Add under Defaultconfig 2     ndk{3         modulename "Jnitext"         // generated so name 4             Abifilters "Armeabi", "armeabi-v7a", "x86"  // outputs specify so libraries under three ABI architectures. Currently dispensable.  5     }



Click Make-project
Build. So library
The Error was reported at this time: (0) ERROR:NDK integration is deprecated in the current plugin.  Consider trying the new experimental plugin.  For details, see Http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "Android.usedeprecatedndk=true" in gradle.properties to continue using the current NDK integration.
<a href= "Openfile:e:\text\myjnidemo\app\build.gradle" >open file</a>
Never mind
we add properties to the Gradle.properties under the project

  Android.usedeprecatedndk=true  

this is complete. Code for Yipjnidemo under Mainactivity
1  PackageCom.yip.yipjnidemo;2 3 Importandroid.app.Activity;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.widget.TextView;7 8  Public classMainactivityextendsActivity {9 Ten  One     PrivateTextView Txtview; A  - @Override -     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -txtview=(TextView) Findviewbyid (r.id.txt_jnitext); -Jnitext jnitext=NewJnitext (); + Txtview.settext (jnitext.getclangstring ()); -     } +}


Jnitext:
1  PackageCom.yip.yipjnidemo;2 3 /**4 * Created by Administrator on 2016/2/22.5  */6  Public classJnitext {7     Static {8System.loadlibrary ("Jnitext");//DefaultConfig.ndk.moduleName9         }Ten      Public nativeString getclangstring (); One}



PS: generated. So file some pots of friends can not find. In fact, it's here.







"Android Development"-a simple example of NDK Jni development under Android Studio

Related Article

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.