How to build linux command for Android on Linux machine - android

Since there are a few commands missing on Android, I want to build them by myself.
Take one as an example:
There is no dig command on Android.
I've got the source code from ftp://ftp.isc.org/isc/bind9/9.2.3/bind-9.2.3.tar.gz and then I want to build it. But I got stuck.
How can I build the executable on my linux machine?

You can do it in two ways.
To build any C Source code for Android Platform you can use Android NDK Toolchian, It lets you build Native code for another platform.
It can be used as a stand-alone compiler without an Android APK.
Documentation and Sample Code.
To build it as an executable (without APK) and not shared object, replace include $(BUILD_SHARED_LIBRARY) with include $(BUILD_EXECUTABLE) in Android.mk.
For Linux commands on Android you can use Busybox.
It supports most of the commands.
However dig is not supported in busybox, you can add it by following this article.
Hopefully this gives you a start.

Related

How to cross-compile for Android without cmakelists.txt?

I know that if a C++ project has CMakeLists.txt, I can create a new project and use Android studio to compile the executable to run on Android via console.
However, some projects don't have CMakeList.txt and need to run . /configure and then make. e.g. https://github.com/strukturag/libheif
Is there a easy tool that can compile it for Android on Windows?
The following assumes that you have mingw installed and also a cross-compiler compatible with your platform. Also, I'm not a big fan of Windows for this type of development. So my explanation is for Linux. But I believe you can easily adapt it to your case.
First of all, you need to install a cross-compiler. For the sake of explanation, I'm going to use gcc-linaro here.
If you open the repo, you'll see that there is a file called autogen.sh.
You will need to run it. This file will create the configuration tool.
Once it finishes, a file called configure will appear.
This is the tool you want to use for configuring the library to be compiled, customized for your specific needs.
The next command you want to run is
./configure --help
The above command will show you all the options that you can set. You can play with them but, typically, you want to set the following, at least:
CC
--prefix
--includedir
--libdir
--host
These options are explained in the configure tool. To give you an idea on how to use them, the following is the generic command that you might want to run:
CC="<path to your gcc compiler>" ./configure --prefix="<path_to_the_folder_where_you_want_to_install>" --includedir="<path_to_the_include_dir>" --libdir="<path_to_the_lib_dir>" --host=arm
In my situation, I have installed the cross compiler at the following path
/home/alexis/gcc-linaro/bin/arm-linux-gnueabi-gcc
Therefore, I would need to run the command:
CC="/home/alexis/gcc-linaro/bin/arm-linux-gnueabi-gcc" ./configure --prefix=/home/alexis/gcc-linaro --includedir=/home/alexis/gcc-linaro/include --libdir=/home/alexis/gcc-linaro/lib --host=arm
Hope this helps.

Using PJSIP open source library for VoIP media and signal handling

I am fairly new to android, I got a job as a Junior Android and I've been suggested to use PJSIP to create the back-end functionality of a VoIP app. It's meant to be handling the media transfer and singaling on its own.
I'm struggling while going through the documentation. I try to read it but it's hard trying to understand the code and functions. How do I implement a library written in C, in Android Studio (Java)? How do I read through the documentation and understand what I'm meant to do?
I've tried reading through the PJSIP Developer's Guide, most of it seems incomprehensible to me.
Find pjsua example application for android in pjsip-apps. It has good example of how pjsip c-library is used via JNI interface from android java application.
I am developing a similar application to yours along with video support. I am yet to integrate the pjsip library to the app. But have built the library for android.
To answer your questions,
NDK(an SDK available in android studio) is used to implement native languages such as C and C++ in android. Using JNI interface the C,C++ code is made compatible with android, You don't have hard code any of these for now.
Go to pjsip and get tar or zip files based on the platform(either windows or linux) and extract it into your system.
You also need to install ndk, google through the steps.
Building the library would be a bit of hassle. So follow the official guide and for the errors that arise follow the steps below
Buiding PJSIP for android
Go to the directory of the pjproject downloaded for android
Go pjproject/pjlib/include/pj and check for config_site.h file, if not found create one and add :
hashtag define PJ_CONFIG_ANDROID 1
hashtag include angular braces pj/config_site_sample.h angular braces
export ANDROID_NDK_ROOT = path to the ndk-bundle of ndk(downloaded).
Go to the root folder of pjproject and
execute
./configure
make dep && make clean && make
Errors:
For no compiler found error install clang using sudo apt-get clang
and execute
export CC="$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target armv7-none-linux-androideabi -gcc-toolchain $NDK_TOOLCHAIN"
export CXX="$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -target armv7-none-linux-androideabi -gcc-toolchain $NDK_TOOLCHAIN"
For C compiler cannot create executables error
Grant permission to ndk-bundle
chmod -R 777 ndk-bundle
For missing seperator error in any folders
execute find . -type f -name '*.depend' | xargs rm
For any other errors, also install g++

