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

Commit 499e793

Browse files
authored
Merge pull request #223 from philip-alldredge/master
Cargo apk build system rework and other enhancements
2 parents c37ac76 + 7de68cc commit 499e793

File tree

39 files changed

+2343
-2131
lines changed

39 files changed

+2343
-2131
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
target/
2+
.idea
3+
*.iml

Dockerfile

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
1-
FROM tomaka/rust-android
1+
FROM rust:latest
22

3+
RUN apt-get update
4+
RUN apt-get install -yq openjdk-8-jre unzip wget
5+
6+
RUN rustup target add armv7-linux-androideabi
7+
RUN rustup target add aarch64-linux-android
8+
RUN rustup target add i686-linux-android
9+
RUN rustup target add x86_64-linux-android
10+
11+
# Install Android SDK
12+
ENV ANDROID_HOME /opt/android-sdk-linux
13+
RUN mkdir ${ANDROID_HOME} && \
14+
cd ${ANDROID_HOME} && \
15+
wget -q https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip && \
16+
unzip -q sdk-tools-linux-4333796.zip && \
17+
rm sdk-tools-linux-4333796.zip && \
18+
chown -R root:root /opt
19+
RUN yes | ${ANDROID_HOME}/tools/bin/sdkmanager "platform-tools"
20+
RUN yes | ${ANDROID_HOME}/tools/bin/sdkmanager "platforms;android-29"
21+
RUN yes | ${ANDROID_HOME}/tools/bin/sdkmanager "build-tools;29.0.0"
22+
RUN ${ANDROID_HOME}/tools/bin/sdkmanager --update
23+
24+
# Install Android NDK
25+
RUN cd /usr/local && \
26+
wget -q http://dl.google.com/android/repository/android-ndk-r20-linux-x86_64.zip && \
27+
unzip -q android-ndk-r20-linux-x86_64.zip && \
28+
rm android-ndk-r20-linux-x86_64.zip
29+
ENV NDK_HOME /usr/local/android-ndk-r20
30+
31+
# Copy contents to container. Should only use this on a clean directory
332
COPY . /root/cargo-apk
33+
34+
# Run tests
35+
RUN cd /root/cargo-apk/cargo-apk && cargo test --release
36+
37+
# Install Binary and remove source and build files
438
RUN cargo install --path /root/cargo-apk/cargo-apk
539
RUN rm -rf /root/cargo-apk
640

41+
# Make directory for user code
742
RUN mkdir /root/src
843
WORKDIR /root/src

README.md

Lines changed: 86 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
## With Docker
66

