参考:http://segmentfault.com/a/1190000000735743

http://ph0b.com/android-studio-gradle-and-ndk-integration/


############################################################


新建工程NDKTest4,新建活动MainActivity


新建MyNDK.java:

package com.zj.ndktest4;/** * Created by root on 15-11-26. */public class MyNDK {    static {        System.loadLibrary("hello-jni-opencv");    }    public static native String hello();}

修改activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context="com.zj.ndktest4.MainActivity">    <TextView        android:id="@+id/text_view"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></RelativeLayout>

修改MainActivity.java:

package com.zj.ndktest4;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {    private TextView mTextView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mTextView = (TextView)findViewById(R.id.text_view);        mTextView.setText(MyNDK.hello());    }}

修改local.properties,加入:

ndk.dir=$  //加入ndk地址

修改gradle.properties,加入:
android.useDeprecatedNdk=truendkDir=$ //加入ndk地址

修改app/build.gradle,加入:

    //add begin    sourceSets.main.jni.srcDirs = []    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {        commandLine "/opt/android-ndk-r10e/ndk-build",                'NDK_PROJECT_PATH=build/intermediates/ndk',                'NDK_LIBS_OUT=src/main/jniLibs',                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',                'NDK_APPLICATION_MK=src/main/jni/Application.mk'    }    tasks.withType(JavaCompile) {        compileTask -> compileTask.dependsOn ndkBuild    }    //add end

点击MyNDK,右键->External Tools->javah,生成app/src/main/jni/com_zj_ndktest4_MyNDK.h:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_zj_ndktest4_MyNDK */#ifndef _Included_com_zj_ndktest4_MyNDK#define _Included_com_zj_ndktest4_MyNDK#ifdef __cplusplusextern "C" {#endif/* * Class:     com_zj_ndktest4_MyNDK * Method:    hello * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_zj_ndktest4_MyNDK_hello  (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif

新建jni/main.cpp:

#include <jni.h>#include <opencv2/opencv.hpp>#include "com_zj_ndktest4_MyNDK.h"JNIEXPORT jstring JNICALL Java_com_zj_ndktest4_MyNDK_hello        (JNIEnv *env, jclass cla) {    return env->NewStringUTF("hello jni");}

新建jni/Android.mk:

LOCAL_PATH := ${call my-dir}include $(CLEAR_VARS)#opencvOPENCVROOT := /opt/opencv-sdkOPENCV_CAMERA_MODULE := onOPENCV_INSTALL_MODULES := onOPENCV_LIB_TYPE := SHAREADinclude ${OPENCVROOT}/sdk/native/jni/OpenCV.mkLOCAL_SRC_FILES := main.cppLOCAL_LDLIBS := -llogLOCAL_MODULE := hello-jni-opencvinclude $(BUILD_SHARED_LIBRARY)

新建jni/Application.mk:

APP_ABI := armeabiAPP_PLATFORM := android-21APP_STL := stlport_shared

运行:



###########################################################


从文章Android Studio,gradle and NDK integration中知道

在Elipse下,当你想要在Android工程中使用C/C++源码时,你必须配置Android.mk文件,而现在,在Android Studio下,默认不使用Android.mk文件,你可以在app/build.gradle中配置如下:

android {   ...   defaultConfig {        ndk {            moduleName "hello-jni"        }   }   ...}
上述语句表示使用一个ndk模块"hello-jni"

而且你还可以在ndk下设置其他属性:

1.cFlags

2.ldLibs

3.stl (ie. stlport_shared, gnustl_shared)

4.abiFilters (ie. "x86", "armeabi-v7a")

android {   ...   defaultConfig {        ndk {            moduleName "hello-jni"            stl := stlport_shared     //配置C++            ldLibs := "log"        //NDK可以进行log输出        }   }   ...}


还可以通过设置android.buildTypes.debug.jniDebuggable := true使得NDK程序可以进行调试(没有试过)


这对于简单的NDK使用来说,非常方便。

不过对于比较复杂的NDK使用来说,就有一些不变,比如加入opencv功能。可以通过加入下述语句来禁止默认的ndk实现,从而转向之前的Android.mk方式

在app/build.gradle下加入:

android {      ...      sourceSets.main.jni.srcDirs = [] //disable automatic ndk-build call                  task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {        commandLine "/opt/android-ndk-r10e/ndk-build",                'NDK_PROJECT_PATH=build/intermediates/ndk',                'NDK_LIBS_OUT=src/main/jniLibs',                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',                'NDK_APPLICATION_MK=src/main/jni/Application.mk'    }    tasks.withType(JavaCompile) {        compileTask -> compileTask.dependsOn ndkBuild    }}

更多相关文章

  1. Android(安卓)ndk 入门4 - C++实现
  2. 调整 FMX Android(安卓)文字显示「锯齿」效果
  3. android permission Suggestion: add 'tools:replace="android:v
  4. android,进入页面textview默认获得焦点问题,如何取消焦点
  5. Android(安卓)全屏设置
  6. Android(安卓)源码热门改动速查(持续更新.....)
  7. Android(安卓)软键盘遮挡Dialog
  8. Android(安卓)Studio 小提示,新建Activity
  9. android studio 新建项目 界面一直停在 【“building ‘ 项目名

随机推荐

  1. Android图片浏览器之缩略图
  2. Android扩展:一个自动findViewById的小工
  3. (原创)Android入门教程(三十六)------实
  4. Android(安卓)RecyclerView之代替ListVie
  5. android中Bitmap数据如何释放
  6. android 如何使用LaunchMode
  7. Android(安卓)使用SQLiteDatabase操作SQL
  8. android多线程handler+runOnUithread+vie
  9. Android的应用组件
  10. Android(安卓)EditText限制输入两位小数