Can someone explain me the compilation step in making a sqlcipher binary file?

I am new to compiling and want to create a binary file of sqlcipher for use in hybrid android apps.
I am stuck here
Building SQLCipher is almost the same as compiling a regular version
of SQLite with two small exceptions:
You must define SQLITE_HAS_CODEC and SQLITE_TEMP_STORE=2 when building
sqlcipher. You need to link against a OpenSSL's libcrypto Example
Static linking (replace /opt/local/lib with the path to libcrypto.a).
Note in this example, --enable-tempstore=yes is setting
SQLITE_TEMP_STORE=2 for the build.
$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
LDFLAGS="/opt/local/lib/libcrypto.a" $ make Example Dynamic linking
$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
LDFLAGS="-lcrypto" $ make
This instruction seems to be for compiling on Linux.
Can anyone tell me how can I do it in windows?
Also 2nd difficulty is I have to include OpenSSL's libcrypto library. I don't know what it is, why is it required, from where to download it and how to include it while compiling.
Compiling SQLCipher for Android is a much more involved process than just building basic binaries of SQLCipher. That should certainly not stop you however, and if you are interested in learning more about how the project is structured, and built, we provide the source to SQLCipher for Android here.
In terms of build instructions, it is generally performed on either OS X, or Linux, along with the Android NDK, I have never built SQLCipher for Android on a Windows machine. The entire build process is orchestrated using this Makefile.

Compiling Unix tools for android

I want to use some unix tools on my rooted android arm6 based phone. I will be using cross compiler tools provided here. If I want to compile gnu netcat, how can I set the cross compiler prefix to arm-none-linux-gnueabi- and how to enable static linking (no shared library).
I managed to cross-compile rsync for Android using Ubuntu's arm-linux-gnueabi toolchain. See this related question.
Unless you particularly need to build against a more standard libc than bionic, you can just use the ndk's toolchain, either by copying the hello-jni example and changing BUILD_SHARED_LIBRARY to BUILD_EXECUTABLE in the jni/Android.mk or using the script to generate a stand alone toolchain. You may want to use the V=1 option to the ndk-build script to see the commands it's issuing to its gcc.
Otherwise you may need to pass the prefix to the configure script or manually edit it into the Makefile for the project. This often has not gone well as many projects have make systems not really set up for cross compiling, I've had to resort to editing the configure script to set prefixes and skip tests where it tries to execute a test program.
An option that sometimes works when the build system is more complicated than the project requires is to do a configure for your host (let's hope that's linux). Then manually edit the generated Makefile to change anything needed to build for android instead. Might not be a bad idea to do a clean just in case (especially if you did a test host build). And then do the build which will pick up the arm compiler from your Makefile modifications.
Lastly, if you can be content with the original netcat by Hobbit rather than the gnu version, you hardly need to port it to android yourself as that's already been done. There's already an android version in the google tree at https://android.googlesource.com/platform/external/netcat
which may be on your device already (as 'nc'), and is definitely included in alternate ROMs such as Cyanogenmod.

Installing & using the Android NDK in Eclipse

