Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Resolved shared library issues and expose C and C++ build tools #232

Merged
merged 7 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
.idea
*.iml
*.iml
CMakeFiles
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM rust:latest

RUN apt-get update
RUN apt-get install -yq openjdk-8-jre unzip wget
RUN apt-get install -yq openjdk-8-jre unzip wget cmake

RUN rustup target add armv7-linux-androideabi
RUN rustup target add aarch64-linux-android
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ the stdlib.

The build process works by:

- Using rustc to always compile your crate as a static library by:
- Using rustc to always compile your crate as a shared library by:
- Creating a custom CMake toolchain file and setting environment variables which expose the appropriate NDK provided build tools for use with the `cc` and `cmake` crates.
- Creating a temporary file in the same directory as your crate root. This temporary file serves as the crate root of the static library. It contains the contents of the original crate root along with an `android_main` implementation.
- Injecting some glue libraries in rust, which is used by `android_main` to perform initialization required by the `android_glue` crate and to call the `main` function of your crate.
- Using `ndk-build` provided by the NDK to to build a shared library.
- Linking to the `android_native_app_glue` library provided by the Android NDK. `android_native_app_glue` provides the entrypoint used by Android's `NativeActivity` that calls `android_main`.
- Compiling a forked version of `android_native_app_glue`. `android_native_app_glue` is originally provided by the NDK. It provides the entrypoint used by Android's `NativeActivity` that calls `android_main`.
- Linking using the NDK provided linker.

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

The command then builds the APK using the shared library, generated manifest, and tools from the Android SDK.
The command then builds the APK using the shared libraries, generated manifest, and tools from the Android SDK. If the C++ standard library is used, it adds the appropriate shared library to the APK.
It signs the APK with the default debug keystore used by Android development tools. If the keystore doesn't exist, it creates it using the keytool from the JRE or JDK.

# Supported `[package.metadata.android]` entries
Expand Down Expand Up @@ -199,3 +200,17 @@ max_sdk_version = 18
[[package.metadata.android.permission]]
name = "android.permission.CAMERA"
```

# Environment Variables
Cargo-apk sets environment variables which are used to expose the appropriate C and C++ build tools to build scripts. The primary intent is to support building crates which have build scripts which use the `cc` and `cmake` crates.

- CC : path to NDK provided `clang` wrapper for the appropriate target and android platform.
- CXX : path to NDK provided `clang++` wrapper for the appropriate target and android platform.
- AR : path to NDK provided `ar`
- CXXSTDLIB : `c++` to use the full featured C++ standard library provided by the NDK.
- CMAKE_TOOLCHAIN_FILE : the path to the generated CMake toolchain. This toolchain sets the ABI, overrides any target specified, and includes the toolchain provided by the NDK.
- CMAKE_GENERATOR : `Unix Makefiles` to default to `Unix Makefiles` as opposed to using the CMake default which may not be appropriate depending on platform.
- CMAKE_MAKE_PROGRAM: Path to NDK provided make.

# C++ Standard Library Compatibility Issues
When a crate links to the C++ standard library, the shared library version provided by the NDK is used. Unfortunately, dependency loading issues will cause the application to crash on older versions of android. Once `lld` linker issues are resolved on all platforms, cargo apk will be updated to link to the static C++ library. This should resolve the compatibility issues.
10 changes: 10 additions & 0 deletions cargo-apk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cargo-apk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ clap = "2.33.0"
itertools = "0.8.0"
dirs = "2.0.1"
failure = "0.1.5"
multimap = "0.5.0"
serde = "1.0.97"
toml = "0.5.1"

Expand Down
13 changes: 13 additions & 0 deletions cargo-apk/native_app_glue/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (C) 2016 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Loading