14 ms·
Rawdrawandroid: Build Android apps in C
- tomcam 6y agoThis looks wonderful. I wish it had been around when I was doing an android app. I absolutely love seeing it and a good old make script. I am thrilled at the tiny tiny runtime requirements. Bravo. This must have required an incredible amount of experimenting and mucking through old, poorly documented examples in the dusty corners of an SDK somewhere.
- kabacha 6y agoI feel the opposite. Whenever I see Makefile being used in 2020 instead of a hundred better modern alternatives I can't help but feel turned off.
- ianlevesque 6y agoThey could do the same with CMake (and regain things like IDE support) but I think it goes against the spirit of the hack.
- izacus 6y agoIt's been around in form of NativeActivity for years (since Android 2.x I think) - it's literaly what that one does as well: initializes a canvas and gives control to native code.
- kllrnohj 6y agoandroid_native_app_glue is a Google-provided helper library in the NDK, and it's incredibly old at this point (hence the "Copyright (C) 2010 The Android Open Source Project" on those files) Setting up a fully-native OpenGL ES window is also straight out of the NDK samples - this is also incredibly old (it's why NativeActivity even exists) Most everything else looks to just be a handful of hand-written JNI to call common SDK methods - I'm not seeing anything that'd require incredible amounts of experimenting or mucking through old, poorly documented examples?
- saagarjha 6y agoRelated: https://stackoverflow.com/questions/10289890/how-to-write-ios-app-purely-in-c https://stackoverflow.com/questions/10289890/how-to-write-io...
- grawprog 6y agoCool. This seems like it would be really helpful for building libraries for other languages to enable native android programming in a variety of languages. I know D's not the most popular language on here, but I know HN has a few D enthusiasts and Walter Bright hangs out here, but my first thought was this https://wiki.dlang.org/Build_D_for_Android https://wiki.dlang.org/Build_D_for_Android I've tried to cross compile D apps for android and it was a hassle, I never really got it to work properly. Would something like this project help efforts in that direction? Writing wrappers around C libraries in D is fairly painless comparatively.
- winrid 6y agoVery cool! But you can write apps in Java and not have big apk sizes... I have a pretty well optimized collaborative drawing pixel art app on the marketplace and it's < 8mb last I checked.
- Lerc 6y agoTo provide perspective, there was a time when people joked that EMACS stood for Eight Megabytes Always Constantly Swapping. Big is a notion that has changed over the years. Comparing to what I did on my 2.5 megabyte Amiga, I feel like more should be done with less.
- beagle3 6y agoI’ve recently pulled up Mission Impossible, Cybernoid II and International Karate on my C64 and played for a few hours. One of these games has synthesized speech. Each of them fits in less than 64K, on a processor that can be implemented with 930 logic gates (although the original 1978 version had about 3000 transistors which is a bit more) Nostalgia isn’t what it used to be.
- psanford 6y agoAs a funny aside, I built the emacs-native branch for the first time this week and I had to upgrade from 16GB to 32GB of ram for the build process to not run out of memory.
- iib 6y agoDo you know if not having enough RAM would result in segmentation faults during the build process? Or how exactly does it manifest? I have way less than 16 GB and when I tried to compile it a week ago, it segfaulted during the build, but I am unsure of the causes.
- psanford 6y agoIt was running out of memory trying to compile `leim/ja-dic/ja-dic.el`. A segfault sounds like a different issue.
- fredsanford 6y agoOh how I wished for something like this about 5 years ago... If I ever get over that experience I know where I'll start anew.
- bla3 6y agoHere's how to do this if you also want to have parts in Java: https://www.hanshq.net/command-line-android.html https://www.hanshq.net/command-line-android.html
- Ui1ahcex 6y agoThat's awesome :) I'm not sure I'm going to use it, though, as it still requires to install a lot of android stuff on the development computer. Something I find to work well to make apps for myself on my phone without requiring the android ndk : using termux. I have installed nginx within it, then I write react apps that I load in my mobile browser and they make http request to golang apps that run in termux as well. The first time I experimented with that, I thought it would drain my battery, but it turns out I don't see any difference (I don't use a database, though, my apps work with files).
- jniedrauer 6y agoIs there a reason you use nginx instead of serving the frontend using a Go static fileserver?
- Ui1ahcex 6y agoOnly contextual reasons : I have already nginx to serve a few backend-less react apps and static files, so this was less code to write.
- solarkraft 6y agoI'm a huge CNLor fan. My favorites are the WiFi signal strength mapping (it quite shaped my understanding of RF communication) and ColorChord. No! Euclid! (which has hot reloading in C!), glass PCBs and oscilloscope images through a VGA port (along with all the ESP8266 stuff) are also great. If you like Applied Science you'll like this dude. I highly recommend you to check out his YouTube channel: https://www.youtube.com/channel/UCG7yIWtVwcENg_ZS-nahg5g https://www.youtube.com/channel/UCG7yIWtVwcENg_ZS-nahg5g
- pengaru 6y agoYou can similarly develop in C for Android using SDL2. Basically your app becomes a .so with a thin java shim SDL2 provides that calls into your .so and you're off to the races. https://wiki.libsdl.org/Android https://wiki.libsdl.org/Android
- pedrohlc 6y agoIn 2013 a friend and I created an SDK for building games with GNOME's Vala (that transpires to C with GLib), SDL2 and Chipmunk physics for Android, Linux, and Windows (all within the same codebase). It worked flawless, I even ported a XNA example. It may be useful for someone today: https://github.com/avalanche-games/avalanche/wiki/Getting-Started-(for-Linux-users)#cross-compiling-for-android-only-different-steps https://github.com/avalanche-games/avalanche/wiki/Getting-St...
- seba_dos1 6y agoSame with Allegro 5.
- emilfihlman 6y agoThis seems super interesting! I've long been saddened at the requirement of Java to write applications for Android and now it's fixed! This should allow lots of interesting (art)projects and whatnot!
- benibela 6y agoI wrote my Android app in Pascal Although I used Java to create the layouts/views. But I have started porting some things to Kotlin. Nearly finished replacing all the Java I also had to buy a new computer. The Pascal IDE worked great on my ten year old laptop; the Java/Kotlin IDE became so slow, it was unusable. It is surprising that Java can run on a phone, when it cannot run on my development system
- realharo 6y agoIf you mean Android Studio being slow (and the Android build system in general, with its many transform steps), that has little to do with Java itself. Even IntelliJ IDEA, built on the same platform is much, much faster than AS (with non-Android projects). Android Studio is just a slow IDE. 16 GB RAM and a good CPU is the absolute bare minimum for it these days for any non-trivial project, especially if you want to run the emulator as well.
- benibela 6y agoYes, Android Studio (and the emulator) The Android app development went from my first laptop with 4GB RAM and HDD to my second laptop with 8GB RAM and HDD to my current, third laptop with 16 GB RAM and SDD. But all the laptops were really fast. I started writing Pascal code with Delphi under Windows 98 on a 166 MHz PC with 64 MB RAM or something, and it was fast enough
- realharo 6y agoI'm pretty sure Delphi also didn't have the same level of code navigation and inspections across the entire codebase (including resources, even from 3rd party libraries), or a built-in CPU/memory/network/energy profiler of the same quality, or layout previews that actually reflect the written code. Android Studio is still slow even by today's standards, but that is not an apples-to-apples comparison. There are also some ways to make incremental debug builds faster (can be brought down to a few seconds for smaller changes, gradle can show which step takes how long).
- realharo 6y agoPlenty of games used to be written like this, where you basically had a C (or C++) OpenGL ES platform independent code base, and then a bunch of small platform-specific parts for the different OSes. They did usually have tiny pieces of code in platform-native language though. I even wrote a game like that back in 2012 (originally for Samsung's bada OS, later ported to many things) https://github.com/peterholak/rail-climber https://github.com/peterholak/rail-climber (it says 2016 on github, but the code is much older). One great thing about it is, when you have a desktop target, you can compile/test/debug things much faster (including things like level files), and only do a mobile build every once in a while to check that it still all works as it should. Almost like a full featured game engine.
- s3cur3 6y agoThis is what we do with X-Plane. It’s really, really nice to be able to debug code destined for mobile on the desktop sim first. It cuts the iteration time down by a huge factor.
- andoma 6y agoI've been building a native-only Android app for testing various aspects of a cross-platform (ie, Android and iOS) library we use at dayjob. Building and running with Makefile feels so liberating and is so d*mn fast compared to the Android Studio and Gradle molasses. Can recommend.
- haberman 6y agoLooks to be based on https://github.com/cntools/rawdraw https://github.com/cntools/rawdraw, "CN's Fundamental Graphics Library", though I don't know who CN is.
- synackrst 6y agoThat's by the same author (CNLohr), but in an organization rather than a personal account.
- matheusmoreira 6y ago> A little bit of this also has to do to stick it to all those luddites people on the internet who post "that's impossible" or "you're doing it wrong" to Stack Overflow questions... > Requesting permissions in the JNI "oh you have to do that in Java" or other dumb stuff like that. > If you want a bunch of examples of how to do a ton of things in C on Android that you "need" java for, scroll to the bottom of this file: > https://github.com/cntools/rawdraw/blob/master/CNFGEGLDriver.c https://github.com/cntools/rawdraw/blob/master/CNFGEGLDriver... > it shows how to use the JNI to marshall a ton of stuff to/from the Android API without needing to jump back into Java/Kotlin land. I respect this attitude but it's wrong to say Java isn't being used or executed. The truth is Java code is being called from C. jclass ClassActivity = env->FindClass(envptr, "android/app/Activity"); jmethodID MethodrequestPermissions = env->GetMethodID(envptr, ClassActivity, "requestPermissions", "([Ljava/lang/String;I)V"); env->CallVoidMethod(envptr, activity, MethodrequestPermissions, perm_array, 0); This code directly interfaces with the Java virtual machine. It obtains a handle to a Java class and a handle to one of its methods. Then it instructs the virtual machine to invoke the method with parameters supplied by code written in C. I searched the repository for JavaVM and JNI_CreateJavaVM and got no results. So this code is being dynamically loaded by a pre-existing Java virtual machine at runtime.
- 411111111111111 6y ago> I respect this attitude but it's wrong to say Java isn't being used or executed. The truth is Java code is being called from C. He isn't wrong, because he didn't say that. It's admittedly very close to that, but not quiet. You just don't have to use Java to ask for permission. If that ultimately still executes Java code... Well, he didn't say.
- matheusmoreira 6y agoThe author wrote: > it shows how to use the JNI to marshall a ton of stuff to/from the Android API without needing to jump back into Java/Kotlin land The C code "jumps back into Java/Kotlin land" when it calls the CallVoidMethod function.
- g_p 6y agoThis is quite interesting to see, as I recall experimenting with something similar a few years ago. In that case, borne out of anger and frustration from how slow Android was at some things, I complained to one of the engineers I had brought onboard about how a 1970s computer was vastly more responsive and performant (to the user's perspective) than these modern (in ~2016) Android devices. We ended up building a prototype of a vertical scrolling ListView, implemented entirely in C, implementing all the "normal" functionality required to allow Android to use it. I believe my initial frustration was borne from performance tracing of an app we were developing, and finding the amount of time spent in nested constructors and other kinds of inherited object-oriented bloat was actually impacting on the user perceived performance (users said it was slow to scroll). My hypothesis was that if the functionality itself was implemented in C, properly and efficiently, with none of these extra layers of abstraction, it would be faster. That was definitely validated. I don't recall the figures now, but you could scroll a list with no perceptible latency as fast as you liked, with no stutter. The performance traces were nice and normal. It was smooth and fast. Mobile apps were something I got far away from for a while, but every time I return, I'm disappointed to see an ecosystem of re-inventing the same (boilerplate) wheel, and running the same code. For the number of layers of abstraction I saw the OS burning cycles on, it is absurd to me why the "end-developer" is left to do so much boilerplate coding! Surely at least one of those layers of abstractions could get developers to a place of implementing their own logic, rather than platform-specific logic. Case in point, the Android activity lifecycle (!)
- nexuist 6y agoIt is a bit confusing why developers are still expected to implement entire UIs themselves. Almost every app today that's not a game uses the same exact layout: tables and grids. As a mobile app developer I could save so much time by having the OS ask me what I want to put in these tables or grids, rather than having to implement the UITableViews and ListViews verbatim in almost every single project. Since we're on this topic: Why is there still no native user login/registration widget? Why can't the user just press a button to have the OS autofill and save all of their information across all of their apps? I know someone is going to say you can already do this, but the truth is, you still can't do it reliably enough to make it the first option. Sure, password manager extensions are supported by the OS, but I still have to go through whatever native interface my password manager wants to show me before I can get close to having a form filled out. In most cases, for most form fields, I am not prompted to fill anything out unless I'm in a web browser. It's a massive waste of time and developer energy considering these forms almost universally require the same input: name, profile icon, email, password.
- musicale 6y agoSomeone should invent a mobile platform where you can just write apps in natively compiled C/C++/ObjC. You know, kind of like the old Palm, WebOS, or iPhone platforms.
- pixelrevision 6y agoThis is still a perfectly valid thing to do on iOS
- snazz 6y agoIt's technically possible to write an entire iOS app in straight C, although it's not advised: https://stackoverflow.com/questions/10289890/how-to-write-ios-app-purely-in-c https://stackoverflow.com/questions/10289890/how-to-write-io...
- flukus 6y agoThat's basically the Librem 5 (https://puri.sm/products/librem-5/ https://puri.sm/products/librem-5/) and to me the most exciting thing about it.
- gridlockd 6y agoThe sad part is that since mobile development has now drifted so far away from that ideal, porting existing iOS/Android applications to the Librem isn't trivial. You could of course have an Android runtime on the Librem...
- flukus 6y agoThat's a good fallback for everyone's $ANDROID_APP that can't live without. But for the most part I'd be fine with not having that tire fire ported anyway, those apps are likely to be slow, suck battery and not conform at all to the native look and feel.
- harry8 6y agoNokia N900 was a beautiful thing. So beautiful it had to be killed.
- ZoomZoomZoom 6y agoThat's super cool. Would love to see someone using this as a base for KOReader (essential app, in my book) which has an unusual architecture for android, being written mostly in lua. Would this permit to compile the APK for ancient android versions, like 2.1?
- Snelius 6y agoYes, it's possible of course. But we need to make "native" android GUI which entire JAVA framework. :(
- Mizza 6y agoThis. Rocks. I quit mobile development after too many Java-induced meltdowns. This just renewed my interest in it. Quick ideas: - Demoscene potential!! - Needs a C-based Android UI lib! - Musical potential! VCV Rack for Android?! - Great for ports!
- fulafel 6y agoAny experiences of building Android apps by just generating code from your favourite productive language on the desktop? Any ideas if this would be a better target than Java for that?
- esamatti 6y agoI wonder if the same method could be used to write Android Apps only with Rust?
- Dig1t 6y ago"A little bit of this also has to do to stick it to all those luddites people on the internet who post "that's impossible" or "you're doing it wrong" to Stack Overflow questions... Requesting permissions in the JNI "oh you have to do that in Java" or other dumb stuff like that. I am completely uninterested in your opinions of what is or is not possible. This is computer science. There aren't restrictions. I can do anything I want. It's just bits. You don't own me." This is so funny and so relatable.
- mrb 6y agoReal life example of Android app weighting 31 kB: https://play.google.com/store/apps/details?id=jp.rallwell.siriuth.touchscreentest https://play.google.com/store/apps/details?id=jp.rallwell.si... No idea what framework the author used to accomplish that.
- gridlockd 6y agoYou don't need a framework, you can just use the interfaces directly. You know, like a framework.
- akavel 6y agoI'm working on a somewhat similar development, where I however wrote an actual assembler for .dex (Dalvik bytecode) files, which gives access to all the Android APIs and allows defining your own subclasses (e.g. new GUI components etc.) For example, I can drive a regular SurfaceView in a multithreaded way. I plan to speak about it on the upcoming Online Nim Conference on Jun 6. You can see a "teaser" that is an old version of the project (initial PoC) here: https://forum.nim-lang.org/t/4840 https://forum.nim-lang.org/t/4840
- kragen 6y agoThis is really inspiring! Would an app built this way have problems getting approved for F-Droid?
- DroitThemes 6y agoGreat article
- valera_rozuvan 6y agoHaving the possibility to write C and have it run on embedded hardware - it feels extremely empowering. Because the usual way (as intended by mobile architecture developers) of writing mobile apps introduces some overhead. Also, this project allows for the slow introduction of the vast C ecosystem (libraries, frameworks) to the Android landscape. Just think of all the possibilities!
- 1602 6y agoIt would be interesting to know how to make something like that integrated with sensors and services OS offers: activity detection, geolocation, foreground tasks, scheduled alarms, etc.
- gridlockd 6y agoWith some exceptions, "you have to do it in Java" or "use JNI" sadly is still the correct answer.
- marco_craveiro 6y agoVery interesting! On this topic, there is also Boden [1] - though it has been a little bit quiet of late... [1] https://github.com/AshampooSystems/boden https://github.com/AshampooSystems/boden