Android NDK初步涉猎

本文详细介绍了如何在Android应用中集成JNI,包括创建Java接口、生成并实现C/C++头部文件、构建.so库及运行验证。

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

大致分是这几步:

(1)新建Android工程:

                                                                                                          

HelloJni.java代码

package com.panpass.main;

public class HelloJni {
	
	static{
		System.loadLibrary("demo-jni");
	}
	
	
	public void sayHello(){}
	public native String jniSay();

}

MainActivity.java代码

package com.panpass.main;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.demo.R;

public class MainActivity extends Activity {
	private TextView mTextView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		mTextView = (TextView) findViewById(R.id.text_view);
		HelloJni demojin = new HelloJni();
		mTextView.setText(demojin.jniSay());
		
	}

}
(2)生成头文件。

在cmd中进入相应的目录下:

例如我的工程在这里:



最终是进入到这里:


也就是打开cmd后输入命令:

cd  C:\android\workone\Demo\bin\classes


接着输入:javah  -jni com.panpass.main.HelloJni


就会生成.h文件。


(3)新建jni文件。

                                                                  

demo-jni.c就是c层函数的具体实现。如下:

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "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 is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
 */
jstring
Java_com_panpass_main_HelloJni_jniSay( JNIEnv* env,
                                                  jobject thiz)
{


    return (*env)->NewStringUTF(env, "hello jni");
}


其中的

Java_com_panpass_main_HelloJni_jniSay( JNIEnv* env,
                                                  jobject thiz)
就是从生成的.h文件中来的。看生成的.h文件:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_panpass_main_HelloJni */

#ifndef _Included_com_panpass_main_HelloJni
#define _Included_com_panpass_main_HelloJni
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_panpass_main_HelloJni
 * Method:    haveReturnFromJni
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_panpass_main_HelloJni_haveReturnFromJni
  (JNIEnv *, jobject, jstring);

/*
 * Class:     com_panpass_main_HelloJni
 * Method:    jniSay
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_panpass_main_HelloJni_jniSay
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Android.mk代码如下:

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "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.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := demo-jni
LOCAL_SRC_FILES := demo-jni.c

include $(BUILD_SHARED_LIBRARY)
具体意思可以baidu。

(4)下载ndk使用ndk生成.so库。

ndk下载去百度。

配置ndk环境变量:



其中NDK:



在cmd中进入工程根目录:



接着执行:

ndk-build


刷新工程你就会发现:


多了libs和obj

最后运行,OK。







                                                                                                             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值