zoukankan      html  css  js  c++  java
  • 安卓NDK流程

    • 定义wrap类,声明native函数,加载库
    package com.ndk.hello;
    
    public class Classs {
    
        public native String say_hello();
        static
        {
            System.loadLibrary("HelloAndroidNDK");
        }
    }
    • 在项目根目录创建jni文件夹,在此文件夹生成JNI头文件
    javah -classpath ../bin/classes com.ndk.hello.Classs
    • 为生成的com_ndk_hello_Classs.h写实现文件
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class com_ndk_hello_Classs */
    
    #ifndef _Included_com_ndk_hello_Classs
    #define _Included_com_ndk_hello_Classs
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     com_ndk_hello_Classs
     * Method:    say_hello
     * Signature: ()Ljava/lang/String;
     */
    JNIEXPORT jstring JNICALL Java_com_ndk_hello_Classs_say_1hello
      (JNIEnv *, jobject);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    #include "com_ndk_hello_Classs.h"
    
    JNIEXPORT jstring JNICALL Java_com_ndk_hello_Classs_say_1hello(JNIEnv * env, jobject this)
    {
        return (*env)->NewStringUTF(env,"Hello Java NDK!");
    }
    • 在jni文件夹写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    := HelloAndroidNDK
    LOCAL_SRC_FILES := com_ndk_hello_Classs.c
    
    include $(BUILD_SHARED_LIBRARY)
    • 在jni文件夹中交叉编译mk文件
    $NDK/ndk-build
    • 将生成libs/armeabi/libHelloAndroidNDK.so文件
    • 编写安卓框架程序,调用native方法。
    package com.ndk.hello;
    import com.ndk.hello.Classs;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class HelloAndroidNDK extends Activity{
        @Override
        public void onCreate(Bundle s)
        {
            super.onCreate(s);
            
            Classs c = new Classs();
            
            String say = c.say_hello();
            TextView tv = new TextView(this);  
            tv.setText(say);  
            setContentView(tv);  
        }
    }
  • 相关阅读:
    求子数组最大和
    <转>Android APP字体大小,不随系统的字体大小变化而变化的方法
    模块25 pageobject改造【进阶】
    第5阶段直播
    web测试-赠送课程
    模块四 -使用postman发送请求
    模块三 使用tcpdump抓包分析tcp三次握手四次挥手
    模块二 常见接口协议解析
    20200611_19元素操作
    20200609_18八大元素定位
  • 原文地址:https://www.cnblogs.com/wubugui/p/4270958.html
Copyright © 2011-2022 走看看