Skip to content

Commit 8d671ed

Browse files
Rework for rust-mobile#212.
Support for latest Android NDK and SDK, ABI rework, additional manifest configuration, and multiple cargo targets. Updated dependencies. Formatted using rustfmt. Removed gradle dependency.
1 parent c37ac76 commit 8d671ed

File tree

30 files changed

+2265
-2098
lines changed

30 files changed

+2265
-2098
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: 59 additions & 40 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

@@ -32,12 +32,10 @@ Before you can compile for Android, you need to setup your environment. This nee
3232

3333
- Install [`rustup`](https://rustup.rs/).
3434
- 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/).
35+
- Install the Java JRE or JDK (on Ubuntu, `sudo apt-get install openjdk-8-jdk`).
3836
- Download and unzip [the Android NDK](https://developer.android.com/ndk).
3937
- 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"`.
38+
- Install some components in the SDK: `./android-sdk/tools/bin/sdkmanager "platform-tools" "platforms;android-29" "build-tools;29.0.0"`.
4139
- Install `cargo-apk` with `cargo install cargo-apk`.
4240
- Set the environment variables `NDK_HOME` to the path of the NDK and `ANDROID_HOME` to the path of the SDK.
4341

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

49-
This will build an Android package in `target/android-artifacts/app/build/outputs/apk`.
47+
This will build an Android package in `target/android-artifacts/<debug|release>/apk`.
5048

5149
### Testing on an Android emulator
5250

@@ -72,62 +70,63 @@ the stdlib.
7270

7371
## The build process
7472

75-
The build process works by invoking `cargo rustc` and:
73+
The build process works by running rustc and:
7674

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.
75+
- Always compiles your crate as a static library.
76+
- Uses `ndk-build` provided by the NDK to to build a shared library.
77+
- Links to the `android_native_app_glue` library provided by the Android NDK.
78+
- Injects some glue libraries in Rust, which ties the link between `android_native_app_glue` and the `main` function of your crate.
8179

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

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`.
82+
The command then builds the APK using the shared library, generated manifest, and tools from the Android SDK.
83+
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.
8684

8785
# Supported `[package.metadata.android]` entries
8886

8987
```toml
90-
[package.metadata.android]
88+
# The target Android API level.
89+
# "android_version" is the compile SDK version. It defaults to 29.
90+
# (target_sdk_version defaults to the value of "android_version")
91+
# (min_sdk_version defaults to 18) It defaults to 18 because this is the minimum supported by rustc.
92+
android_version = 29
93+
target_sdk_version = 29
94+
min_sdk_version = 26
95+
96+
# Specifies the array of targets to build for.
97+
# Defaults to "armv7-linux-androideabi", "aarch64-linux-android", "i686-linux-android".
98+
build_targets = [ "armv7-linux-androideabi", "aarch64-linux-android", "i686-linux-android", "x86_64-linux-android" ]
99+
100+
#
101+
# The following settings can be customized on a per bin/example basis. See multiple_targets example
102+
#
91103

92104
# The Java package name for your application.
93105
# Hyphens are converted to underscores.
94-
package_name = "com.author-name.my-android-app"
106+
package_name = "rust.cargo.apk.advanced"
95107

96108
# The user-friendly name for your app, as displayed in the applications menu.
97-
label = "My Android App"
109+
label = "Advanced android-rs-glue example"
98110

99-
# Path to your application's res/ folder. See `examples/use_icon/res`.
100-
res = "path/to/res_folder"
111+
# Path to your application's res/ folder.
112+
res = "res"
101113

102-
# Virtual path your application's icon for any mipmap level. See `examples/use_icon/icon`.
103-
icon = "@mipmap/ic_laucher"
114+
# Virtual path your application's icon for any mipmap level.
115+
icon = "@mipmap/ic_launcher"
104116

105-
# Path to the folder containing your application's assets. See `examples/use_assets/assets`.
106-
assets = "path/to/assets_folder"
107-
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
117+
# Path to the folder containing your application's assets.
118+
assets = "assets"
114119

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

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-
127126
# The maximum supported OpenGL ES version , as claimed by the manifest. Defaults to 2.0.
128127
# See https://developer.android.com/guide/topics/graphics/opengl.html#manifest
129-
opengles_version_major = 2
130-
opengles_version_minor = 0
128+
opengles_version_major = 3
129+
opengles_version_minor = 2
131130

132131
# Adds extra arbitrary XML attributes to the <application> tag in the manifest.
133132
# See https://developer.android.com/guide/topics/manifest/application-element.html
@@ -140,4 +139,24 @@ opengles_version_minor = 0
140139
[package.metadata.android.activity_attributes]
141140
"android:screenOrientation" = "unspecified"
142141
"android:uiOptions" = "none"
142+
143+
[[package.metadata.android.feature]]
144+
name = "android.hardware.camera"
145+
146+
[[package.metadata.android.feature]]
147+
name = "android.hardware.vulkan.level"
148+
version = "1"
149+
required = false
150+
151+
# Request permissions. Note that android_version 23 and higher, Android requires the application
152+
# to request permissions at runtime. There is currently no way to do this using a pure NDK based application.
153+
[[package.metadata.android.permission]]
154+
name = "android.permission.WRITE_EXTERNAL_STORAGE"
155+
maxSdkVersion = "18"
156+
157+
[[package.metadata.android.permission]]
158+
name = "android.permission.CAMERA"
159+
160+
[dependencies.android_glue]
161+
path = "../../glue"
143162
```

0 commit comments

Comments
 (0)