Add native code to Android Studio project.
I felt a bit lost when switching from eclipse to the Android Studio environment. Also I found it difficult to interpret the documentations and tutorials I found online that tried to explain how to setup android studio projects. After struggling for a while I decided to learn to compile a project with the terminal first to get a grip around what was happening in the background, and then the pieces started to fall in place.
First of I added the gradle and the ndk-build to the PATH variable. The path variable is a environment variable (both in Linux and Windows) that tells the environment which programs can be executed. Here is a bit discussion that I found about it. You fix it by adding some lines in your ~/.bashrc file. This makes it possible to be able to run grandle without specifying the path on every call. I did the same for ndk-build that is located in the native sdk folder.
Create a project
If you do not already have a project select File → New → New Project... and create a project that suits for you. Alternatively you can import an eclipse project by selecting File → New → Import project... Some things will differ, as I will mention in the text.
Add a jni folder to your project.
In your project explorer (probably on your left) right-click on "app" then go to New → Folder → New JNI Folder ... Select it and create a folder. Note that if you are in Android-mode (the text "android" is visible above the explorer) it might work differently, but then just press the drop-down menu (with the text android) and select "project".
Include library files into your project
Android studio will automatically call ndk-build in the jni-folder, so what you will have to do is to simply include your library into your code. The fastest way I found is to simply add this to your app/build.gradle
build.gradle
android {
... //old code
sourceSets.main {
jni.srcDirs = [] //This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/libs' //this is not necessary unless you have precompiled libraries in your project
}
}
You could do a bit more fancy stuff to match to different types of processors, but that will do for now.
If you have imported from an eclipse project you probably have a part in your gradle script that says "ndk {... }". I use to remove this statement because otherwise grandle will complain about duplicate libraries. Another alternative is to remove the jniLibs.srcDir statement above, it seems to have a similar effect.
I have also noticed that when importing from an eclipse project, Android Studio puts the old files in app/jniLib/... This caused an error message for me that confused me at first, but the solution was to simply remove the old copied so-files (the ones in app/jniLib) because new ones will be generated anyway.
Create some source files
Because the ndk-build
Lazy way to discover function names
You need to come up with a name that reflects the native functions. However if you are a bit lazy, like me you could just run the program, without implementing the right functions and then look for an error message like this in logcat.
java.lang.UnsatisfiedLinkError: No implementation found for void se.laserskold.mattias.android1.MainActivity.test() (tried Java_se_laserskold_mattias_android1_MainActivity_test and Java_se_laserskold_mattias_android1_MainActivity_test__)
then you know what to call your function.
Here are some more good resources http://ph0b.com/android-studio-gradle-and-ndk-integration/
Andra inlägg från bloggen
2024
Använda cpp som ett skriptspråk (2024-12-03)
Mekaniskt tangentbord Lasersköld L1 del 1 (2024-11-14)
Alphasmart neo 2 font editor (2024-10-11)
Bloggen är äntligen fyttad (2024-09-24)
2022
Figurer i skuggan (2022-06-04)
2018
prestandatest pa morgonen c (2018-11-21)
2017
Framsteg och frustration (2017-08-31)
2015
fix audacity startup bug on ubuntu 1510 (2015-11-24)
git subtree (2015-06-21)
add native code to android studio (2015-06-21)
2014
Ingenting (2014-11-08)
Äldre inlägg...