77
The easiest way to compile for Android is to use [Docker](https://www.docker.com/) and the
8-
[tomaka/cargo-apk](https://hub.docker.com/r/tomaka/cargo-apk/) image.
8+
[philipalldredge/cargo-apk](https://hub.docker.com/r/philipalldredge/cargo-apk/) image.
99

1010
In order to build an APK, simply do this:
1111

1212
```
13-
docker run --rm -v <path-to-local-directory-with-Cargo.toml>:/root/src tomaka/cargo-apk cargo apk build
13+
docker run --rm -v <path-to-local-directory-with-Cargo.toml>:/root/src philipalldredge/cargo-apk cargo apk build
1414
```
1515

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

1919
```
20-
docker run --rm -v "$(pwd):/root/src" -w /root/src tomaka/cargo-apk cargo apk build
20+
docker run --rm -v "$(pwd):/root/src" -w /root/src philipalldredge/cargo-apk cargo apk build
2121
```
2222

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

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

2727
## Manual usage
2828

@@ -31,13 +31,15 @@ After the build is finished, you should get an Android package in `target/androi
3131
Before you can compile for Android, you need to setup your environment. This needs to be done only once per system.
3232

3333
- Install [`rustup`](https://rustup.rs/).
34-
- Run `rustup target add arm-linux-androideabi`, or any other target that you want to compile to.
35-
- Install the Java JDK (on Ubuntu, `sudo apt-get install openjdk-8-jdk`).
36-
- Install CMake (on Ubuntu, `sudo apt-get install cmake`).
37-
- [Install Gradle](https://gradle.org/install/).
34+
- Run `rustup target add <target>` for all supported targets to which you want to compile. Building will attempt to build for all supported targets unless the build targets are adjusted via `Cargo.toml`.
35+
- `rustup target add armv7-linux-androideabi`
36+
- `rustup target add aarch64-linux-android`
37+
- `rustup target add i686-linux-android`
38+
- `rustup target add x86_64-linux-android`
39+
- Install the Java JRE or JDK (on Ubuntu, `sudo apt-get install openjdk-8-jdk`).
3840
- Download and unzip [the Android NDK](https://developer.android.com/ndk).
3941
- Download and unzip [the Android SDK](https://developer.android.com/studio).
40-
- Install some components in the SDK: `./android-sdk/tools/bin/sdkmanager "platform-tools" "platforms;android-18" "build-tools;26.0.1"`.
42+
- Install some components in the SDK: `./android-sdk/tools/bin/sdkmanager "platform-tools" "platforms;android-29" "build-tools;29.0.0"`.
4143
- Install `cargo-apk` with `cargo install cargo-apk`.
4244
- Set the environment variables `NDK_HOME` to the path of the NDK and `ANDROID_HOME` to the path of the SDK.
4345

@@ -46,7 +48,15 @@ Before you can compile for Android, you need to setup your environment. This nee
4648
In the project root for your Android crate, run `cargo apk build`. You can use the same options as
4749
with the regular `cargo build`.
4850

49-
This will build an Android package in `target/android-artifacts/app/build/outputs/apk`.
51+
This will build an Android package in `target/android-artifacts/<debug|release>/apk`.
52+
53+
### Compiling Multiple Binaries
54+
55+
`cargo apk build` supports building multiple binaries and examples using the same arguments as `cargo build`. It will produce an APK for each binary.
56+
57+
Android packages for bin targets are placed in `target/android-artifacts/<debug|release>/apk`.
58+
59+
Android packages for example targets are placed in `target/android-artifacts/<debug|release>/apk/examples`.
5060

5161
### Testing on an Android emulator
5262

@@ -72,62 +82,75 @@ the stdlib.
7282

7383
## The build process
7484

75-
The build process works by invoking `cargo rustc` and:
85+
The build process works by running rustc and:
7686

77-
- Always compiles your crate as a shared library.
78-
- Injects the `android_native_app_glue` file provided by the Android NDK.
79-
- Injects some glue libraries in Rust, which ties the link between `android_native_app_glue` and
80-
the `main` function of your crate.
87+
- Always compiles your crate as a static library.
88+
- Uses `ndk-build` provided by the NDK to to build a shared library.
89+
- Links to the `android_native_app_glue` library provided by the Android NDK.
90+
- Injects some glue libraries in Rust, which ties the link between `android_native_app_glue` and the `main` function of your crate.
8191

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

84-
The command then sets up an Android build environment, which includes some Java code, in
85-
`target/android-artifacts` and puts the shared libraries in it. Then it runs `gradle`.
94+
The command then builds the APK using the shared library, generated manifest, and tools from the Android SDK.
95+
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.
8696

8797
# Supported `[package.metadata.android]` entries
8898

8999
```toml
90-
[package.metadata.android]
100+
# The target Android API level.
101+
# "android_version" is the compile SDK version. It defaults to 29.
102+
# (target_sdk_version defaults to the value of "android_version")
103+
# (min_sdk_version defaults to 18) It defaults to 18 because this is the minimum supported by rustc.
104+
android_version = 29
105+
target_sdk_version = 29
106+
min_sdk_version = 26
107+
108+
# Specifies the array of targets to build for.
109+
# Defaults to "armv7-linux-androideabi", "aarch64-linux-android", "i686-linux-android".
110+
build_targets = [ "armv7-linux-androideabi", "aarch64-linux-android", "i686-linux-android", "x86_64-linux-android" ]
111+
112+
#
113+
# The following value can be customized on a per bin/example basis. See multiple_targets example
114+
# If a value is not specified for a secondary target, it will inherit the value defined in the `package.metadata.android`
115+
# section unless otherwise noted.
116+
#
91117

92118
# The Java package name for your application.
93119
# Hyphens are converted to underscores.
94-
package_name = "com.author-name.my-android-app"
120+
# Defaults to rust.<target_name> for binaries.
121+
# Defaults to rust.<package_name>.example.<target_name> for examples.
122+
# For example: for a binary "my_app", the default package name will be "rust.my_app"
123+
# Secondary targets will not inherit the value defined in the root android configuration.
124+
package_name = "rust.cargo.apk.advanced"
95125

96126
# The user-friendly name for your app, as displayed in the applications menu.
127+
# Defaults to the target name
128+
# Secondary targets will not inherit the value defined in the root android configuration.
97129
label = "My Android App"
98130

99-
# Path to your application's res/ folder. See `examples/use_icon/res`.
131+
# Path to your application's resources folder.
132+
# If not specified, resources will not be included in the APK
100133
res = "path/to/res_folder"
101134

102-
# Virtual path your application's icon for any mipmap level. See `examples/use_icon/icon`.
103-
icon = "@mipmap/ic_laucher"
135+
# Virtual path your application's icon for any mipmap level.
136+
# If not specified, an icon will not be included in the APK.
137+
icon = "@mipmap/ic_launcher"
104138

105-
# Path to the folder containing your application's assets. See `examples/use_assets/assets`.
139+
# Path to the folder containing your application's assets.
140+
# If not specified, assets will not be included in the APK
106141
assets = "path/to/assets_folder"
107142

108-
# The target Android API level.
109-
# It defaults to 18 because this is the minimum supported by rustc.
110-
# (target_sdk_version and min_sdk_version default to the value of "android_version")
111-
android_version = 18
112-
target_sdk_version = 18
113-
min_sdk_version = 18
114-
115143
# If set to true, makes the app run in full-screen, by adding the following line
116144
# as an XML attribute to the manifest's <application> tag :
117145
# android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen
118146
# Defaults to false.
119147
fullscreen = false
120148

121-
# Specifies the array of targets to build for.
122-
# Defaults to "arm-linux-androideabi".
123-
# Other possible targets include "aarch64-linux-android",
124-
# "armv7-linux-androideabi", "i686-linux-android" and "x86_64-linux-android".
125-
build_targets = [ "arm-linux-androideabi", "armv7-linux-androideabi" ]
126-
127-
# The maximum supported OpenGL ES version , as claimed by the manifest. Defaults to 2.0.
149+
# The maximum supported OpenGL ES version , as claimed by the manifest.
150+
# Defaults to 2.0.
128151
# See https://developer.android.com/guide/topics/graphics/opengl.html#manifest
129-
opengles_version_major = 2
130-
opengles_version_minor = 0
152+
opengles_version_major = 3
153+
opengles_version_minor = 2
131154

132155
# Adds extra arbitrary XML attributes to the <application> tag in the manifest.
133156
# See https://developer.android.com/guide/topics/manifest/application-element.html
@@ -140,4 +163,28 @@ opengles_version_minor = 0
140163
[package.metadata.android.activity_attributes]
141164
"android:screenOrientation" = "unspecified"
142165
"android:uiOptions" = "none"
166+
167+
# Adds a uses-feature element to the manifest
168+
# Supported keys: name, required, version
169+
# The glEsVersion attribute is not supported using this section.
170+
# It can be specified using the opengles_version_major and opengles_version_minor values
171+
# See https://developer.android.com/guide/topics/manifest/uses-feature-element
172+
[[package.metadata.android.feature]]
173+
name = "android.hardware.camera"
174+
175+
[[package.metadata.android.feature]]
176+
name = "android.hardware.vulkan.level"
177+
version = "1"
178+
required = false
179+
180+
# Adds a uses-permission element to the manifest.
181+
# Note that android_version 23 and higher, Android requires the application to request permissions at runtime.
182+
# There is currently no way to do this using a pure NDK based application.
183+
# See https://developer.android.com/guide/topics/manifest/uses-permission-element
184+
[[package.metadata.android.permission]]
185+
name = "android.permission.WRITE_EXTERNAL_STORAGE"
186+
max_sdk_version = 18
187+
188+
[[package.metadata.android.permission]]
189+
name = "android.permission.CAMERA"
143190
```

0 commit comments

Comments
 (0)