首页 > 技术文章 > android studio 1.0 开发 ndk 调用 c++ so库

SamFang 2014-12-23 09:35 原文

一个没用过java和安卓的人使用android studio开发带c++ so库的安卓程序用例(以ndk的hello-jni为例),对于不熟悉java和安卓的人来说这个很花时间,希望通过这篇文章帮助跟我一样的人,欢迎随便转载:

1.下载安装android sdk和ndk,ndk r10(目前最新)是单独可以编译c++的,无需cygwin。

 
2.安装android studio。
 
3.通过ndk-build命令编译sample中的hello-jni,生成so库。
 
4.在android studio新建项目,把生成的全部so文件连同处理器文件夹一同拷贝到项目的libs文件中。修改build.gradle文件,添加sourceSets,内容如下:
apply plugin: 'com.android.application'
android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.sample.hello"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

 

 
5.添加java文件hellojni文件(注意package):
基本拷贝的sample中的hellojni文件,代码如下(该代码略有不同,我给stringFromJNI传string参数,可自行参考ndk中sample/src中的hellojni文件):
/*
 * 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.
 */
package com.example.hellojni;
public class HelloJni
{
    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI(String msg);
    /* This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     *
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String  unimplementedStringFromJNI();
    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.hellojni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("hello-jni");
    }
}
 
6.项目中调用so库函数stringFromJNI,首先添加引用:
import com.example.hellojni.HelloJni;
再调用代码:
finalEditText editText =(EditText) findViewById(R.id.editText);
HelloJni hj =newHelloJni();
editText.setText(hj.stringFromJNI("this is java!"));

 

 
 
7.打包发布:
 
8.其他:
ndk默认是不支持c99(c++),如果需要c99,则修改Android.mk文件,添加LOCAL_CFLAGS := -std=c99

LOCAL_CFLAGS := -std=c99

推荐阅读