Android Studio and NDK Integration on Windows Step by Step with a Hello JNI example

本文详细介绍了如何在Android应用中使用JNI技术调用本地C/C++代码,包括下载安装NDK、创建HelloWorld应用、添加本地方法、生成及实现JNI接口等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Please refer to https://www.youtube.com/watch?v=0fEtrekNcOo. 


First, download NDK and install it. Then, launch Android to create a Hello World app. Then,


1) Open the Activity class, then add following code below inside this class.
public native String HelloJNI();


    static
    {
        System.loadLibrary("HelloJNI");
    }


2) Then, within Android Studio, launch Android Terminal,
cd C:\PublicSourceCode\AndroidStudioSamples\HelloJNI\app\src\main
javah -d jni -classpath C:\Users\Qingxu_Li\AppData\Local\Android\sdk\android-sdk\platforms\android-21\android.jar;C:\Users\Qingxu_Li\AppData\Local\Android\sdk\extras\android\support\v7\appcompat\libs\android-support-v4.jar;C:\Users\Qingxu_Li\AppData\Local\Android\sdk\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar;..\..\build\intermediates\classes\debug com.ebookfrenzy.hellojni.HelloJNIActivity


3) Now, under HelloJNI\app\src\main\jni, you can see a com_ebookfrenzy_hellojni_HelloJNIActivity.h file generated by the javah command above.


4) Create a HelloJNI.c file under HelloJNI\app\src\main\jni. The file content is below:


#include "com_ebookfrenzy_hellojni_HelloJNIActivity.h"


JNIEXPORT jstring JNICALL Java_com_ebookfrenzy_hellojni_HelloJNIActivity_HelloJNI
  (JNIEnv *env, jobject obj)
  {
      return (*env)->NewStringUTF(env, "Hello from JNI !");
  }


5) Change your activity's onCreate(...) method as below.
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( HelloJNI() );
        setContentView(tv);
    }

6) In your gradle\local.properties file, add following line. This is where your NDK is installed.
ndk.dir=C\:\\Users\\Qingxu_Li\\AppData\\Local\\Android\\android-ndk-r10d


7) In your app\build.gradle file, add the ndk section below.
    defaultConfig {
        applicationId "com.ebookfrenzy.hellojni"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"


        ndk {
            moduleName "HelloJNI"
        }
}
8) Under HelloJNI\app\src\main\jni, create a new file Android.mk file as below.
LOCAL_PATH := $(call my-dir)


include $(CLEAR_VARS)


LOCAL_MODULE    := HelloJNI
LOCAL_SRC_FILES := HelloJNI.c


include $(BUILD_SHARED_LIBRARY)


8) Under HelloJNI\app\src\main\jni, create a new file Application.mk file as below.
APP_ABI := all


9) In app\build.gradle file, add the sourceSets.main as below. This section should be under the "android" node.


sourceSets.main
{
    jni.srcDirs = [] // ndk-build.cmd needs to be invoked from command line
jniLibs.srcDir "src/main/libs"
}


10) With Android Studio, go to Android Terminal.
cd C:\PublicSourceCode\AndroidStudioSamples\HelloJNI\app\src\main
ndk-build


11) Then, run it!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值