I want to develop an App that makes use of some heavy C++ code.
From what I have understood, I have two choices.
Build the library outside Android Studio and then import it, or put the C++ source code directly in Android Studio to let it build.
If I choose to build the library Outside Android Studio, I can use (OW): Windows or (OL): Linux.
At the same time, I know I can run Android Studio on either (AW): Windows or (AL): Linux.
My question is, can there be any performance difference in the App product running on the phone, depending on a specific approach I would follow (OW+AW/OW+AL/OL+AW/OL+AL/AW/AL)?
To integrate a C++ library into your Android application, from the performance point of view there's absolutely no difference. The difference in all those your mentioned options is the build process of your C++ library (the files with .so extension, i.e. shared library) which will be later integrated into your final .apk executable file. The Android NDK is doing everything at one place, but aside from performance, you should also consider other factors like development and testing capabilities, based on your project and your (and your peers') active skills. Speaking of the performance, it mostly depends on the number of calls between Java and C++ parts - the less data goes back and forth the better for the performance.
Related
I want to make an interface for a C++ complicated algorithm on a tablet environment, the idea of a server running the C++ code isn't applicable.
Can I embed my C++ algorithm in NDK application (for android, can I do the same for iOS?) and make the interface with Android SDK, and then use the NDK app (maybe as a function) in my SDK android app?? is this scenario possible and how hard it's?
I'd prefer the most a solution involving cordova and hybrid approaches.
If there is a simpler approach I'd appreciate any idea.
You probably want to port some code to Android. I do not know if you code has been ever built under Android.
Anyway, first of all you have to locate and manage to build the hello-jni example in the Android NDK. I have it in android-ndk-r9d/samples/hello-jni. After that, you may start to add functions to that project. Forwarding data between Java and C++ is... well... error-prone.
Android comes with two build systems, the old one is ant-based, the new one is gradle. In addition, some guys manage to use 3rd-party build systems for Android.
If your project has never been built under Android, you will have to port it. If it has Windows/Linux/Mac builds, start from a Linux or a Mac one (in the latter case please note that "darwin" contains "win", this may cause bugs in your build script.) You will likely have to borrow some standard functions from where you can find them. In addition, a while ago Android did not support std::string at all...
If your project has been built under Android, you will have to manage to build it yourself and then use the binaries. Alternatively, you might prefer to just use the existing binaries (this will require less effort) and just not care about bugs.
One more possible approach is to find the project that both uses your preferred build system and has an Android build, build that project for Android and remove the contents, thus obtaining an empty project that is built with your build system of choice. I did no do this and I suppose it will not be as easy as it sounds now. (But you will have a chance to become an expert in that build system.) UPD This section applies mostly to projects that come with a 3rd-party build system. Usually such exotic build systems have some specific features, and one needs these features to build successfully. The general rule is that beyond some level of complexity all working systems are modifications of other working systems (and are never created from scratch). (Example: programmers add functionality gradually and testing is done after each addition; they just cannot write all and then test all.) So in the case of a 3rd-party build system it may be reasonable to take some working project using that 3rd-party build system, get the build system working and replace the project source.
In general what you want is possible for both Android and iOS, but there is no general-case solution.
I don't have experience in android development and I'd like to start writing an application.
The official developer tools page contains link to two different IDEs. The first contains a bundled ADT version of Eclipse.
Android Studio, the second IDE, is based on IntelliJ.
Apart from these differences I can't get if there is anything that I can do with only one of them (I guess the answer to this question is no) and what's the point of having two distinct official IDEs.
What are the differences between the two?
The first contains a bundled ADT version of Eclipse.
This is simply a packaging convenience. You are welcome to obtain Eclipse separately and add Android tooling to it.
Android Studio, the second IDE, is based on IntelliJ.
At the present time, this is an early-access preview. IMHO, it is not suited for someone who does not have experience in Android application development.
Apart from these differences I can't get if there is anything that I can do with only one of them
At the present time, Android Studio is an early-access preview, meaning that there are lots of things that it does not have integrated in. Now, by this time next year, and hopefully far sooner, Android Studio will have equivalent or superior integration than does Eclipse with the ADT plugin.
and what's the point of having two distinct official IDEs.
There are an infinite number of "official" tools. You are welcome to use a plain text editor and tools outside of any IDE, for example.
You are welcome to watch the Google I|O 2013 video on developer tools, where they describe a bit of the rationale behind the development of Android Studio.
Android Studio vs Eclipse – Main Differences:
Build Tools
Android Studio utilizes the fast growing Gradle build system. It
builds on top of the concepts of Apache Ant and Apache Maven but it
also introduces a Groovy DSL (Domain-Specific Language) that allows
for scripted builds which opens up many automation possibilities like
uploading your beta .apk to TestFlight for testing. Eclipse on the
other hand uses Apache Ant as its main build system which a very
robust XML based build system that many Java developers may already be
familiar with.
Advanced Code Completion/Refactoring
Both IDEs feature the standard Java code auto completion but in the
case of Android Studio, Google has baked in deeper support for
specific Android code and refactoring. Android Studio can refactor
your code in places where it’s just not possible using Eclipse and
ADT. In addition, in my opinion IntelliJ’s Java auto completion seems
more “intelligent” and predicts better what I want to do so there is
definitely an improvement in this area over Eclipse.
User Interface Design
One of the main selling point Google used to market Android Studio
when it came out was its completely redesigned user interface design
tool. After working with it for some time, it’s clear that the new
tool is much better than the old. It literally crashes it. The new
interface design too in Android Studio is faster, responds to changes
more rapidly and has more customization options that with Eclipse, you
had to manually set in the XML.
Project Organization
Both IDEs work differently in an effort to help you manage and
organize your projects. If you’ve used Eclipse then you must be
familiar with the concept of workspaces. When Eclipse starts, you
select the workspace that contains your projects and you can load all
project of that workspace in your tree navigation. If you want to
switch to a project in a different workspace, then you have to restart
the whole IDE. Android Studio treats this situation differently by
introducing the concept of modules. Your app could be one module, a
library that you just downloaded can be another and the Ad SDK you are
currently integrating could be a third. Each of these modules can have
their own Gradle build files and declare their own dependencies. To
me, Android Studio seems more natural but it takes some time to get
used to if you have been using Eclipse for a long time.
IDE Performance/Stability
Eclipse is a purely Java based software, and a big one. In order to
run it reliably you need to have more than decent amount of RAM and
good CPU power to back it up. Many user who do not strictly meet these
criteria are reporting very bad experiences with it. It is not unusual
for Eclipse to crash while exporting an apk or having to restart it
after using it for a few hours straight. Having said that, Android
Studio is still in beta so it comes with its own bugs that crash the
IDE every now and then but in the meanwhile, the whole experience
feels faster and more robust.
Conclusion
Having used both Android Studio and Eclipse for a while now, I would
personally say that Android Studio has the edge over the two. It might
be a bit unstable yet and some updates require a complete
re-installation of the software but when it eventually comes out of
beta, it will blow Eclipse with ADT out of the water. I especially
like the stability of the editor and not having to reboot every now
and then, the new and improved UI designer and the sexy themes that
make Android Studio a real eye candy. What side will you take in the
Android Studio vs Eclipse battle?
Besides the numerous points already made, while you can use the ADT tool for other projects outside Android, the Android Studio is purely dedicated to the development of Android apps.Android Studio also allows you to work faster as it gives you intelligent suggestions as you type, freeing you from the shackles of having to go back to your .java or .xml files to look up names of variable or other stuff you can't remember.I just started using Android Studio myself and i can say i am never leaving it.It is particularly good for new programmers.
OK this is a strange one:
Is there a way someone can develop native C applications or libraries for Android without using the Android NDK?
What was happening before the NDK was released?
(It's not there for too long, I think it was released only one or two years ago).
Apparently, you can -- a friend of mine is a real Android guru and he managed to build a GCC-based native toolchain entirely by hand. He also fixed some missing parts in Android's libc. The main idea is the following: GCC has builtin support for the arm-elf-linux target, so with an appropriate build script, you can configure it to build for Android. However, you have to root the phone to run the resulting binaries. One even cooler thing is that because GCC is a self-hosting compiler, with the arm-linux-elf toolchain, you can recompile GCC once again and have the toolchain on the phone itself.
Before the NDK was released, the only officially supported way of developing Android applications was to use the Android SDK and writing your applications in Java.
As others have mentioned, it's possible to cross-compile some applications as completely stand-alone and run them on a rooted phone. The downsides of this should be obvious: very few people will be able to run your application (they also need to be root, plus you won't be able to get your application up on the Play store); and you might even run into compatibility problems yourself across different devices e.g. if you rely on dynamic linking against various libraries (which you might need to do in order to keep the size of the binary down).
TL;DR: it's possible, but severly limited, and not recommended.
You can compile your C code with an Android-compatible toolchain (such as CodeSourcery) and run it on a non-rooted phone, from its command line (for example through an SSH connection like SSHDroid).
What is the Android NDK (native development kit) ? How can one use it? Why should one use it?
The NDK (Native Development Kit) is a tool that allows you to program in C/C++ for Android devices. It's intended to integrate with the SDK (it's described as a "companion tool") and used only for performance-critical portions of a project. See here for more information.
NDK may improve application performance. This is usually true for many
processor-bound applications. Many multimedia applications and video games use
native code for processor-intensive tasks.
The performance improvements can come from three sources. Firstly, the native code is compiled to a binary code and run directly on OS, while Java code is translated into Java
byte-code and interpreted by Dalvik Virtual Machine (VM). At Android 2.2 or higher,
a Just-In-Time (JIT) compiler is added to Dalvik VM to analyze and optimize the Java
byte-code while the program is running (for example, JIT can compile a part of the
byte-code to binary code before its execution). But in many cases, native code still
runs faster than Java code.
Java code is run by Dalvik VM on Android. Dalvik VM is specially designed
for systems with constrained hardware resources (memory space, processor
speed, and so on).
The second source for performance improvements at NDK is that native code allows
developers to make use of some processor features that are not accessible at Android SDK,
such as NEON, a Single Instruction Multiple Data (SIMD) technology, allowing multiple
data elements to be processed in parallel. One particular coding task example is the color
conversion for a video frame or a photo. Suppose we are to convert a photo of 1920x1280
pixels from the RGB color space to the YCbCr color space. The naive approach is to apply a
conversion formula to every pixel (that is, over two million pixels). With NEON, we can process multiple pixels at one time to reduce the processing time.
The third aspect is that we can optimize the critical code at an assembly level, which is a
common practice in desktop software development.
Disadvantage
NDK cannot access lots of APIs available in the Android SDK directly, and developing in NDK will always introduce extra complexity
into your application.
The Android NDK is a companion tool used only in conjunction with Android SDK which allows application developers to build performance-critical portions of their apps by use of native (C/C++) code.
This provide benefits in form of reuse of existing code and increased speed.
Please go through below links.
Link-1
Link-2
Link-3
The Android NDK is a companion tool to the Android SDK that lets you build performance-critical portions of your apps in native code. It provides headers and libraries that allow you to build activities, handle user input, use hardware sensors, access application resources, and more, when programming in C or C++. If you write native code, your applications are still packaged into an .apk file and they still run inside of a virtual machine on the device. The fundamental Android application model does not change.
The following links also answers your question:
What is NDK?
When to Develop in Native Code
NDK Download
How to build NDK app
how to work with NDK
10 tips for Android NDK
The Android NDK is a toolset that lets you embed components that make
use of native code in your Android applications.
Android applications run in the Dalvik virtual machine. The NDK allows
you to implement parts of your applications using native-code
languages such as C and C++. This can provide benefits to certain
classes of applications, in the form of reuse of existing code and in
some cases increased speed.
Source: http://developer.android.com/sdk/ndk/overview.html
The Android NDK is a companion tool to the Android SDK that lets you
build performance-critical portions of your apps in native code. It
provides headers and libraries that allow you to build activities,
handle user input, use hardware sensors, access application resources,
and more, when programming in C or C++. If you write native code, your
applications are still packaged into an .apk file and they still run
inside of a virtual machine on the device. The fundamental Android
application model does not change.
Source: http://developer.android.com/sdk/ndk/index.html
NDK is just a set of tools which lets you to write C/C++ codes for your application.For example suppose you want to add a critical function/performance to your app and you want to write it in C/C++ then eclipse or any other IDE will not allow you to write your C/C++ and in that case you have to use NDK and integrate it in your app.
NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++....Checkout this https://developer.android.com/tools/sdk/ndk/index.html
Android NDK (native development kit)
Android Native Development Kit (NDK) is developers to write code in C/C++ that compiles to native code
Why should one use it?
The source code is compiled directly into machine code for the CPU (and not into an intermediate language, as with Java) then developers are able to get the best performance
How can one use it?
Here best tutorials
https://developer.android.com/ndk/index.html
https://www.androidauthority.com/android-ndk-everything-need-know-677642/
https://www.ntu.edu.sg/home/ehchua/programming/android/Android_NDK.html
Im pretty sure the answer is no, but im using the new IntelliJ EAP version and have a project i worked on a while back which was just written against JDK 1.6 for an applet based application.
However now i could see how it could be useful in an android app that im interested in creating.
Do i now have to re-write all the code/tests again targeting the android sdk or can i just drop in my existing JAR file and only use android for the UI layer.
Its just android seems to make testing WAY harder than it needs to be, and i have alot of existing tests written and working, if it wasnt so hard to just write a quick unit test (standard Junit #Test style) i wouldnt mind porting, however i just dont get the whole instrumentation thing, as i dont need a UI at the moment...
Anyway so back to the point, can i use my existing JDK built code in an android app?
No!
Android uses the "Dalvik" VM from project harmony which uses a different set of bytecodes which are incompatable with the standard Java JVM bytecodes.
This was done both to optimise the VM for opreration on mobile platforms, and, probably more importantly to try and avoid Copyright and patent disputes with Sun and now Oracle.
More info here
However there is a tool called "dx" which can perform the conversion in the dev environment.
Okay, let me clear up your confusion.
Jars are converted to dex Dalvik bytecode during the compiling process thus you can use 3rd party jar libs.
However, in your case because its applet which has a different application lifecycle yes you might have to re-do it to get it to work in android.
As far as testing instrumentation is used on all java mobile development even JavaMe. It basically means that the Junit tests are run in the emulator or device but in android's case you are using android mock objects to test android specific things.