如标题所述 - 如何将本地代码添加到现有的Android Studio项目中,而不会破坏当前项目,包括gradle和proguard设置?
如标题所述 - 如何将本地代码添加到现有的Android Studio项目中,而不会破坏当前项目,包括gradle和proguard设置?
自Android Studio 3.1起,可以采取以下简单方式:
1. 在app\src\main
文件夹中创建cpp
文件夹。
2. 在app\src\main\cpp
路径下创建<YOUR_FILE_NAME>.cpp
文件(例如:native-lib.cpp)。
3. 向app
文件夹添加CMakeLists.txt
文件。
在该文件中应定义库的名称、.cpp
文件路径和其他一些设置,例如(从支持C++的新项目开始):
# 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(Module app)中添加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(Module app) 文件中的 defaultConfig 部分添加 externalNativeBuild
标签和其中的 cmake
标签:
...
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
}
...
(新的、空的带有 C++ 支持的 Android Studio 项目中也提供了“基本”build.gradle
文件的示例)
6. 与 Gradle 文件重新同步项目
通过在工具栏中点击 Sync Project ,请注意!在 Android Studio 3.3 中,图标是
。
此外,请查看官方教程。
附注:如果 cpp
文件夹中没有显示文件:
尝试像Thinh Vu在他的评论中提到的那样,选择File/Invalidate Caches & Restart
。
请按照以下步骤从您的现有项目开始:
apply plugin: 'com.android.model.application'
model {
android.signingConfigs {
create ("myConfig") {
keyAlias '--your-key-alias--'
keyPassword '--key-password--'
storeFile file('--/path/to/keystore.jks--')
storePassword '--store-password--'
}
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "--your.app.name--"
minSdkVersion.apiLevel 19
targetSdkVersion.apiLevel 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles.add(file('proguard-android-optimize.txt'))
proguardFiles.add(file('proguard-rules.pro'))
signingConfig = $("android.signingConfigs.myConfig")
}
}
ndk {
moduleName "--c-file--"
ldLibs.addAll(["android", "log"])
}
}
android.dexOptions {
javaMaxHeapSize "2048m"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
}
您可以复制/粘贴上面的代码,并修改至少带有“--value--”的值以匹配您的值。
在其中类似以下内容的位置:
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
转换为:
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.9.3'
}
在我的示例中,0.9.3是gradle-experimental的最新版本,可以在此处找到。如果您还没有这样做,请将gradle版本更改为Android Studio推荐的版本。
将proguard-android-optimize.txt
移动到app/proguard-android-optimize.txt
像这样
static {
System.loadLibrary("--c-file--");
}
private native byte my_jni(Context context, byte[] mByte, int i);
根据您的需求进行更改。上面的示例加载C文件(去除扩展名),该文件在gradle文件中声明,并调用my_jni函数,传递应用程序的Context、一些字节数组和一些整数,期望该函数返回一个字节。
现在您的函数名称被突出显示为红色 - 允许Android Studio创建它,方法是点击行上的红色灯泡 Create function ...
。这将在您的c文件中创建函数并将焦点改为该函数。
进一步阅读请点击 此处。
技巧:
小心释放每个malloc的内存,对于每个GetByteArrayElements使用ReleaseByteArrayElements等等
小心如何正确地将一些“危险”的值从C返回到Java,例如数组和字符串
CMakeList.txt
。 - Andrii Omelchenko