Build NDK with included shared library on multiple platforms - eclipse

I have built a C++ shared library with NDK builder in Eclipse (firestly creating a JNI project with no MainActivity), which provided as output the different platforms that I have specified in the Application.mk file. The output is in the /obj/local/ directory, spread over the platforms subdirectories (armabi, mips, ... i.e. all the platforms that I have specified in the above file).
Now, I want to use this shared library inside an Android native project, where my shared library is called inside a project's native library (therefore it is about "native-calling-native").
If I build both the shared library and the Android native aplication for just one type of platform (let's say "armeabi"), everything works fine.
My question is how to create the make files, Android.mk and Application.mk, of the Android native project so that to build for ALL the platforms.
I suppose the key is in the Android.mk file, which, for just one platform build it looks like this (on Linux, for the .so type, C++11 for info):
LOCAL_PATH := $(call my-dir)
# Define the shared library module used by the calling JNI C++ module
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libs/libCreateSharedWithJni.so
LOCAL_MODULE := CreateSharedWithJni
LOCAL_EXPORT_C_INCLUDES := libs/CreateSharedWithJni.h
include $(PREBUILT_SHARED_LIBRARY)
# Define the calling JNI C++ module
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE := finalmodule
LOCAL_SRC_FILES := CppJniCallShared.cpp
LOCAL_SHARED_LIBRARIES := libCreateSharedWithJni
include $(BUILD_SHARED_LIBRARY)
Thanks!

Got it!
In the above Android.mk file just change the line as following
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libCreateSoWithJni.so
i.e. using the $(TARGET_ARCH_ABI) in the path drives the builder to process all platforms specified in the Application.mk APP_ABI variable.

Related

Adding and using aar file with resources in AOSP build

I need to build an application with android.mk in aosp build tree. I have a custom .arr lib with some resources like drawables,strings..etc, Which resides in the following folder apps/libs/mylib.aar
Anyone can tell me how to include the aar in the android aosp build and use that aar resources in another applications. I already tried the following method described here in Stackoverflow.
https://stackoverflow.com/questions/31205856/aar-support-in-android-mk
Android.mk of my application is looked like
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_PACKAGE_NAME := TestSampleAarLib
LOCAL_STATIC_JAVA_AAR_LIBRARIES:= sampleaarlib
LOCAL_STATIC_ANDROID_LIBRARIES += android-support-v7-appcompat
LOCAL_STATIC_ANDROID_LIBRARIES += android-support-v7-gridlayout
LOCAL_STATIC_ANDROID_LIBRARIES += android-support-v13
LOCAL_USE_AAPT2 := true
LOCAL_AAPT_FLAGS := \
--auto-add-overlay \
--extra-packages com.myaar.sample.lib
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_TAGS := optional
LOCAL_PRIVILEGED_MODULE := true
LOCAL_MANIFEST_FILE := AndroidManifest.xml
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := sampleaarlib:libs/sampleaarlib.aar
include $(BUILD_MULTI_PREBUILT)
If anyone have idea about using resources from aar in AOSP Please share.
Thanks in advance.
To add resources from the aar file, please add the following in your android.mk file.
LOCAL_RESOURCE_DIR +=$(call intermediates-dir-for,JAVA_LIBRARIES,<aar_file_name>,,common)/aar/res
Tested and verified in the Android 8.1 version.
NB: In the latest android versions like 9+ resources automatically merged to the project.

How do I add an application in AOSP?

We are trying to develop a custom ROM using AOSP's master branch. We have successfully built and run the compiled image on the emulator provided by AOSP.
Now we are trying to add an application in the AOSP so that the AOSP branch compiles and runs with the application present in it, that means when we run the android version our newly added application will act as a default/system application.
We have tried doing that using following steps, but have failed:
Place the App_name folder to /packages/apps
Add Android.mk to /packages/apps/App_name/
Add App_name entry to /build/target/product/core.mk
PRODUCT_PACKAGES := \
... \
SomeApp \
App_name
Note: App_name is the application folder that is developed using android studio and is present the Android-Studio Projects folder.
After performing these steps we compile the entire source code/AOSP and eventually the compilation fails.
Could someone please help me out??
For adding a default application to AOSP , You should create a directory with arbitrary name in packages/apps (name of directory doesn't matter) , then you should put necessary code and resources in it . Notice AOSP build system doesn't use Gradle ,hence you don't need to copy gradle build files (like build.gradle and setting.gradle and etc).
For a typical app you should create these directories :
src : place your java codes here .
res : place your resources directory for examle drawable , layout , ...
assets : if your project has any assests file , place them in this folder
Your AndroidManifest.xml file should be added in top of your directory.After placing your code and resources , create an Android.mk file and write the following lines in it :
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := <Name of your app>
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := $(call all-java-files-under, src)
# Include libraries
LOCAL_JAVA_LIBRARIES := <Java lib dependencies>
LOCAL_STATIC_JAVA_LIBRARIES := android-common
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v4
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7-appcompat
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_ASSETS_DIR := $(LOCAL_PATH)/assets
LOCAL_AAPT_FLAGS := --auto-add-overlay
LOCAL_AAPT_FLAGS += --extra-packages android.support.v7.appcompat
include $(BUILD_PACKAGE)
LOCAL_PACKAGE_NAME is name of your app (for examle camera , app1 , ...) ,you should add this name to /build/target/product/core.mk (no folder name , folder name is not important) .
LOCAL_SRC_FILES is pointer to Java codes.
LOCAL_JAVA_LIBRARIES : if your project has java lib dependency , reference to it here.
LOCAL_RESOURCE_DIR is address of res directory
LOCAL_ASSETS_DIR is address of assets directory
Finally build your app and add it to system image . use these commands
make <name of your app>
make snod
<name of your app> is value of LOCAL_PACKAGE_NAME in your Android.mk file. You don't need to build whole AOSP build tree using something like make -j8 . Just build your app and add it to system image. Default applications are placed in read only system partition.

Android NDK Source Compiled & Build Successfully - But Function 'to_string' could not be resolved in Eclipse IDE

I searched how to use to_string() in android ndk. After a long search, from this link, i changed the Application.mk file as said. And the cpp source compiled and build properly only from the terminal.
Terminal output :
> ndk-build
Android NDK: WARNING: APP_PLATFORM android-21 is larger than android:minSdkVersion 9 in
/<some path>/Android/Exercise01/AndroidManifest.xml
[armeabi-v7a] Install : libndkfoo.so => libs/armeabi/libndkfoo.so
But in ecplise IDE, still i am getting error as "Function 'to_string' could not be resolved". So from the IDE i am not able to run the android application. (Note : before the usage of std::to_string, the source compiled properly)
I tried to include the header files as below (under project properties->C/C++ general->Path and Symbols->GNU C++)
vstring.h from //android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.9/include/ext
&
basic_string.h from //android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.9/include/bits
But no luck.
Androi.mk File
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
TARGET_PLATFORM := android-21
TARGET_ARCH_ABI := armeabi-v7a
TARGET_ABI := android-21-armeabi-v7a # concatenation of above two variables
# Here we give our module name and source file(s)
LOCAL_MODULE := ndkfoo
LOCAL_C_INCLUDES := $(LOCAL_PATH)/header
LOCAL_SRC_FILES := src/NativeFunction.cpp src/clsArithmeticParser.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
#APP_STL := stlport_static
APP_STL:=c++_static
I know, i am doing some small mistake, but not able to find for a long time. Any solution/suggestion appreciated.
Edited :
I am able use the std::to_string & std::stoll through eclipse in the below trick only. These can help some one in rare situation. So adding this point to test the c++11 support through eclipse IDE. The error shown only when the source file or the header file opened. Just close open source file documents (.h, .c, .cpp). Close and reopen the eclipse. Now it is ready to compile and run through the android emulator.
What version of Eclipse IDE do you use? Try to update to the latest Mars version, it may fix your problem.
After referring the answer by #Khaled Lakehal, I myself posting this answer for my question.Hope this may help someone.
Followed the below step to make it work:
Update the eclipse version from Luna to latest Mars 2
Imported the existing project from the old version of the eclipse
using -> import -> Existing Android code into Workspace
Had some problem with the C/C++ project conversion after import. So followed this link to undo the C/C++ project conversion
Converted the project to C/C++ newly.
To remove the error, I referred this link, and selected the "Run with build" only
Close & Reopen the IDE
So now able to use the std::to_string & std::stoll functions.

Building Oculus Mobile VR SDK with Eclipse: VrApi module already defined

I am building a blank project in Eclipse with the Oculus Mobile VR SDK. I am getting the following error:
Android NDK: Trying to define local module 'vrapi' in jni/../../../../../../VrApi/Projects/AndroidPrebuilt/jni/Android.mk.
Android NDK: But this module was already defined by jni/../../../../../../VrApi/Projects/AndroidPrebuilt/jni/Android.mk.
Looks like that Android.mk is somehow included twice. I'm using Oculus Mobile VR SDK 0.6.2.0. The following projects are included in my Eclipse workspace:
LibOVRKernel_Prebuilt
MediaSurface
VrApi_Prebuilt
VrAppFramework_Prebuilt
VrGUI
VrLocale
VrSound
Tried re-creating the workspace twice, but still the same error. How can vrapi module be included twice? The only Android.mk where it says
LOCAL_MODULE := vrapi
is in VrApi_Prebuilt and nowhere else.
Thanks
I fixed this by modifying Android.mk inside VrApi_Prebuilt in the following way.
I added
include $(BUILD_SHARED_LIBRARY)
right before the following block:
ifneq (,$(wildcard $(LOCAL_PATH)/$(LOCAL_SRC_FILES)))
include $(PREBUILT_SHARED_LIBRARY)
endif
I removed .so extension from
LOCAL_SRC_FILES := ../../../Libs/Android/$(TARGET_ARCH_ABI)/lib$(LOCAL_MODULE).so
Hence, the line becomes
LOCAL_SRC_FILES := ../../../Libs/Android/$(TARGET_ARCH_ABI)/lib$(LOCAL_MODULE)

CoffeeScript and sbt-concat

I'm having trouble concatenating and fingerprinting all the CoffeeScript files in Play application. Everything works fine for JavaScript files with build.sbt like this one
pipelineStages := Seq(concat, digest)
Concat.groups := Seq(
"javascripts/app.js" -> group(((sourceDirectory in Assets).value / "javascripts") * "*.js")
)
But when sourceDirectory is changed to resourcesManaged that supposedly contains compiled CoffeeScript files sbt-concat doesn't pick them up.
sbt-coffeescript, and all other official source task plugins, don't put their files in resourcesManaged in Assets, but instead their own sub-directory in target/web/<taskname>. They scope the resourcesManaged setting to their main task, in this case this means resourcesManaged in (Assets, coffeescript) and resourcesManaged in (TestAssets, coffeescript).
When you run sbt coffeescript you can see the files are output to target/web/coffeescript/main. You can verify this by running show web-assets:coffeescript::resourceManaged from the sbt console.