IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> NDK 开发流程 -> 正文阅读

[移动开发]NDK 开发流程

NDK 开发流程


25.JNI_Android Studio下的 NDK 环境配置

26?JNI_快速开发

参考文档: 1 ,https://developer.android.google.cn/ndk/guides

操作流程一


1 :参考上述视频或者文档,先安装了NDK和cmake

? ? 1.1??安装NDK

? ? ? ? ? ? ? ? 1.1.1 下载

? ? ? ? ? ? ? ? ?1.1.2? 解压缩

? ? ? ? ? ? ? ? 1.1.3? 将路径放入环境变量path里

? ? 1.2? 安装cmake

? ? ? ? ? ? ? ? 1.2.1 下载

? ? ? ? ? ? ? ? ?1.2.2? 解压缩

? ? ? ? ? ? ? ? 1.2.3? 将路径放入环境变量path里

2 :修改已有项目的配置,使之支持c++

? ?2.1 修改app/build.gradle文件。增加内容,具体内容见附文

? ? ?2.1? 在src/main目录下新建一个文件夹,命名为cpp;

? ? ?2.2? 在cpp文件夹下建文件,具体内容见附文;

? ? ?2.3? 在cpp文件夹下建文件native-lib.cpp,具体内容见附文;

? ? ;

?app/build.gradle文件

android { 
    defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}
src/main/cpp/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

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.
# 创建并命名一个库,第一个参数是名称,第二个参数是模式,第三个参数是源码位置。
# 可以定义多个库,CMake会自动构建他们
# Gradle会为apk一起自动打包共享的库

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             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.
# 搜索指定的预构建库并将路径存储为变量
# CMake默认情况下包含搜索路径下的系统库,我们只需要指定我们想添加的库在ndk库的里名称。CMake会在编译他之前验证他是否存在。

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.
#告知CMake需要去连接哪些目标库,我们可以链接多个库,例如你在这个脚本中定义的库、预编译的三方库还可以是系统库。
target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
src/main/cpp/native-lib.cpp

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

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

3 :java调用jni


关键代码在

1:System.loadLibrary("native-lib"); //加载类库

2:public native String stringFromJNI(); //声明方法

3:String ticks = stringFromJNI(); //调用

4:System.out.println("sayHello方法返回值为:"+ticks);? //打印返回结果,可选

package com.example.apkdecryptapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
    TextView tickView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String ticks = stringFromJNI();
        System.out.println("sayHello方法返回值为:"+ticks);
        setContentView(R.layout.activity_main);

        tickView = (TextView) findViewById(R.id.tickView);
        MainActivity.this.tickView.setText("sayHello方法返回值为:"+ticks);
    }


    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xxx!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        android:id="@+id/tickView"/>



</androidx.constraintlayout.widget.ConstraintLayout>

4 :最终效果图

?

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:19:40  更:2021-08-04 11:19:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 16:47:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码