Android-NDK的配置和使用(ADT)

本文详细介绍使用 Android NDK 进行本地应用开发的过程,包括配置 ADT 和 SDK 环境、创建工程、生成及调用 JNI 方法等步骤,并提供了多平台编译和头文件自动生成的具体操作。

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

1,打开ADT,设置SDK路径,NDK路径
2,建立android工程 cn.githan.hellondk
3,右键点击工程包名—>android tools —>add native support—>输入hello,自动生成jni文件夹
4,建立类GetString,定义native无需实现的方法;
5,进入工程bin目录,输入以下命令生成头文件:
javah -classpath classes/ -jni cn.githan.hellondk.GetString
6,将头文件放入jni文件夹内
7,在hello.cpp内引入头文件 cn_githan_hellondk_GetString.h
8,实现头文件中的方法
9,在MainActivity中调用类的方法

例子:

package cn.githan.hellondk;
public class newTest {
public native static String GetString();
public static native String aString();
public native int sum(int a, int b);
static{
    System.loadLibrary("helloNDK");
}
}

多平台编译:
1,jni下建立Application.mk
2,输入
APP_ABI := x86 armeabi
3,command+B重新构建工程
4,查看libs中的文件夹会出现不同cpu架构中的so文件

编译多个源文件:
1,建立一个新的C++源文件
2,Android.mk中添加对源文件的编译:
LOCAL_SRC_FILES := CPPShowLog.cpp \
hello.cpp

ANT自动创建头文件:
1,添加ant控制台
2,新建build_header.xml
3,alt+/ 创建模版
4,

<project name="test" default="BuildAllHeaders”>  ->default对应下面的target
    <description>
            description
    </description>

    <!-- =================================

          target: BuildAllHeaders
         ================================= -->
    <target name="BuildAllHeaders”> —>创建了一个集合target,使用antcall调用了另外两个target
        <antcall target="buildShowlogHeader"></antcall>
     <antcall target="buildGetStringHeader"></antcall>
    </target>

    <!-- - - - - - - - - - - - - - - - - -

          target: depends
         - - - - - - - - - - - - - - - - - -->
    <target name="buildShowlogHeader">
    <javah destdir="./jni" classpath="./bin/classes/" class="cn.githan.showcpplog.ShowLog"></javah> —>具体使用javah命令建立头文件
    </target>
     <target name="buildGetStringHeader">
     <javah destdir="./jni" classpath="./bin/classes/"                    class="cn.githan.showcpplog.GetString"></javah>
     </target>

</project>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

githan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值