News for hackers. Stuff that matters.
Re:Rust in the Android Platform by thesuperbigfrog on Tuesday April 06, 2021 @03:40PM (#26716435)
>> For the past 18 months we have been adding Rust support to the Android Open Source Project, and we have a few early adopter projects that we will be sharing in the coming months. Scaling this to more of the OS is a multi-year project. Stay tuned, we will be posting more updates on this blog.

Patiently waiting for the Rust APIs to arrive . . .

Java feels so heavy, so getting to use Rust would be a breath of fresh air.

(Kotlin has been better, but it also requires the JVM.)

Re:Rust in the Android Platform by nindalf on Tuesday April 06, 2021 @03:45PM (#26716495)
The third paragraph says

> Managed languages like Java and Kotlin are the best option for Android app development. These languages are designed for ease of use, portability, and safety.

You're not going to write Android Activities in Rust. Nothing is stopping you from adding Rust code to your Java/Kotlin app though.

Re:Rust in the Android Platform by jeroenhd on Tuesday April 06, 2021 @04:28PM (#26716986)
There's already an API for native activities. While I doubt that Google will be the one to release the framework to write Rust applications, there's no reason why it would be impossible to interface app-level Rust with the Android SDK.

The big problem with such a setup, of course, is that the application itself would be running inside a JVM/Dalvik VM, so the app would constantly be switching between Java and native, which can't be great for performance. However, it can be done; thankfully, Chrome and Firefox aren't completely compiled to byte code, the JIT and rendering code is written in native code. With Mozilla's use of Rust in their browser, that means that there's already a significant Rust runtime running on Android, at least.

If the app uses Rust and a library to hook the draw call, it should be fairly easy to write the app like a video game and render everything in Rust. This would come bundled with a tonne of accessibility problems, of course, as every rendered control would be a non-standard one.

Re:Rust in the Android Platform by throwaway189262 on Tuesday April 06, 2021 @11:58PM (#26720442)
Java is a better fit for high level code, and you can already use Rust where performance matters.

I've put a lot of time into learning Rust and I still reach for Java for high level stuff. GC is just wonderful for developer productivity. Lifetimes are way better than C but still a PITA.

Especially when you mix with threading, you end up using Reference Counting anyways which is just shitty GC.

Re:Rust in the Android Platform by asveikau on Tuesday April 06, 2021 @09:16PM (#26719446)
I think another problem is that native binaries for multiple architectures would grow the binary, one per arch; aarch64 is not the only thing android runs on. Maybe some people would also forget that and not bother targeting others.
Re:Rust in the Android Platform by rbanffy on Tuesday April 06, 2021 @07:17PM (#26718553)
Honestly, between the magic Kotlin adds on top of Java and Rust, I prefer Rust. At least it’s not doing stuff behind my back.
Re:Rust in the Android Platform by bobthebuilders on Tuesday April 06, 2021 @07:42PM (#26718753)
C++ is better. Rust is a cargo cult language (same with Kotlin, Kotlin especially is a corporate language to sell more IDEs and Google was bribed by JetBraine to adopt it). Rust is just a bad attempt at reinventing the wheel.
Re:Rust in the Android Platform by howinteresting on Tuesday April 06, 2021 @09:32PM (#26719560)
Rust is a fantastic attempt at reinventing C++. It works really well. I've been writing production code in it for the last few years and it's an utter delight to use.

I maybe break out rust-gdb once a year. My code has fewer bugs than ever, thanks to extensive use of the high-quality modeling tools (types and lifetimes) provided by the language. My main project for the last year has had a grand total of one logic bug that was caught after release.

It also has an inclusive community: at the last RustConf, half the speakers were nonbinary and/or transgender. I fit both those criteria and feel more welcome in the Rust community than in that of any other language, meaning that I can do my best work in Rust.

Re:Rust in the Android Platform by carlhjerpe on Tuesday April 06, 2021 @09:50PM (#26719682)
Considering way less than 50% of developers are nonbinary/transgender, don't you get the impression that they're then trying to fill quotas rather than picking the best speakers? Not saying they weren't the best, just that either rust is a very unique community or something is "up" with the selection.

Just to clarify, I'm asking with good intent.

Re:Rust in the Android Platform by howinteresting on Tuesday April 06, 2021 @10:16PM (#26719857)
There's a selection effect: people like me experience so much ambient hostility in society that we gravitate towards communities that welcome us and that already have people like us in leadership positions. So the correct metric is not % of all developers as a whole but just the ones involved in the Rust community.

The Rust community has many nonbinary and transgender people in it, and speakers are selected from the community. The selection process for RustConf is blind so the talks were picked for their merit: it just so happened that half of the speakers weren't cis.

> rust is a very unique community

Yes, this is true in my experience.

Re:Rust in the Android Platform by carlhjerpe on Tuesday April 06, 2021 @10:48PM (#26720054)
I've been thinking about a good answer, but these days it's a fine line to talk about these things. I'm happy the rust community is so open while being widely successful. Might shine some light into the rest of the world. I'm from Sweden,where this kind of hostility probably exists, but I never see it (gay people are very well accepted, trans I'm not as certain, only have a pool of 1 friend so not enough data).
Re:Rust in the Android Platform by howinteresting on Wednesday April 07, 2021 @12:19PM (#26727083)
I'm not Swedish so I don't know much about what it's like to be a Swedish trans person.

One thing I will say is that my general impression from afar is that Europeans are generally a lot more tolerant of the state interfering in personal identity issues than the liberal parts of the US. Iceland used to have a fixed list of approved baby names for example, something unimaginable in the US.

The situation was made better in 2019: https://www.icelandreview.com/news/icelandic-names-will-no-l...

Re:Rust in the Android Platform by Rule35 on Thursday April 08, 2021 @08:14PM (#26745362)
I think the discussion gets a bit blurred around what acceptance means.

It varies from acceptance of an adult just being trans, of trans babies, of schools preaching medical transition without parent approval, and of issue of bathroom usage and sex-segregated prisons.

Regardless of the political questions presumably Swedes are fairly liberal about someone's personal choices.

Re:Rust in the Android Platform by dabraham1248 on Tuesday April 06, 2021 @10:27PM (#26719920)
It might be that people from disadvantaged communities _need_ to have more talent & insight to become experts than people like me (straight, white, male, speaks English, went to college, ...) and (I assume) you. And even more so to become _recognized_ experts. Also, they might feel more of a need to demonstrate that they should be allowed in the "club".

Put another way and exaggerated wildly for hopeful clarity, if person A became an expert despite being shoved away, and person B was wooed into the group, had tutors, friends,... I'd be more interested in A's talk than B's.

Re:Rust in the Android Platform by carlhjerpe on Tuesday April 06, 2021 @10:50PM (#26720062)
Good insight, it seems like the reply below said the talks were submitted blindly though, so that's even better. They're just accepted and doing really well!
Re:Rust in the Android Platform by anoncake on Wednesday April 07, 2021 @03:38AM (#26721517)
Somehow, queer people are subconsciously attracted to each other (not necessarily in a romantic or sexual way). While 50% non-cis is surprising, when we are represented we tend to be overrepresented.
Re:Rust in the Android Platform by wffurr on Tuesday April 06, 2021 @09:10PM (#26719417)
Rust is a bad attempt at reinventing C++? That's a unique take.

Memory safety is a big deal for writing secure programs, which has been repeatedly shown to be impossible to do at scale in C++. Provably safe parallelism and concurrency is key for performance on multi core devices, also extremely difficult to do safely and correctly in C++.

If C++ is the wheel being reinvented, it's not much of a wheel.

Re:Rust in the Android Platform by throwaway189262 on Wednesday April 07, 2021 @12:03AM (#26720468)
No way. Rust is great, trust me.

I'm not even going to say why because it would be a huge rant. It improves on C++ in virtually every way. Every feature you have in C++, Rust does it better. Every feature you wish C++ had, Rust has it.

I agree with you on Kotlin. It's better than Java on paper, but in much the same ways Groovy already was. And the advantages are shrinking in new versions of Java, especially if you use Lombok.

Re:Rust in the Android Platform by lenkite on Wednesday April 07, 2021 @04:01AM (#26721686)
"Every feature you have in C++, Rust does it better"

How does one perform generic fold expressions in Rust lang ? (Possible in C++ 17 and above)

https://foonathan.net/2020/05/fold-tricks/ https://en.cppreference.com/w/cpp/language/fold

Re:Rust in the Android Platform by pixel_fcker on Wednesday April 07, 2021 @05:27AM (#26722284)
Macros baby
Re:Rust in the Android Platform by lenkite on Wednesday April 07, 2021 @06:45AM (#26722973)
AFAIK, macros are only applicable to a subset of problems. Anyways, I found that variadic generics is a draft RFC: https://github.com/rust-lang/rfcs/issues/376
Re:Rust in the Android Platform by scoutt on Wednesday April 07, 2021 @10:27AM (#26725437)
>Every feature you have in C++, Rust does it better.

IDK. I miss variadic functions (not macros) and being able to do certain simple things without resorting to strange paradigms, like arbitrarily closing an std::fs::File without needing to go out of scope or calling drop. Things like that.

Re:Rust in the Android Platform by 3836293648 on Wednesday April 07, 2021 @12:10PM (#26726959)
How is calling drop any different from calling close?
Re:Rust in the Android Platform by scoutt on Wednesday April 07, 2021 @01:34PM (#26728097)
For the practical case is not different. But is not coherent. It’s weird. And opening/closing files is an historical concept.

Why “drop” is the opposite of “open”? Let’s change “open” for “lift” or “notdrop”.

Re:Rust in the Android Platform by nl on Wednesday April 07, 2021 @03:11AM (#26721358)
Google was bribed by JetBraine to adopt it

It's hard not being sarcastic in reply to this. But no, this did not happen.

Re:Rust in the Android Platform by berkes on Tuesday April 06, 2021 @03:54PM (#26716607)
While it feels terribly hacky, one could probably write an Android-wrapper around Rust in Java. A rather big undertaking, I'd guess, since it means exposing all APIs to rust somehow.

An other approach would be to employ one of many thin webview wrappers, in which your app is HTML+Js+CSS. When such a webview wrapper supports WASM, the JS part could be written in rust.

Not something I'd do other than "to prove it can be done".

Re:Rust in the Android Platform by raphlinus on Tuesday April 06, 2021 @04:16PM (#26716839)
The "cargo apk" tool already exists to bundle Rust code into a NativeActivity that can be run without needing to write any additional Java code. The API surface so provided is more limited than the full toolkit, but could be useful in many domains. It's currently mostly used for games. I am using it for the Android port of piet-gpu, and it's working pretty well.
Re:Rust in the Android Platform by Argorak on Tuesday April 06, 2021 @04:07PM (#26716741)
The biggest problem here is that in Android, Java "owns" the app, so you will have to invert a lot of lines to make "develop a whole app in Rust" feel awesome.

But, TBH, I think Kotlin and Java are _indeed_ fine application languages.

What Rust is already quite often used for is for libraries that work on both iOS and Android. For a public example, see the Glean SDK: https://mozilla.github.io/glean/book/index.html

Re:Rust in the Android Platform by berkes on Wednesday April 07, 2021 @04:44AM (#26721982)
> But, TBH, I think Kotlin and Java

I agree wholeheartedly.

But there will be a demographic of people who know Rust and not Java, but who might want to build an app. Somewhat similar to how there is an apparent need for "HTML+JS+CSS"-devs to build native apps.

Re:Rust in the Android Platform by Argorak on Wednesday April 07, 2021 @05:46AM (#26722495)
Well, this is always a tradeoff: the cost of being away from the mainstream may outweigh familiarity with a programming language at some point.

I'm not sure if the argument for "HTML+JS+CSS" is purely familiarity, there's also technical arguments like portability and future-compatibility. But I _do_ hope that the portability story will become better in the coming years beyond just this stack.

Re:Rust in the Android Platform by skybrian on Wednesday April 07, 2021 @01:54AM (#26720997)
Apparently the Android team is still not recommending Flutter. Hmm.
Re:Rust in the Android Platform by pjmlp on Wednesday April 07, 2021 @03:14AM (#26721375)
Why should they? Jetpack Compose is their defensive answer against Flutter.
Re:Rust in the Android Platform by steveklabnik on Tuesday April 06, 2021 @03:44PM (#26716483)
They say earlier

> now supports the Rust programming language for developing the OS itself.

So, this seems to be about in Android, not building apps for Android.

Guess we'll see with more posts!

Re:Rust in the Android Platform by tialaramex on Tuesday April 06, 2021 @07:35PM (#26718690)
Right, as with other complicated low level systems, Android sees an annoying but seemingly unending stream of bugs that would be either impossible or unlikely if you used Rust.

I expect replacing low-level C++ with Rust to result in improved platform reliability as well as reduced security bugs.

For every weird memory safety bug that is usefully exploitable there are a hundred more that just mysteriously break things and may be very difficult for a human to spot. My WebAuthn tests broke on my Pixel 2 on Saturday, I spent about an hour trying to debug the problem, wondering why it was happening now but not before and why it didn't affect other platforms - then I rebooted the phone and it went from reporting a "NotReadableError" about the secure local storage to just working.

Re:Rust in the Android Platform by larsberg on Tuesday April 06, 2021 @04:49PM (#26717212)
That's exactly right. Today, the focus is on making it possible to use Rust to write pieces of the Android platform itself.

Certainly, there are some _apps_ on Android today that use Rust (shout-out to my Firefox friends!), but that's not something I'm investing in right now.

Re:Rust in the Android Platform by k__ on Tuesday April 06, 2021 @04:13PM (#26716810)
Shouldn't Rust be able to hook into the NDK already anyways?
Re:Rust in the Android Platform by david_allison on Tuesday April 06, 2021 @04:24PM (#26716938)
Re:Rust in the Android Platform by steveklabnik on Tuesday April 06, 2021 @04:14PM (#26716819)
I haven't personally done it, but given Rust can call C and be called like C, I don't know why not. I do know that Rust is being used in Android apps like Cloudflare's 1.1.1.1 app, but I don't know if those make ndk calls or not.
Re:Rust in the Android Platform by username_my1 on Tuesday April 06, 2021 @04:31PM (#26717027)
I think you can do 60fps on android with kotlin easily ... in fact you can do that with RN (if you're careful).

Rust is not a UI programming language and shouldn't be.

but writing your own native modules in rust might be fun (VR, Audio processing, AI ...)

Re:Rust in the Android Platform by carlhjerpe on Tuesday April 06, 2021 @10:57PM (#26720107)
What prevents Rust(as a language) from being used to write UI? Qt is implemented in C++, which rust seems to aim to replace.

Rust seems like an excellent contender for the future.

Re:Rust in the Android Platform by pjmlp on Wednesday April 07, 2021 @03:33AM (#26721481)
The borrow checker.

UIs tend to have designs that require graph based structures and self referencial references, so in the end you get either lots of Rc<RefCell<>> and clone() calls, or have to bind the whole stack to fit into Rust's memory model.

Have a look at,

https://raphlinus.github.io/rust/druid/2019/10/31/rust-2020....

https://raphlinus.github.io/rust/druid/2020/09/28/rust-2021....

Re:Rust in the Android Platform by steveklabnik on Wednesday April 07, 2021 @12:40AM (#26720668)
Many UI toolkits use shared ownership and callbacks, and that's annoying in Rust. Some bindings make it work. There's a bunch of folks working on interesting architectures. But the classic styles just clash with Rust's default way of doing things.
Re:Rust in the Android Platform by throwaway189262 on Wednesday April 07, 2021 @12:06AM (#26720488)
Multi threading sanely requires Arc, and reference counting is a crappy form of GC.

If you have enough threads passing stuff around and can deal with the ~50% performance loss between Java and Rust, it's much easier to use a language with GC.

There's talk of adding a quality GC to rust and dedicated GC types. If/when that happens, Rust will be a contender for a bunch of higher level systems

Re:Rust in the Android Platform by stusmall on Tuesday April 06, 2021 @04:05PM (#26716714)
If official* APIs come then they will probably come via ndk. It's been a while since I've heavily done Android dev but historically those APIs have lagged behind a lot. At one point there was a major push to catch them up but they were still pretty second class.

I'd still much rather use the standard SDK and kotlin and I absolutely adore rust.

*There are already unofficial ndk crates for rust.

Re:Rust in the Android Platform by kitsunesoba on Tuesday April 06, 2021 @08:04PM (#26718911)
I don’t mind Kotlin itself so much, but the Java baggage it brings and the general weirdness that is possible when developing for Android in Java/Kotlin can get irritating. If not Rust, it’d be nice to be able to instead write Swift or some other modern compiled language suitable for UI work.

This is actually an area where iOS is more flexible. Because UIKit is C-accessible, it’s feasible to develop bindings to it for other languages, and even autogenerate them. It’s too bad the same isn’t true of Android Framework.

Re:Rust in the Android Platform by throwaway189262 on Wednesday April 07, 2021 @12:09AM (#26720505)
Newer versions of Java are better, but Android is stuck on a hacky version of Java 7 from a decade ago.

Google is planning to fix this eventually.

Android's API is also atrocious.

Re:Rust in the Android Platform by souldeux on Tuesday April 06, 2021 @03:44PM (#26716489)
I don't know enough about Kotlin to know if it's useful or not to point out Kotlin/Native: https://kotlinlang.org/docs/native-overview.html
Re:Rust in the Android Platform by The_rationalist on Tuesday April 06, 2021 @04:32PM (#26717034)
GraalVM native is generally better than Kotlin native as it maintain Java-kotlin interop. Kotlin native is more for niche use cases like objective-C/Swift interop.
Re:Rust in the Android Platform by pjmlp on Wednesday April 07, 2021 @03:34AM (#26721490)
Kotlin/Native is also being redone after being proven that not following the same memory model as the JVM was a bad decision.