0
点赞
收藏
分享

微信扫一扫

android native方法调用

水墨_青花 2023-02-18 阅读 44


adb devices连接设备

 手机上找到系统版本,触摸5下打开开发者选项,打开usb调试,手机上确认连接fingerprint

android native方法调用_android

 

android native方法调用_CMake_02

安装ndk support插件

android native方法调用_ide_03

android native方法调用_ide_04

 * MainActivity.java

package com.example.mysdkdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("mysdkdemo");
}
public native String getStringFromNative();

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

@Override
public boolean onTouchEvent(MotionEvent event) {
boolean shouldClose = super.onTouchEvent(event);
System.out.println(this.getStringFromNative());
return shouldClose;
}
}

android native方法调用_ide_05

 *mysdkdemo.cpp

#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_mysdkdemo_MainActivity_getStringFromNative(JNIEnv *env, jobject thiz) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}

android native方法调用_ide_06

运行,手机上触摸屏幕

android native方法调用_ide_07

* CMakeList.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.22.1)

# Declares and names the project.

project("mysdkdemo")

# 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.
mysdkdemo

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
mysdkdemo.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.
mysdkdemo

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

举报

相关推荐

0 条评论