Android TechFFmpegAndroid知识

Android Studio jni开发入门——看我就够了!

2017-03-30  本文已影响2305人  磊_lei

此贴记录一下Android Studio 进行jni开发的基础配置以及入门教程,想当初使用eclipse配置ndk环境都难,现在基本都向AS转型,特此给需要帮助的新手村的同学们少走弯路☺

一、环境配置

主要需要配置的就是NDK(Native Development Kit),现在Android studio很便利,可以一键下载:
file → setting → 按截图找到如下路径 → 选择NDK → 确定应用下载

NDK安装

二、jni hello world!

1. 新建一个项目

2.设置支持jni

android.useDeprecatedNdk=true
ndk.dir=NDK的路径
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.lilei.testjni"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


        ndk {
            moduleName "JNISample"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
    testCompile 'junit:junit:4.12'
}

3.新建java访问c层的接口类

package com.lilei.testjni;

/**
 * Created by lilei on 2017/3/29.
 */

public class JniUtils {

    public static native String getJniString();

}

4.生成头文件

cd app/build/intermediates/classes/debug/
javah com.lilei.testjni.JniUtils

运行成功之后打开app/build/intermediates/classes/debug/ 即可找到编译出的头文件"com_lilei_testjni_JniUtils.h",不难发现头文件名是有原报名+类名组成

生成头文件

5.创建jni开发的文件夹

#include "com_lilei_testjni_JniUtils.h"

JNIEXPORT jstring JNICALL Java_com_lilei_testjni_JniUtils_getJniString
        (JNIEnv *env, jclass) {
    // new 一个字符串,返回Hello World
    return env -> NewStringUTF("Hello World");
}

6.java层加载so

static {
        System.loadLibrary("JNISample");
    }
加载so

7.运行Run

package com.lilei.testjni;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i("Jni", JniUtils.getJniString());

    }
}

三、生成so文件

前文介绍如何运行C++程序,但是实际开发中大多是封装编译出so文件后进行开发,就类似java里面的jar包

1.配置NDK环境变量

2.新建mk文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := JNISample
LOCAL_SRC_FILES := com_lilei_testjni_JniUtils.cpp

include $(BUILD_SHARED_LIBRARY)
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions -std=c++0x
APP_ABI := armeabi-v7a
APP_PLATFORM := android-18

3.编译生成so

ndk-build

libs和obj里面都有so文件,两者的区别google给出的解释是:
As part of the build process, the files in the libs folder have been stripped of symbols and debugging information. So you'll want to keep two copies of each of your .so files: One from the libs folder to install on the Android device, and one from the obj folder to install for GDB to get symbols from.
也就是说,libs目录下生成的库是剥离了符号表与调试信息的,而obj下的库是带有调试信息的。

至此jni的开发入门已完成

四、jni常用类型和方法

上一篇下一篇

猜你喜欢

热点阅读