I've been running the Android SDK for a while now in Eclipse (MAC OSX). I've downloaded the NDK and installed the C/C++ tools in Eclipse, but could anyone guide me on using the NDK? For example, do I just create an Android project like normal and build it with the NDK instead?
Really could do with a decent tutorial if anyone know of any.
EDIT: OK so I have the NDK installed now (I think) but does anyone have any idea how to use it? I got as far as this (taken from here):
Run Terminal
cd ~/android-ndk-1.5_r1
make APP=hello-jni
In order to run the hello-jni sample application, but I get an error in terminal saying:
Android NDK: APP variable defined to
unknown applications: hellojni
Android NDK: You might want to use
one of the following:
build/core/main.mk:81: *** Android
NDK: Aborting . Stop.
Any ideas why?
As simply as I can describe it, building an Android app from within Eclipse that uses the NDK requires two steps.
First, inside your terminal you need to run the NDK build script on your project. cd into the root of your project directory and then execute the ndk-build script within that directory.
For example:
cd ~/workspace/hello-jni
./~/android-ndk-1.5_r1/ndk-build
After doing this, you should see some output that results in the creation of a *.SO file within the obj directory within your project directory.
Once you have the *.SO file, the final step to building an application with the Android NDK through Eclipse is to build it with Eclipse like you would any other application and then deploy it for testing.
If you make any changes to the C/C++ code you'll need to repeat step one and regenerate your *.SO file before building and deploying your application from within Eclipse again.
I would like to note that by using the Android NDK your android apps are still based upon Java. They're just communicating with code written in C/C++ by way of the Java Native Interface.
Finally, I am not aware of any Eclipse plugins that will aid with NDK development. Everything I know about the NDK I have learned the official Android NDK documentation. Please feel free to comment and let me know if there anything I can clear up in my response.
Native development and debugging support came into Eclipse environment as of ADT version 20. http://tools.android.com/recent/usingthendkplugin
Set path to NDK from Eclipse Preferences -> Android -> NDK
Right-click on your project and choose Android Tools -> Add Native Support
developer.android.com states you also need Cygwin.
http://developer.android.com/tools/sdk/ndk/index.html#Contents
Required development tools
For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
A recent version of awk (either GNU Awk or Nawk) is also required.
For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.
The docs directory in the NDK has some pretty good information on how to use the NDK itself. Read the overview, Application.mk, and Android.mk HTML docs. You'll want to google for the Sun JNI PDF, download it, and learn what JNI is all about before you go any further. This is because simply compiling a bunch of C/C++ code into libraries with the NDK is only part of the process. You have to write native Java code that calls your C/C++, and you have to create wrapper functions in C/C++ that adhere to JNI conventions that the native Java code can invoke. JNI has been around a long time, it's not Android specific by any means. So, you can, to learn about it, go quite far following tutorials geared towards JNI, using command line tools like javah and javac, and then return to integrating with the NDK after you know the basics. (For an example of what these C shims look like, take a look at the hello-jni sample in the NDK; the C source file there shows you typically what the shims look like. Using javah to generate these shims is the way to go, you create Java classes that have native methods, process them with javah, and it generates the C headers for you, then you code up C functions that adhere to the generated function prototypes).
Note: while the NDK docs would have you manually building from command line and then going into Eclipse to build your app (a laborious sequence of steps, to be sure, especially if you are changing the C/C++ code), it turns out you can integrate easily with Eclipse so that the NDK is run each time you build from Eclipse. To see how, read here.
This is for benefit of others who want to create the project from scratch from within eclipse: I followed steps mentioned here in this blog here and it works fine: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/
To attempt to answer the question directly - you need to run ndk-build in the folder with the native code within your project folder. This creates the .so files found in the file explorer/resources tree under jni in Eclipse. These functions, if the syntax in the code is correct, can now be called from your java code.
I found many sources of help when install and getting to grips with Android Developer Tools and the NDK. I wrote a blog post to share my experiences and hopefully give back to the community that helped me get there which may help understand my answer: http://workingmatt.blogspot.co.uk/2013/03/set-up-android-sdk-and-ndk.html

Categories

Resources