Giter Club home page Giter Club logo

android-rs-glue's Introduction

Become a patron

Usage

With docker

The easiest way to compile for Android is to use docker and the tomaka/cargo-apk image.

In order to build an APK, simply do this:

docker run --rm -v <path-to-local-directory-with-Cargo.toml>:/root/src tomaka/cargo-apk cargo apk build

For example if you're on Linux and you want to compile the project in the current working directory.

docker run --rm -v "$(pwd):/root/src" -w /root/src tomaka/cargo-apk cargo apk build

Do not mount a volume on /root or you will erase the local installation of Cargo.

After the build is finished, you should get an Android package in target/android-artifacts/app/build/outputs/apk.

Manual usage

Setting up your environment

Before you can compile for Android, you need to setup your environment. This needs to be done only once per system.

  • Install rustup.

  • Run rustup target add arm-linux-androideabi, or any other target that you want to compile to.

  • Install the Java JDK (on Ubuntu, sudo apt-get install openjdk-8-jdk)

  • Install Gradle.

  • Download and unzip the Android NDK

  • Download and unzip the Android SDK (under SDK Tools Only at the bottom)

  • Install some components in the SDK: ./android-sdk/tools/bin/sdkmanager "platform-tools" "platforms;android-18" "build-tools;26.0.1"

  • Install cargo-apk with cargo install cargo-apk.

  • Set the environment variables NDK_HOME to the path of the NDK and ANDROID_HOME to the path of the SDK.

Compiling

In the project root for your Android crate, run cargo apk build. You can use the same options as with the regular cargo build.

This will build an Android package in target/android-artifacts/app/build/outputs/apk.

Testing on an Android emulator

Start the emulator, then run:

cargo apk run

This will install your application on the emulator, then run it.
If you only want to install, use cargo apk install.

To show log run: cargo apk logcat | grep RustAndroidGlueStdouterr

Interfacing with Android

An application is not very useful if it doesn't have access to the screen, the user inputs, etc.

The android_glue crate provides FFI with the Android environment for things that are not in the stdlib.

How it works

The build process

The build process works by invoking cargo rustc and:

  • Always compiles your crate as a shared library.
  • Injects the android_native_app_glue file provided by the Android NDK.
  • Injects some glue libraries in Rust, which ties the link between android_native_app_glue and the main function of your crate.

This first step outputs a shared library, and is run once per target architecture.

The command then sets up an Android build environment, which includes some Java code, in target/android-artifacts and puts the shared libraries in it. Then it runs gradle.

Supported [package.metadata.android] entries

[package.metadata.android]

# The Java package name for your application.
# Hyphens are converted to underscores.
package_name = "com.author-name.my-android-app"

# The user-friendly name for your app, as displayed in the applications menu.
label = "My Android App"

# Path to your application's res/ folder. See `examples/use_icon/res`.
res = "path/to/res_folder"

# Virtual path your application's icon for any mipmap level. See `examples/use_icon/icon`.
icon = "@mipmap/ic_laucher"

# Path to the folder containing your application's assets. See `examples/use_assets/assets`.
assets = "path/to/assets_folder"

# The target Android API level.
# It defaults to 18 because this is the minimum supported by rustc.
android_version = 18

# If set to true, makes the app run in full-screen, by adding the following line
# as an XML attribute to the manifest's <application> tag :
#     android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen
# Defaults to false.
fullscreen = false

# Specifies the array of targets to build for.
# Defaults to "arm-linux-androideabi".
# Other possible targets include "aarch64-linux-android", 
# "armv7-linux-androideabi", "i686-linux-android" and "x86_64-linux-android".
build_targets = [ "arm-linux-androideabi", "armv7-linux-androideabi" ]

# The maximum supported OpenGL ES version , as claimed by the manifest. Defaults to 2.0.
# See https://developer.android.com/guide/topics/graphics/opengl.html#manifest
opengles_version_major = 2
opengles_version_minor = 0

# Adds extra arbitrary XML attributes to the <application> tag in the manifest.
# See https://developer.android.com/guide/topics/manifest/application-element.html
[package.metadata.android.application_attributes]
"android:debuggable" = "true"
"android:hardwareAccelerated" = "true"

# Adds extra arbitrary XML attributes to the <activity> tag in the manifest.
# See https://developer.android.com/guide/topics/manifest/activity-element.html
[package.metadata.android.activity_attributes]
"android:screenOrientation" = "unspecified"
"android:uiOptions" = "none"

android-rs-glue's People

Contributors

boguszaur avatar davidhewitt avatar dhild avatar diamondlovesyou avatar dlrobertson avatar dten avatar gw3583 avatar jonhoo avatar mark3982 avatar mbrubeck avatar mortimergoro avatar mwolting avatar oli-obk avatar ozkriff avatar readmecritic avatar remexre avatar skligys avatar solidsnack avatar tomaka avatar tomc1998 avatar wbigger avatar wvdschel avatar yoanlcq avatar ztdwu avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.