Android Studio 3.0 利用cmake搭建jni环境(很详细哦)

2019-04-01 22:59:22

原文链接  Android Studio 3.0 利用cmake搭建jni环境(很详细哦)

我用的Android Studio是3.0的版本,然后想搭建一下jni的环境。这里把自己遇到的问题和注意点都记录下。 

首先是需要在android studio里面安装最基本的环境。 

打开Default Preference里面查看SDK Tool选项。 


CMake、LLDB、NDK这三个勾选上去,然后安装。 

安装好了以后,在File-》Project Structure的SDK Location里面应该可以看到Android NDK Location的地址。 

如下图所示。 


新建一个Android Studio项目,注意到我这里新建项目的时候并没有什么include C++ 的勾选项目,as也不会在项目生成后帮忙生成cmakelist.txt, 我以为这个as版本的问题,后面也能够顺利搭建就行了。 


首先新建一个JNITest类,代码如下:


package jnifive.preqel.com.jnifive;

public class JNITest {


    static {

        System.loadLibrary("jnilib");

    }


    public native String getString();

}


这里的jnilib代表的就是你的jni库名。这时候编译器会提示有错误,不用管。 

然后根据javah工具来生成c语言头文件。 

首先配置好你的jdk环境变量,即在termianla终端里面可以正常使用javac和javah。 

到JNITest同级目录下面用javac 生成class文件。 

具体操作:用终端进入到JNITest.java所在目录,执行 javac JNITest.java 


然后在终端里面用命令行退到java文件目录下面,用javah生成c++头文件。 

终端输入:javah -jni jni.example.com.testjni6.JNITest 

注意:请用cd命令退到工程文件目录的java同级目录下面执行,否则会提示找不到文件 

成功后会生成 



然后 

右击app目录,新建Folder-》Jni Folder 建立jni目录 



点击下一步后看见android studio在工程下面帮我们生成了一个cpp的文件夹,并没有看到所谓的jni文件夹。晕了?没关系,其实是一样的,把Android studio的视图转化为Project,找到app目录,然后在main下面找到jni目录。如图所示 



然后把刚才的jni_example_com_testjni6_JNITest.h文件拷贝进来。 

在这个jni目录下面创建一个新的c/c++ source file。 

名字就叫main.cpp好了。里面代码如下:


#include "jni_example_com_testjni6_JNITest.h"

#include <string>

extern "C"

JNIEXPORT jstring JNICALL

JNICALL Java_jni_example_com_testjni6_JNITest_getString(

        JNIEnv* env,

        jobject /* this */) {

    std::string hello = "Hello from C++";

    return env->NewStringUTF(hello.c_str());

}


注意这里的包名都要对应上。 

这时候build一下工程你会发现 



这时候不用配什么android.useDeprecatedNdk=true ,这个东西在3.0以后好像是被取消了,配了只会走错路。(表示折腾了很久。。。) 

正确的做法是直接用cmakelist.txt配置环境。在工程app/java/目录(并非一定要这个地方,但是最后在build.gradle里面应正确关联)下面直接新建一个CMakeLists.txt,里面的内容如下:


# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html


# Sets the minimum version of CMake required to build the native library.


cmake_minimum_required(VERSION 3.4.1)


# Creates and names a library, sets it as either STATIC

# or SHARED, and provides the relative paths to its source code.

# You can define multiple libraries, and CMake builds them for you.

# Gradle automatically packages shared libraries with your APK.


add_library( # Sets the name of the library.

             jnilib


             # Sets the library as a shared library.

             SHARED


             # Provides a relative path to your source file(s).

             ../jni/native-lib.cpp )


# Searches for a specified prebuilt library and stores the path as a

# variable. Because CMake includes system libraries in the search path by

# default, you only need to specify the name of the public NDK library

# you want to add. CMake verifies that the library exists before

# completing its build.


find_library( # Sets the name of the path variable.

              log-lib


              # Specifies the name of the NDK library that

              # you want CMake to locate.

              log )


# Specifies libraries CMake should link to your target library. You

# can link multiple libraries, such as libraries you define in this

# build script, prebuilt third-party libraries, or system libraries.


target_link_libraries( # Specifies the target library.

                       jnilib


                       # Links the target library to the log library

                       # included in the NDK.

                       ${log-lib} )


带#号全是注释,所以这里有几个重要的属性要配的。 

add_library 下面首先指明库的名称,这里是jnilib,Provides a relative path to your source file(s),这里要用相对路径指到cpp文件所在的路径。cmakelist的配置,这个配置一定要配对,否则会抱什么 

CMake Error: CMake can not determine linker language for target: 的错误。


然后在app的build.gradle下面新增一些语句。 

在defaultConfig下面新增


   ndk {

            abiFilters 'armeabi', 'armeabi-v7a','x86'

        }


    externalNativeBuild {

            cmake {

                cppFlags ""

                //生成多个版本的so文件

                abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'

            }

        }


这里表示支持的abi版本,如果设备的abi版本不在这个列表中是安装不了apk的。 

然后在同一文件的android下面加入下面的代码


    externalNativeBuild {

        cmake {

            path "src/main/java/CMakeLists.txt"  // 设置所要编写的c源码位置,以及编译后so文件的名字

        }

    }



path 就是CMakeLists.txt的相对路径。 

好了。 

最后在MainActivity里面调用一下这个JNITest的getString()函数就行了,关键代码如下:


   @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TextView tv = (TextView) findViewById(R.id.textview);

        JNITest ndkTest = new JNITest();

        try {

            tv.setText(ndkTest.getString());

        }catch (Exception e){

            e.printStackTrace();

        }

    }


 

如有需要,请点击下面链接: 

项目github源码地址



  • 2021-02-04 14:08:13

    基于 Electron 的爬虫框架 Nightmare

    Electron 可以让你使用纯 JavaScript 调用 Chrome 丰富的原生的接口来创造桌面应用。你可以把它看作一个专注于桌面应用的 Node.js 的变体,而不是 Web 服务器。其基于浏览器的应用方式可以极方便的做各种响应式的交互,接下来介绍下关于 Electron 上衍生出的框架 Nightmare

  • 2021-02-04 20:13:02

    iOS framework制作及使用(包含xib和图片)

    静态库与动态库简介: 静态库:链接使用时完整地拷贝至可执行文件中 动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次 本文制作framework对应xcode版本:10.1

  • 2021-02-11 15:53:08

    node缓存框架memory-cache

    无论是在 desktop, mobile or web哪一方面,Cache都常被我们用来提升程序性能。当处理web应用程序的时候,虽然可以使用当前所有浏览器都支持的响应头来进行客户端缓存,从而提升页面加载效率。但当一个内容非常繁杂的页面需要2s来进行HTML输出的时候,即使启用客户端缓存该页面,服务器仍然需要针对每一个来访用户进行页面渲染。想想一个大型的新闻门户网站首页,难道他们要针对每一个用户一遍又一遍地处理HTML吗?

  • 2021-02-19 16:46:35

    window安装composer

    Composer 是 PHP 的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。

  • 2021-02-21 22:52:12

    php去除字符串中的HTML标签

    php自带的函数可以去除/删除字符串中的HTML标签/代码。 strip_tags(string,allow):函数剥去 HTML、XML 以及 PHP 的标签。 参数:string,必填,规定要检查的字符串;allow,选填,规定允许存在的标签,这些标签不会被删除。