Crates.io | android_base |
lib.rs | android_base |
version | 0.1.0 |
source | src |
created_at | 2018-12-26 04:57:01.392254 |
updated_at | 2018-12-26 04:57:01.392254 |
description | A base for making android applications in rust with piston and glutin-window that simplifies android development |
homepage | |
repository | https://github.com/OptimisticPeach/android_rs_base |
max_upload_size | |
id | 103818 |
size | 20,248 |
- A base to create android applications with ease
android_base
provides an api to develop applications in rust for android without worrying about android specific details like opengl implementations or the event loop, all while having its exposed functions be very abstract in terms of what you can do
This is basically the same example as shown in the piston getting started page, except with inverted colours.
Note that this example requires some basic knowledge of rust and working with android
Getting started:
Make sure you've followed the getting set up
section of this readme beforehand to setup the environment/dependencies
To set up the cargo project follow the standard procedure you'd go through with for any other binary rust application:
cargo new android_example --bin
in the directory you want to create the project inandroid_example
folder, edit the Cargo.toml
file and add android_base = "0.1.0"
as a dependencyTake note that this is the only dependency you are required to add; android_base automatically imports the right versions of all the crates you'll need in the form of a prelude
#![feature(uniform_paths)]
//automatically imports right versions of crates
use android_base::*;
use graphics::*;
use android_glue::*;
pub struct App {
rotation: f64
}
impl App {
pub fn new() -> Self {
Self {rotation: 0.}
}
}
impl AppImpl for App {
fn draw(&mut self, c: Context, gl: &mut GlGraphics, args: &RenderArgs){
clear([1., 0., 0., 1.], gl);
let transform = c.transform
.trans(args.width as f64 / 2., args.height as f64 / 2.)
.rot_rad(self.rotation)
.trans(-75., -75.);
rectangle([0., 1., 0., 1.], [0., 0., 150., 150.], transform, gl);
}
fn update(&mut self, args: &UpdateArgs) {
self.rotation += args.dt;
}
fn cancel_poll(&self) -> bool {
false
}
}
fn main() {
enable_backtrace();
let mut container = AppContainer::init(App::new(), AppConfig::new());
container.run();
}
#![feature(uniform_paths)]
at the startandroid_base
and two important dependencies in making an android app with piston; graphics
and android_glue
. For now I'll just assume you know what graphics
does, but android_glue
serves as the, well, glue between your app and android's eventsdraw
method.new
function for simplicity, but we could just use the constructor instead, either way worksAppImpl
, which holds all the important functions that can be implemented for your app:
draw
: This holds all the drawing functionality for your app, and takes the parameters supplied from GlGraphics
' draw method, so that android_base
takes care of getting ready to draw, and you just drawupdate
: This holds information when your app is needed to draw; this is basically just a passthrough from piston::event_loop::Events::next()
when it emits an update flag.cancel_poll
: This polls your app if it should stop running, when it returns true, it will stop executing, even if it is only meant to render a numbered amount of frames (Look at the part on configuration below)src/app_implementor.rs
RUST_BACKTRACE
to 1 to allow debugging backtraces; this just makes debugging easierAppContainer
with our app, and a default configuration. Configuring the AppContainer
with an AppConfig
can make it run only a certain number of frames, or call a reset function on run()
run
function which will run our app until cancel_poll
returns true (Which it won't because it's set to false)cargo-apk
Cargo.toml
:[package.metadata.android]
build_targets = [ "arm-linux-androideabi" ]
Note that this example will follow the steps for compiling to a real device running arm linux android and will not detail how to start an emulator or even how to install one
$ rustup target add arm-linux-androideabi
adb tcpip 5555
adb connect <your ip address>
to connect over wifi to make things easier for repeated runscargo-apk run
, and if it doesn't work:
adb logcat
and re-open your appenable_backtrace
in main
will print out a backtrace when panic!()
occurscargo install --git https://github.com/tomaka/android-rs-glue.git --rev 1d095846a687f873b6aed7d62ee32bc5a062e534 --force cargo-apk
Setting up your environment
but without re-installing cargo-apk because the published version is out of dateNote: This readme/setup/example is meant for users either on ubuntu or on on WSL (Which I personally use), I've found that windows directly doesn't work
Another note: due to a minor implementation detail in the latest version of
cargo-apk
is broken and you can't callcargo apk
and instead have to callcargo-apk
.
Another note: This crate includes a git clone of
opengles_graphics
which is a fork ofopengl_graphics
meant to work with opengles, but it hasn't been updated for over 2 years. I therefore updated its dependencies to accomodate for newer versions of other crates. I do not claim to have developed the entirety of it, and have only made minor changes, and instead give credit to Drakulix and the Piston team