Android Studio NDK Development

Source: Internet
Author: User

Clean up Eclipse's NDK development and then organize your Android studio.

A relatively good Baidu network disk: http://pan.baidu.com/share/home?uk=2383159761 often update the latest Android development package, you can download to the NDK package

Create an Android project, put a textview for displaying text, function is simple, get the string from the native layer and display it on the TextView

Then write the relevant code:

 Public classMainactivityextendsappcompatactivity{//Load so    Static{system.loadlibrary ("Myjni"); }    //declaring a local method     Public nativeString GetString (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView TextView=(TextView) Findviewbyid (R.ID.TEXTVIEWID);    Textview.settext (GetString ()); }}

1. Select the menu build-->build APK and use command line cmd to enter the Android project \app\src\main\java directory to generate the. h header File

Javah-d. /JNI package name. Main Activit Name

such as: javah-d. /jni Com.example.eclipsendk.MainActivit

2. In the Project Catalog column Jni folder right----C/c++source File

Fill in the file name, select the. C file (do not generate a header file), and if you use a. cpp file, remember the extern "C"

3. Write the native layer code, in MyJni.cpp

#include"Test_com_androidstudiondk_mainactivity.h"//Copy the function declaration from the file.jniexport jstring jnicall java_test_com_androidstudiondk_mainactivity_getstring (JNIEnv*env, Jobject obj) {    //. c files are used in this way    return(*env)->newstringutf (env,"Hello Android Studio NDK"); //. cpp files are used in this way//return Env->->newstringutf ("Hello Android Studio NDK");}

4. Then add the contents of several files

①. local.properties file, adding the path to the NDK

②. gradle.properties file declaration using the NDK

Android.usedeprecatedndk=true

③. The name of the Build.gradle file in the app directory.

You can toggle the view from here

④. After the changes are complete, compile the apk and put it into the emulator to run.

Debug version (Build APK) generated in: app\build\outputs\apk directory

Release version (Generate signed APK) generated in: App Directory

Android Studio NDK Development

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.