gpt4 book ai didi

android - 如何将 JNI(C/C++ 原生代码)添加到现有的 Android Studio 项目

转载 作者:IT老高 更新时间:2023-10-28 23:40:46 31 4
gpt4 key购买 nike

如标题所说——如何在不破坏当前项目的情况下,将原生代码添加到现有的 Android Studio 项目中,包括 gradle 和 proguard 设置?

最佳答案

从 Android Studio 3.1 开始,它可能是简单的方法:

1.创建 cpp app\src\main 内的文件夹.

<强>2。创建 <YOUR_FILE_NAME>.cpp文件在 app\src\main\cpp路径(例如 native-lib.cpp)

3.添加 CMakeLists.txt文件至app文件夹。

在该库的文件名中,.cpp应该定义文件路径和一些其他设置,例如(来自支持 C++ 的新的空 Android Studio 项目):

# 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.
native-lib

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
^^^^^^^^^^^^^^
YOUR_CPP_FILE_NAME

# 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.
native-lib

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

4.添加到 build.gradle(模块应用程序)externalNativeBuild引用 CMakeLists.txt 的标签进入 android部分:

android {
compileSdkVersion 26
defaultConfig {
...
}
buildTypes {
...
}
externalNativeBuild { <--- these lines should be added
cmake { <--- these lines should be added
path "CMakeLists.txt" <--- these lines should be added
} <--- these lines should be added
} <--- these lines should be added
}

5.添加到 build.gradle(模块应用程序)externalNativeBuild带有 cmake 的标签标记到 defaultConfig部分:

...
defaultConfig {
applicationId "<YOUR_APP_ID>"
minSdkVersion 26
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


externalNativeBuild { <--- these lines should be added
cmake { <--- these lines should be added
cppFlags "" <--- these lines should be added
} <--- these lines should be added
} <--- these lines should be added
}
...

(“基本”build.gradle 文件的示例也可用于支持 C++ 的新的空 Android Studio 项目)

6.使用 Gradle 文件重新同步项目

点击同步项目Sync Project button on Toolbar在工具栏中。注意!在 Android Studio 3.3 中,图标为  Android Studio 3.3 Sync Project button on Toolbar .

另外,看看 Official Tutorial .

附言。如果文件未在 cpp 中显示文件夹:

试试 File/Invalidate Caches & RestartThinh Vu在他的评论中提到。

关于android - 如何将 JNI(C/C++ 原生代码)添加到现有的 Android Studio 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45118175/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com