android studio 3.0以上 jni集成过程注意事项

1.要生成头文件主要命令行

E:\project\android_jni_test>javah -jni -d E:\project\android_jni_test\app\src\main\jni -classpath "E:\project\android_jni_test\app\src\main\java" com.jni.test.MyJniUtils

-jni:   生成jni形式的头文件(默认值)

-d:      指定输出路径

-classpath:     指定类所在的根目录

com.test.ClassABC: 包名+类名

2.遇到找不到到类型,有可能是win10系统环境变量没有配置好 具体配置

CLASSPATH  D:\android_studio\jdk1.8\lib

JAVA_HOME  D:\android_studio\jdk1.8

Path 新建三个环境变量 D:\android_studio\jdk1.8   D:\android_studio\jdk1.8\jre\bin  D:\android_studio\jdk1.8\bin

主要步骤:

1.写一个工具类,用户调用jni的方法,myJniLib为生成的so库的名称

public class JniUtils
{
    static {
        System.loadLibrary("myJniLib");
    }
    public static native String getName();
}

2.将工具类生成头文件,然后放在jni文件夹下,命令参照上面

3.实现jni方法,

#include "com_lib_jni_JniUtils.h"

JNIEXPORT jstring JNICALL Java_com_lib_jni_JniUtils_getName
        (JNIEnv *env, jclass) {
    //如果是用C语言格式就用这种方式
    //    return (*env)->NewStringUTF(env,"Kiss dream");
    //如果是用C语言格式就用这种方式
    return env->NewStringUTF((char *) "test success");
}

4.build.gradle文件夹配置

android {
   
    defaultConfig {
        // 使用Cmake工具
        externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
            }
        }
    }
    // 配置CMakeLists.txt路径
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"   // 设置所要编写的c源码位置,以及编译后so文件的名字
        }
    }
    sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/'] } }
}

5.把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.
#CMakeLists.txt
#指定需要CMake的最小版本
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.
            # 设置so文件名称.
             myJniLib

             # Sets the library as a shared library.
             SHARED
             # 设置这个so文件为共享.

             # Provides a relative path to your source file(s).
             # 设置这个so文件为共享.
             src/main/jni/com_lib_jni_JniUtils.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.
                       # 制定目标库.
                       myJniLib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

6.clean project ,build完成!









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中进行JNI(Java Native Interface)开发,可以让我在Java代码中调用C/C++代码,实现跨语言的功能。下面是在Android Studio中进行JNI开发的一般步骤: 1. 创建一个新的Android项目或打开一个已有的项目。 2. 在项目的`app`模块下,创建一个名为`jni`的文件夹(如果没有的话)。 3. 在`jni`文件夹下,创建一个名为`native-lib.cpp`(或其他任意名称)的C/C++源文件,用于编写JNI代码。 4. 在`native-lib.cpp`文件中,编写你需要的C/C++代码。这些代码将会被Java代码调用。 5. 在`app`模块的`build.gradle`文件中,添加以下配置: ``` android { ... defaultConfig { ... externalNativeBuild { cmake { cppFlags "" } } } ... externalNativeBuild { cmake { path "CMakeLists.txt" } } } ``` 6. 在`app`模块下,创建一个名为`CMakeLists.txt`的文件,并添加以下内容: ``` cmake_minimum_required(VERSION 3.10.2) project("YourProjectName") add_library( native-lib SHARED native-lib.cpp ) find_library( log-lib log ) target_link_libraries( native-lib ${log-lib} ) ``` 7. 在Android Studio的底部工具栏中,点击"Build Variants"按钮,将"Active Build Variant"设置为`native-lib`。 8. 在Android Studio的顶部菜单中,点击"Build" -> "Make Project",编译项目。 9. 在Java代码中,通过`System.loadLibrary("native-lib")`加载C/C++库,并调用其中的函数。 这样,你就可以在Android Studio中进行JNI开发了。记得在编写JNI代码时,遵循JNI的规范和语法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值