How do I develop Android apps using assembly language, either in Windows or Linux?
I don't personally know, but I found examples on the net (Android ARM inline assembly, Motivation for hand-optimized Assembly code), so I assume you can. Looking at it, it is clear they are coding in C or C++ using Android's NDK and inserting there the assembly code, so I can only suggest you should start looking there.
You can enormously increase efficiency by writing your code in assembly language, but you should try to find a middle point between efficiency and maintainability, that's the reason we have object oriented languages (such as Java). So, as a personal tip, I would use Java for everything except for a really specific task that needs maximum performance (like rendering video or images).
Note also some other problems of assembly like using specific methods that may not be present on all ARM processors.
Try this: http://peterdn.com/post/e28098Hello-World!e28099-in-ARM-assembly.aspx
Generally, all you need is a cross compiler for android linux.
I never heard or view android developing with assembly language (If you are talking about C,C++ then its possible)..
But, If you want to write your android application in some C, C++ language then
you can use Android NDK for that. But in that case also you have to make a UI interface with Android java side for your application.
Also If you are good in Objective C then you can also use a QT and then integrate it with android but its in some initial supported phase..
UPDATE: Also you have any Assembly language file then you can integrate it with native Android.mk (Java Native Interface) file and use it in your android application. But this not include any UI part.
Related
I would like to create a dll that would contain definition for some functions. I would be able to use the functions in both Objective-c and java environnement.
Is it even possible to do this?
Thanks!
Write in C or C++. You'll be able to link that to Objective C/Cocoa via the magic of Objective C++, and to Java on Android via NDK and JNI. That's what I do in my project. The compiler is GCC in both cases, the RTL is not identical but similar enough.
Avoid hairy data structures in the interface, stick to primitives and primitive arrays. And, naturally, you'll probably need to abstract away some of the platform.
You might want to compile your code with -fshort-wchar. It happens so that short is the native character format in both Cocoa and JNI. You'll lose widestring functions of the RTL though, but they're no use with Cocoa strings and Java strings anyway. Or you can use UTF-8 on the library/platform boundary. Conversion overhead on every call, yadda yadda.
Note: if you just want to reuse some minor helper functions, it's just easier to write them twice, or copy/paste then adjust the syntax. Debugging NDK code is notoriously tricky. Only go this way if the shared bits constitute 25-30% of the project or more. In my case, it's more like 60% shared.
EDIT: if you go this way, some further porting to other mobile platforms will be a snap and some - not so much. Specifically:
Samsung bada - snap (also C++ with GCC, yay)
Mobile Qt (Meego, etc) - snap (same as above)
Windows Mobile 6.5 and under - relatively easy (compiler difference between GCC and MSVC might get in the way)
Windows 8 tablets (AKA WinRT, Metro) - same as above
Blackberry Playbook - possible in theory, never tried
Old school Blackberry - impossible, it's all Java
Windows Phone 7 - impossible, it's all C#/VB.NET
If you want to create an application for iOS and Android and you want to reuse the business logic of your application (not the UI), take a look to Xamarin.
You can develop with C# and create Android and/or iOS apps.
From Xamarin web site:
Save time by sharing data structures and non-UI code between iOS and
Android.
Hope it helps.
I recently know that we can use Android NDK (use C++ language) to program. But, I'm afraid that, can we use Android NDK to do most of work in Android ? (because people just say that NDK just helps increasing performance of an android program)
I see that ADT plugin for eclipse is powerful, but It's for java. So, if I want to develop on Android NDK, does it has a plugin same with that.
thanks :)
Java is well-supported on Android: there are books, tutorials, Google results and a lot of similar documentation and resources. Developing with the NDK has less stuff available.
Although you can in principle write your full application in C++ using the NativeActivity, I believe that you will have less resources at your disposal for that, compared to Java. If you really want to make your Java program faster, you should profile it, to find which part needs this performance gain and write only that part in C++ using JNI.
you could, but it is not recommend. For one c++ and java performance for multiple aspects are equivalent, then every jni call has its overhead that you have to take in account. Take a look to this link
Forenote: This is an extension of the thread started on /r/haskell
Lets start with the facts:
Android is one awesome Operating System
Haskell is the best programming language on the planet
Therefore, clearly, combining them would make Android development that much better. So essentially I would just like to know how I can write Haskell programs for the Android OS. My question is:
How can I get a Haskell program to execute/run on the Android OS?
How you do it is by first getting a Haskell compiler which can target C with the android NDK which comes with a GCC port for ARM architectures. JHC can trivially do this with a very small inf style file which describes the platform (word size, c-compiler, etc) I've done this with the Wii homebrew dev kit and it was quite easy. However jhc still has some stability issues with complex code such as using a monad transformer stack with IO but jhc has been improving a lot over the last 6 months. There is only one person working on JHC I just wished more people could help him.
The other option is to build an "unregistered" port of GHC targeting the ndk gcc, this is a lot more involved process because GHC is not a true cross-compiler at the moment and you need to understand the build system what parts you need to change. Another option is NHC which can cross-compile to C, like GHC you need to build nhc targeting a C compiler, NHC does not have many Haskell extensions like GHC.
Once you have Haskell compiler targeting NDK GCC, you will need to write bindings to either the android NDK JNI glue code framework (added since android 2.3) or you must write JNI glue code between Java-C-Haskell, the former option is the easier solution and if I remember correctly might actually be backwards compatible with previous versions of Android below 2.3.
Once you have this you must build Haskell code as shared library or static library which gets linked into the NDK java glue code (which itself is a shared library). As far as I'm aware you can not officially run native executables on android. You could probably do it with a rooted phone, thus I assume this means you can not distribute native executables on the app store even when the NDK gcc port can generate native executables just fine. This also probably kills the option for using LLVM unless you can get the NDK JNI working with LLVM.
The biggest hurdle isn't so much of getting a Haskell compiler for android (which is still a big hurdle) the biggest problem is that some one needs to write binding APIs for NDK libraries which is a huge task and the situation is worse if you need to write android UI code because there are no NDK APIs for this part of the android SDK. If you want to do android UI code in Haskell somebody will have to write Haskell bindings to Java through JNI/C. Unless there is a more automated process to writing binding libraries (I know there are some, they are just not automated enough for me) then chances of some one doing it are quite low.
L01man: Is there a tutorial about how to do this? For the
first part, I understand I have to download JHC. What do I have to
write in the inf file and how to use it?
Please note before I answer this question I haven't used jhc for quite sometime since I originally wrote this and newer versions have been released since so I do not know how stable jhc currently is when it comes to code generation of more complex Haskell programs. This is a warning to anyone before you consider making a large Haskell program with JHC, you should do some small tests before you go full on.
jhc does have a manual http://repetae.net/computer/jhc/manual.html and a section on setting-up cross-compilation and .ini file with options: http://repetae.net/computer/jhc/manual.html#crosscompilation.
L01man: The second part is an alternative to the first. I don't know how to do what you said in the
third.
Before you begin you should have some knowledge of C and be comfortable with using the Haskell foreign function interface (FFI) and tools such as hs2c. You should also be familiar with using the Android NDK and building .apk with shared libraries. You will need to know these to interface between C-Haskell, Java/C-Haskell and develop Haskell programs for Android that you can officially distribute/sell on the market store.
L01man: I understand that its goal is to create a binding for the
Android API. But... does the 4th part says we can't make .apk with
Haskell?
.apk is just an app package file format and is built with the tools that come with the Android SDK (not NDK), this has very little to do building the binaries itself. Android packages can contain native shared libraries, this what your Haskell program will be and the native shared/static libraries are generated via the Android NDK.
A language that has recently come to my attention is Eta.
Eta's compiler is a fork of GHC 7.10 which has a JVM backend. It is possible to use the generated JAR files to write Android apps and even use its Foreign Function Interface to call native Android Java libraries.
Brian McKenna has written a blog post about how to configure an Android Studio project to use an Eta library.
There is https://github.com/neurocyte/android-haskell-activity demonstrating Haskell code running.
I once came across the same Reddit thread, but it was old, and comments were closed. I sent a message to the OP, but am not sure whether it reached the recipient. My suggestion here (may work for older Androids where native activities were not possible).
I (developed in Haskell some time ago, but currently switched to Smalltalk) am currently developing a port of Squeak VM to Android. The way I am doing this is similar to what might be dealt with in a haskell-on-android project: a lump of C code which needs to be called from Java part of the application (basically all that can be done in Android is to handle various events; an application cannot poll for events itself and does not have any event loop). In my case the code is generated by the Squeak VM building tools, in the case of haskell on android this will be output from GHC of JHC or whatever front end used. This repo may be worth looking at:
http://gitorious.org/~golubovsky/cogvm/dmg-blessed/trees/master/platforms/android/project
Under "src" there is the Java code which provides for user events interception and sending them to the native code (see the CogView class). The C code of the VM itself is not completely there (see squeakvm.org, the Cog branch for that), but one may get the idea. One also might look under http://gitorious.org/~golubovsky/cogvm/dmg-blessed/trees/master/platforms/android/vm which is the C frontend to the interpreter (including user events handling, some timekeeping, etc.)
Hope this helps.
Dmitry
There is https://github.com/conscell/hugs-android a port of HUGS Haskell interpreter to Android.
I think the general answer should come from source->source transformations, since loading specially compiled shared objects seems to be a bit of a kludge (involving ghc->c and a c->java step in the answers above). This question thus falls under the heading of Haskell on the JVM, which has been tried (with one step as a Java intermediate representation) and discussed at length. You could use frege if the libraries you need compile there. The only remaining steps would be the beginnings of the Android framework API translated into IO() actions and maybe a wrapper for building the manifest xml and apk.
The Android NDK has just been significantly expanded to include support for writing android applications entirely in native C/C++ code. One can now capture input events on the keyboard and touch screen using native code, and also implement the application lifecycle in C/C++ using the new NativeActivity class.
Given all the expanded native capabilities, would it be worthwhile to completely bypass Java and write Android application in native code?
The NDK is not native per-se. It is to a large extent a JNI wrapper around the Android SDK. Using NativeActivity gives you a convenient way of dealing with certain app-life cycle events, and add your own native code on top. ALooper, AInputQueue etc. are all JNI wrappers of the Java SDK counterparts, some with additional code that is private and unaccessible for real apps.
When it comes to Android development, there's no such thing as writing an application entirely in native C++ - you will (in every real App case that I can think of) always need to use the Android API:s, which are to a huge extent pure Java. Wether you use these through wrappers provided by the NDK or wrappers that you create yourself doesn't really change this.
So, to answer your question: No, it wouldn't be worthwhile, because you would end up writing JNI wrappers for SDK calls instead of writing JNI wrappers to your own Java methods that do the same thing, with less code, simpler code and faster code. For example, showing a dialog using "pure c++", involves quite many JNI calls. Just calling a Java method through JNI that does the same thing will give you faster code (one JNI call), and, arguably, code that is easier to maintain.
To fully understand what you can do, you really must examine the Android source code. Start with native_app_glue.c, which is available in the NDK, then continue with the OS implementation of AActivity, ALooper, AInputQueue etc. Google Code Search is a great help in this. :-)
If it is easy to do in Java, and includes many calls, call a method through JNI that does it all, rather than writing all the extra code to do it with multiple JNI calls. Preserve as much of your existing C++ code as is reasonable.
Not if you are just making a standard application. The Java SDK is more complete than its Native counterpart right now so you would still be making things more difficult for yourself.
If you are not doing something that requires the NDK (read: real time performance sensitive) then stick with Java.
Just some food for thought but if you have an app on iOS and Android, some C/C++ code might be shareable. Obviously the iOS Obj-C and platform specific code wouldn't work elsewhere. (Ditto for the Android specific stuff). But you might be able have some shared code that's platform neutral.
If you can, stick with the java style apps until versions of Android supporting native activities constitute a significant fraction of the installed base.
For things that were hard to do before - particularly ports of existing code - this will probably be a big help.
It's not entirely clear yet what has changed vs. just writing your own thin java wrapper. For example, is there still a copy of the dalvik VM hanging around?
I face a problem while developing a native C/C++ shared library for Android platform. As we all know that Android use Java language for the upper layer development. Now I have ported my Engine code using ASCII C/C++ to Android using its bionic library, yet when need to design the User Interface, I have to use the JNI to call my engine code.
As far as I know, that is the only choice. The problem is my engine own hundreds of export APIs. If I use the JNI tech. I need to wrap these APIs to a new shared library for use, which will cost a lot of time for development and testing.
Can somebody give some suggestions for this situation? I am not familiar with java or JNI tech by the way.
TIPS:
When I searched the internet, I found some open source for JNI generator such as JNative etc. Until now I do not know is it suitable for Android platform or not.
You can easily use SWIG www.swig.org which will generate the JNI bindings for you.
there is nothing android-specific in that operation, so it will work rather out-of-the-box.