Android Jni 工程简单配置过程 (前提是环境搭建好 )

过程:

1. 写 java文件(你需要的类 或者函数)
生成. h 文件

首先写完ImageProc.java 这个保存 以后 eclipse会自动帮你生成 java 的class文件

   你可以再工程目录--》 bin --》classes -》com -------。。。。。 的文件里找到

   比如我的 在E:\code\TestJni\bin\classes\com\test2\test2 这个路径下
然后cd 到classes这个目录下 
然后 javah com.test2.test2.ImageProc 回车 即可生成 .h文件 可以改名字 
然后 将此.h文件copy到工程jni中 
2. 下一步开始编写.c 或者.cpp文件(不要忘记添加头文件)  需要学习一下编写规则等等
 
注意:
(return  (*env)-> NewStringUTF (env,  "Hello JTC from JNI !"  );) 这条语句貌似只能在.c中不报错
然后主程序中加载.so库文件时 不要添加 //import android.provider.Settings.System; 
否则 System.loadLibrary("com_example_testhellojni_TestJni");会报错

3. 生成.so库文件

windows环境下 

 build command 为 ndk-build.cmd (前提是设置好了Path)  
其他的几个配置说明:
Tool Chain Editor 默认就可以吧 (不太懂 )




加入库文件 我的文件路径为:

然后运行程序会自动生成.so 文件 然后android程序运行

也可以通过cygwin生成.so文件:
如图:

我的一个jni工程 太烂了 :
工程目录:

详细内容:
MainActivity.java:
package com.example.testhellojni;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
//import android.provider.Settings.System;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.example.testhellojni.TestJni;

public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout. activity_main);
              
               //System.loadLibrary("com_example_testhellojni_TestJni");
              Log.d("Hi", "Android call JNI: " + TestJni.outputJNI() );
       }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
               // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu. main, menu);
               return true ;
       }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
               // Handle action bar item clicks here. The action bar will
               // automatically handle clicks on the Home/Up button, so long
               // as you specify a parent activity in AndroidManifest.xml.
               int id = item.getItemId();
               if (id == R.id.action_settings) {
                      return true ;
              }
               return super .onOptionsItemSelected(item);
       }
       
        static {
        System. loadLibrary("com_example_testhellojni_TestJni");
    }
}

TestJni.java:
package com.example.testhellojni;  //自动生成

public class TestJni {
        native static String outputJNI();
}

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

#ifndef _Included_com_example_testhellojni_TestJni
#define _Included_com_example_testhellojni_TestJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_example_testhellojni_TestJni
* Method:    outputJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_testhellojni_TestJni_outputJNI
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

hello.c:
/*
* hello.c
*
*  Created on: 2014-11-25
*      Author: jiangtc
*/
#include <string.h>
#include <jni.h>
#include <com_example_testhellojni_TestJni.h>

JNIEXPORT jstring  Java_com_example_testhellojni_TestJni_outputJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello JTC from JNI !");
}

Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    : = com_example_testhellojni_TestJni
LOCAL_SRC_FILES := hello.c
include $(BUILD_SHARED_LIBRARY)



Application:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值