How to link ndk module using Android Studio - android-studio

I have an Android Studio project to which I've added an ndk module. My ndk module builds and creates lots of different .so files in various build/intermediates directories. However, when I run the app it is unable to load the library .so from my System.loadLibrary call.
Java files that I include in the same module are found just fine so I was expecting Android Studio to 'find' the ndk files as well.
In looking at the google hello-libs demo I see that they have added a step in the CMakeLists.txt file to set the LIBRARY_OUTPUT_DIRECTORY:
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../distribution)
set_target_properties(gperf
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY
"${distribution_DIR}/gperf/lib/${ANDROID_ABI}")
and then added an associated reference in app:build.gradle
jniLibs.srcDirs = ['../distribution/gperf/lib']
Adding similar entries to my project allowed it to run properly.
Is there a simpler way to cause an ndk compiled module to be included in the main application? If not, is there a way to do it without having to figure out all the relative ../../ path elements. I guess I was hoping the module CMakeLists.txt would be
the same configuration regardless of what other projects it might be included in but having to know where it's including project is located seems to break this.

Related

Android Studio Library Module to AAR file for Dynamic Loading

I have an Android Studio project that has two modules:
app (dependencies on common.jar and ext)
ext (dependecies on common.jar)
common.jar has most of the model logic of my app.
Right now the app uses reflection to find and instantiate the extension service. Since the class is in the APK the nornal classloaded finds it.
Now I want to sever the app's dependency on the ext module and use DexClassLoader to find the service class from an AAR file.
I have used java and the AndroidSDK dx tool to create a simple JAR file to verify I have the dynamic loading working ... I can find, instantiate, and execute methods from the test JAR file.
I just can't figure out how to make Android Studio generate the AAR file for my library module.
I found an answer burried in:
Create aar file in Android Studio
I saw this before ... but missed the key missing information ...
Using Gradle menu ... I have never used or noticed this before.

Opened project in android studio and all packages/symbols are marked as incorrect

I am trying to contribute to my first open source project but after forking and cloning from the repo, all files are marked as an error.
Sample error
This is after selecting a source folder.
Current project source
Under package it says: package name does not correspond to filepath
... but I have this
misnamed packages?
Android studio isn't picking up those libraries.
IME there are 2 ways of importing these (I have only had success with the first):
1) copy the source parallel to your own (ie example.com)
2a) include the jar in a libs folder and tell AS to look out for it. (right click will typically provide a good option that I can't recall)
2b) because Android can struggle (ie I couldn't do it, though inroads may have been made since) with importing jars, you may need to use AAR's (android library packages, Android Archive Library (aar) vs standard jar)
However, because this is an open source project, this should all be handled auto-magically for you via the gradle scripts included in the distro.

Android studio - Unable to add library from outside folder

I am trying to develop an android library and an app using Android Studio. For this, I need to use the library directly in my app project, so I can modify both the library sources and app sources easily.
I am using versions: Android Studio 3.1.2 and Gradle 4.4
First of all, I have tried both methods described in the documentation, but both methods duplicate the library.
Second, I tried to add the library from outside folder as described here, but I get the error:
Unable to find module with Gradle path ':mytestlibrary3-release' (needed by module 'app'.)
My test structure is like this:
In "Workspace" folder I have the app folder "MyApplication4" and library folder "MyTestLib3" containing the library module "mytestlibrary3" (I attached screens)
The application settings.gradle:
include ':app'
include ':mytestlibrary3-release'
project(':mytestlibrary3-release').projectDir = new File(settingsDir, '../MyTestLib3/mytestlibrary3')
and the application build.gradle:
...
dependencies {
...
implementation project(':mytestlibrary3-release')
}
What am I doing wrong here? My purpose is to use the code directly (similar to Eclipse "Add Project to Build Path" or link src folder to source) or to build and use the library as simple and elegant as possible.

How to use NDK with Android Studio to compile a Linux .so to Android .so platform

I have installed Android Studio and would like to use Linux .so to
recompile and make it Android .so.
I have searched around and all examples show a ready make .c compile to .so and reuse that .so, in the Android Studio. This is not in my situation, I only have Linux .so without the .c file.
By referring from multiple websites, I made a JNI folder and put it along with gradle/build folder OR under app\src\main. It is OR because many developers put them at either location. I have no idea which is the correct version both the locations I have tried and it does not make any change to the project (no libs folder generated).
Some suggested that the Linux .so cannot be reused for Android .so because of platform difference, however, some also suggested it is possible without further explanation or example.
Would anyone here know how to do it?
Specifically:
Steps for creation of JNI folder and its location (I know how to create JNI folder, just not sure about the location)
How to call the .so and write codes to re-compile it? (Need
MainActivity or not, how does the code looks like)
ndk-build setup and the path/project settings
Thanks. Lee
You can't. The people that told you that platform differences will prevent it were right.

Android Studio -- linking with a .aar that contains a .so

I'm migrating an existing, working project from Eclipse to Android Studio. The code base consists of a native C++ library built with the NDK (libMylib.so), a Java class that wraps all the native code (MyClass.java), and a test harness(MyApp).
In Android Studio, I've created a stand-alone project (not just a module) that builds "MyClass", and that project has a "src/main/jniLibs" folder where I place "libMyLibrary.so".
The project builds and produces "MyClass.aar", which contains all of its dependencies -- including those .so files.
My test app imports this .aar file as a new dependency "module". It builds and deploys to a device, but encounters a runtime exception when trying to call a native method contained in the .so file.
Trying to load lib /data/data/com.mycompany.webview/lib/libMylib.so 0x4131cd18
Added shared lib /data/data/com.mycompany.webview/lib/libMylib.so 0x4131cd18
nativeCalls.cpp﹕ JNI_OnLoad() called successfully.
No implementation found for native Lcom/mycompany/Mylib/MyClass;.nativeInit
Shutting down VM
The debug statement printed in "JNI_OnLoad()" is mine, so I know the correct .so is being loaded without issue. But then something goes wrong with the call to 'nativeInit()'.
What sorts of things could cause this kind of failure?
Thanks.
1) What value does JNI_OnLoad() return? In the code that I have it is JNI_VERSION_1_6.
2) Are the function names correct?
cd bin/classes
javah com.example.MyClass com.example.OneMoreClass
This will generate the correct C/C++ headers for your native functions.

Resources