«

android studio ndk编程 配置

时间:2024-3-2 17:25     作者:韩俊     分类: Android


参考:http://blog.csdn.net/ashqal/article/details/21869151
1.

模块的目录结构


jni的文件目录可以通过

java/Native.java

package com.example.administrator.mylibrary;

/**
 * Created by Administrator on 2015/5/13.
 */
public class Native {
    public static native String getStr();

    static {
        System.loadLibrary("mylibrary");
    }
}

通过javah生成头文件

javah -jni *

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

#ifndef _Included_com_example_administrator_mylibrary_Native
#define _Included_com_example_administrator_mylibrary_Native
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_administrator_mylibrary_Native
 * Method:    getStr
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_administrator_mylibrary_Native_getStr
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

实现的cpp文件

#include "com_example_administrator_mylibrary_Native.h"
JNIEXPORT jstring JNICALL Java_com_example_administrator_mylibrary_Native_getStr
  (JNIEnv * env, jobject obj){
    return env->NewStringUTF("native");
  }

jni/empty.c
是个空文件,不添加会有问题
http://stackoverflow.com/questions/29122903/ndk-is-not-configured-issue-in-android-studio

配置ndk路径


sdk.dir=d:UsersAdministratorAppDataLocalAndroidsdk
ndk.dir=F:adt-bundle-windows-x86-20140624android-ndk-r10d

主项目 引入子模块

运行

示例下载
http://pan.baidu.com/s/1o6EAIEm

延伸

1、手动配置生成的android.mk

子模块的build.gradle 在debug realease下添加ndk

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
        versionCode 1
        versionName "1.0"
    }
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        ndk {
            moduleName "jnimain"
            abiFilters "armeabi", "armeabi-v7a"
        }
    }
    debug {
        ndk {
            moduleName "jnimain"
            abiFilters "armeabi", "armeabi-v7a"
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
}

2.cpp代码没提示
不知如何解决

标签: android

热门推荐