I have an Android project setup in Android Studio.
I'm using NDK and JNI so I have some cpp/h files in the jni folder. I can use them and everything is fine.
I also have another folder somewhere in my harddrive (which is not under the Android project root folder) with more cpp files.
So in Android.mk I added the path to both local (jni folder) files and the external files directory path. Everything compiles and everything works fine.
The issue I'm having is how to actually link to those cpp files in the external folder so I can actually edit them inside Android Studio or at least see the functions and member names. Currently I edit those files in notepad++ save and then go back to Android Studio to call the methods etc.
The external folder is simply a folder with some cpp files inside. It's not a module, it does not have a build.gradle or anything like that.
Is what I'm asking even possbile?
I hope the question was clear enough. Any help is appreciated!
Thanks
Related
I'm having trouble linking some files to my project. It's an android studio project and I want to link one external file: "Emv_lib.so" (I know that are some questions on how to link .so files, but those are about internal .so files, this is an external one.)
Beside this file, I have an emv_lib.lib and emvIIapi.h (the header file that actually contains the functions that i need to call). I cannot get this to work. I tried with CMakeList, putting the emvIIapi.h file in there, but it comes with the error: "Execution failed for task ':app:configureCMakeDebug'.
C/C++"
I follow some suggestion, to create in src/main the folder jniLibs and put your .so files in there, but it didn't work.
From my undestanding, I have to compile, somehow, either the .so files or the .lib file.
I'm new to android studio, so don't go hard on me. Thank you for your time!
I want to copy and execute some precompiled binaries on device, the problem is that the relative folder structure needs to be the same as in the original package (I cannot move all binaries to root folder).
What so far I found about this is how to copy binaries without .so extension to lib folder here. It works as expected, once installed I can locate the binary using context.getApplicationInfo().nativeLibraryDir, however when I place the directory with binaries it cannot find it using the command.
Further investigation shows that folder does get coppied to apk. I can confirm that by unziping the apk and finding the binaries coppied at /lib/arm64-v8a/.
Now since the apk is fine, the next step I did is check what actually gets copied to device. This can be found in /data/app/app.name.com+random_hash/lib/ and I confirmed that folder does not get copied over, only binaries from root folder do.
What I want to know is whether the code responsible for copying native libraries is located on device and cannot be changed, or the application can be somehow configured to copy the folder structure?
Example repo: https://github.com/D4no0/copy_native_binaries
The code is on device, part of the OS :
"Subdirectory is not supported by the Android OS.
When the APK is installed, the .so libraries are extracted to a directory in the form of /data/app/your.app/lib/. That directory is added to the library path searched by System.loadLibrary so that it can be found. System.loadLibrary do not support a directory structure, thus, the .so files in a subdirectory under lib/ are not extracted."
The above answer is coming from a Googler working on Android, from the following issue :
https://issuetracker.google.com/issues/63707864#comment4
Cheers,
Jérôme
I have an requirement which i have to build .so file using NDK build in Android and I have to call some of the methods from the .so file in my other Android application.
What I have tried so far.
I have taken the .so file from the lib folder of my Android project and put in my other application libs folder where I should call that .so file. I have used the below code:
Note: I am using ECLIPSE
static {
System.loadLibrary("NativeCode");
}
and also tried with full path of library
static {
System.loadLibrary("fullpath\NativeCode");
}
both the cases its giving unsatisfiedLinkError that mean it's not getting the path of my .so file.
You need to have your libNativeCode.so in the jni folder of your root, and not in the libs or lib folder
Here is a nice tutorial : http://www3.ntu.edu.sg/home/ehchua/programming/android/android_ndk.html
Alternatively, you can use following commands:
adb shell
$echo $PATH
What it will do is display a path like this :
/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin
You can have your .so in any of these folders like /system, /system/bin, /system/sbin. (Remember, but you would need to manually copy it, and it is not a good idea to do so for an app)
Edit :
You can not call any JNI function in your app, because the signature. You need to have a JNI wrapper (a .so JNI lib that in turn uses the .h header file to call the JNI function of the other library). This is because JNI is very sensitive to signatures. So, as I recommended earlier, follow the tutorial !
In order to get your .so library to be loaded using loadLibrary() you'll have to copy the libNativeCode.so lib into the jniLibs/architecture folder.
The folder should be located under src/main/jniLibs/armeabi-v7a of your Android Application module (assuming you're using Android Studio).
After you copy the libraries, add the following line to your build.gradle located at the app module folder:
assert file("./src/main/jniLibs/armeabi-v7a/libNativeCode.so").exists()
After you'll complete the above steps it should load the lib properly.
If the libNativeCode.so is located at the wrong path, the Gradle build will fail.
Good luck,
I am trying to add SQLCipher to my project. I am able to link the jar files to the project but there is problem linking the .so files provided.
Due to that I am getting UnSatisfiedLinkError when i try to open the DB.
Can anyone please let me know the best possible way to add .so files to the project and get it running.
In addition to jar files you need to include .so files. In my project I have:
jar files in project_root/libs/
.so files in project_root/libs/armeabi/
Also make sure that you have added the .jar files properly. Go to Project Properties -> Java Build Path -> Libraries tab make sure commonc-codec.jar, gueva-r09.jar, sqlcipher.jar are added there.
EDIT
1) Add a single sqlcipher.jar and a few .so’s to the application libs directory
2) Update the import path from android.database.sqlite.* to info.guardianproject.database.sqlite.* in any source files that reference it. The original android.database.Cursor can still be used unchanged.
3) Init the database in onCreate() and pass a variable argument to the open database method with a password*:
SQLiteDatabase.loadLibs(this); //first init the db libraries with the context
SQLiteOpenHelper.getWritableDatabase(“thisismysecret”):
Can you use the adb shell command to verify what files are being deployed to your simulator after it has been unpacked. I have seen an issue where the .so files are not packaged up in the .apk file before, which is due to the IDE not pointing to the correct native library path for your application.
In a class belonging to a Library project I call:
webview.loadUrl("file:///android_asset/info.html", null);
Unfortunately, this only works if I duplicate the file info.html into the Application's project asset folder as well.
Is there a way to tell an Android library code: "look for this file in the library's assets folder, not in the application's assets folder" ?
This answer is out of date, the gradle build system and AAR files support assets.
From the Android Docs:
Library projects cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.
If you want to include files from a Library project, you'll need to put it in the resources instead of the assets. If you're trying to load HTML files from your library project into a WebView, this means that you'll have to go a more roundabout method than the usual asset URL. Instead you'll have to read the resource data and use something like loadData.
This is now possible using the Gradle build system.
Testing with Android Studio 0.5.0 and v0.9 of the Android Gradle plugin, I've found that files such as
MyLibProject/src/main/assets/test.html
are correctly packaged in the final application and can be accessed at runtime via the expected URL:
file:///android_asset/test.html
You can achieve this by creating a symbolic link in the project's asset folder that points to the directory in the library project.
Then you can access as below:
webview.loadUrl("file:///android_asset/folder_in_a_libary_project/info.html", null);
Okay. Ive been stressing out and losing sleep about this for a while. Im the type of person that loves API creation, and HATES complicated integration.
There arent many solutions around on the internet, so im quite proud of what Ive discovered with a bit of Eclipse Hackery.
It turns out that when you put a file in the Android Lib's /assets folder. The target apk will capture this and place it on the root of the APK archive. Thus, making general access fail.
This can be resolved by simply creating a Raw Java Library, and placing all assets in there, ie (JAVALIB)/assets/fileX.txt.
You can in turn then include this as a Java Build Path Folder Source in
Project > Properties > Java Build Path > Source > Link Source.
Link Source
Click on Variables. and Add New Variable, ie VAR_NAME_X. location : ../../(relative_path_to_assets_project)
Click Ok
Now, when you build and run your app, the assets folder in the APK will contain your (GLOBAL Library) files as you intended.
No need to reconfigure android internals or nothing. Its all capable within a few clicks of Eclipse.
I confirm that Daniel Grant's approach works for at least the following situation: target project does NOT have an asset folder (or the folder is empty, so you can safely delete it).
I did not setup any variable.
Simply setup a LinkSource as follows (just an example)
Linked folder location: /home/matthew/workspace_moonblink/assetsForAdvocacy/assets
Folder name : assets
The "assetsForAdvocacy" is a Java project, (created with New- Project - Java Project) with empty src folder, and a new folder named "assets", which now provides the entire assets folder for the target project.
This is a fairly straightforward way within Eclipse to provide assets re-use across many different projects IF they do not already have assets, good enough to get going with. I would probably want to enhance it to become a content provider in the long run, but that is a lot more development.
My project accesses the assets with the following code:
String advocacyFolderInAssets = "no_smoking/"; //a folder underneath assets/
String fn =advocacyFolderInAssets+imageFilename;
Bitmap pristineBitmapForAdvocacy = getBitmapFromAsset(context, fn);
I use Motodev Studio 3.1.0 on Ubuntu. It would not let me 'merge' a new assets folder in the new assets-only project onto an existing assets folder in the target project.
If you want to use a setup where multiple derivate products are created from one library you might consider using svn:externals or similar solution in your SCM system. This will also do the trick that static assets like online help may be versioned seperately from the android source code.
I found this older question, it might help you, too.
This is the official way Google uses to archive this (from the above post): Link