Like the title says - how to add native code to existing Android Studio project, without breaking the current project, including gradle and proguard settings?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Since Android Studio 3.1 its possible easy way:
1. Create
cpp
folder insideapp\src\main
.2. Create
<YOUR_FILE_NAME>.cpp
file inapp\src\main\cpp
path (e.g. native-lib.cpp)3. Add
CMakeLists.txt
file toapp
folder.In that file name of the library,
.cpp
file path and some other settings should be defined, e.g. (from new, empty Android Studio Project with C++ support):4. Add to build.gradle (Module app)
externalNativeBuild
tag with reference toCMakeLists.txt
intoandroid
section:5. Add to build.gradle (Module app)
externalNativeBuild
tag withcmake
tag intodefaultConfig
section:(example of "basic"
build.gradle
file also available in new, empty Android Studio project with C++ support)6. Resync Project with Gradle files
By clicking Sync Project in the toolbar. NB! In Android Studio 3.3, the icon is .
Also, take a look at Official Tutorial.
PS. If files not shown in
cpp
folder:try
File/Invalidate Caches & Restart
as Thinh Vu mentioned in his comment.Follow this steps from your existing project:
1. Modify build.gradle (Module app) to look like this (a lot changes!):
You can copy/paste the above code and modify at least the values with "--value--" to match yours.
2. Modify build.gradle (Project)
where it says something like this:
to this:
The number in my example 0.9.3 is the latest version of gradle-experimental to be found here. Eventually change your gradle version in gradle-wrapper.properties to the version recommended by Android Studio if you did not already.
3. Move your proguard settings file
proguard-android-optimize.txt
toapp/proguard-android-optimize.txt
4. Add the call from java
like this
changing to your needs. The example above loads the c-file (write it without the extension) - the same one declared in the gradle file, and calls the function my_jni, passing the application's Context, some byte array and some int, expecting that the functions returns a byte.
5. Create the function in JNI:
Now the name of your function is highlighted in red - allow Android Studio to create it
Create function ...
with clicking on the red lamp on the row. This creates the function in your c file and changes focus to it.Done
Further reading here.
Tips:
Take care to
free
everything youmalloc
,ReleaseByteArrayElements
for everyGetByteArrayElements
and so onTake care how to properly return some dangerous values from C to Java, like arrays and Strings