Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manifest (de)serialization #106

Merged
merged 17 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion cargo-apk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unreleased

- Breaking, uses `ndk-build`'s new (de)serialized `Manifest` struct to properly serialize a toml's `[package.metadata.android]` to an `AndroidManifest.xml`. The `[package.metadata.android]` now closely resembles the structure of [`an android manifest file`](https://developer.android.com/guide/topics/manifest/manifest-element). See [README](README.md) for an example of the new `[package.metadata.android]` structure and all manifest attributes that are currently supported.
- **Breaking:**, uses `ndk-build`'s new (de)serialized `Manifest` struct to properly serialize a toml's `[package.metadata.android]` to an `AndroidManifest.xml`. The `[package.metadata.android]` now closely resembles the structure of [an android manifest file](https://developer.android.com/guide/topics/manifest/manifest-element). See [README](README.md) for an example of the new `[package.metadata.android]` structure and all manifest attributes that are currently supported.
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved

# 0.5.6 (2020-11-25)

Expand Down
26 changes: 10 additions & 16 deletions cargo-apk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,20 @@ target_sdk_version = 29
max_sdk_version = 29

# See https://developer.android.com/guide/topics/manifest/uses-feature-element
#
# Note: there can be multiple .feature entries.
[[package.metadata.android.feature]]
#
# Note: there can be multiple .uses_feature entries.
[[package.metadata.android.uses_feature]]
name = "android.hardware.vulkan.level"
required = true

# If no `opengles_version` is specified in any feature, belows feature is added as a default
# `opengles_version` feature.
[[package.metadata.android.feature]]
opengles_version = [3, 1]
required = true
version = 1

# See https://developer.android.com/guide/topics/manifest/uses-permission-element
#
# Note: there can be multiple .permission entries.
[[package.metadata.android.permission]]
# Note: there can be multiple .uses_permission entries.
[[package.metadata.android.uses_permission]]
name = "android.permission.WRITE_EXTERNAL_STORAGE"
max_sdk_version = 18


# See https://developer.android.com/guide/topics/manifest/application-element
[package.metadata.android.application]

Expand All @@ -91,9 +85,9 @@ label = "Application Name"

# See https://developer.android.com/guide/topics/manifest/meta-data-element
#
# Note: there can be several .metadata entries.
# Note: there can be several .meta_data entries.
# Note: the `resource` attribute is currently not supported.
[[package.metadata.android.application.metadata]]
[[package.metadata.android.application.meta_data]]
name = "com.samsung.android.vr.application.mode"
value = "vr_only"

Expand Down Expand Up @@ -122,9 +116,9 @@ orientation = "landscape"

# See https://developer.android.com/guide/topics/manifest/meta-data-element
#
# Note: there can be several .metadata entries.
# Note: there can be several .meta_data entries.
# Note: the `resource` attribute is currently not supported.
[[package.metadata.android.application.activity.metadata]]
[[package.metadata.android.application.activity.meta_data]]
name = "com.oculus.vr.focusaware"
value = "true"

Expand Down
39 changes: 27 additions & 12 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,46 @@ impl<'a> ApkBuilder<'a> {

// Set default Android manifest values
let mut manifest = self.manifest.android_manifest.clone();

assert!(manifest.package.is_empty());
manifest.package = package_name;
manifest.version_name = Some(self.manifest.version.clone());
manifest.version_code = Some(VersionCode::from_semver(&self.manifest.version)?.to_code(1));
if manifest
.version_name
.replace(self.manifest.version.clone())
.is_some()
{
panic!("version_name should not be set in TOML");
}

if manifest
.version_code
.replace(VersionCode::from_semver(&self.manifest.version)?.to_code(1))
.is_some()
{
panic!("version_code should not be set in TOML");
}

manifest.sdk.target_sdk_version = manifest
manifest
.sdk
.target_sdk_version
.or(Some(self.ndk.default_platform()));
manifest.application.debuggable = manifest
.get_or_insert(self.ndk.default_platform());
manifest
.application
.debuggable
.or(Some(*self.cmd.profile() == Profile::Dev));
manifest.application.has_code = Some(false);
.get_or_insert(*self.cmd.profile() == Profile::Dev);
if manifest.application.label.is_empty() {
manifest.application.label = artifact.name().to_string();
}
let default_meta_data = MetaData {
name: "android.app.lib_name".to_string(),
value: artifact.name().replace("-", "_"),
};
if let Some(meta_datas) = &mut manifest.application.activity.meta_datas {
meta_datas.push(default_meta_data);
} else {
manifest.application.activity.meta_datas = Some(vec![default_meta_data]);
};
manifest
.application
.activity
.meta_data
.push(default_meta_data);

let assets = self.manifest.assets.as_ref().map(|assets| {
dunce::simplified(
&self
Expand Down
5 changes: 3 additions & 2 deletions cargo-apk/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Manifest {
Ok(Self {
version: toml.package.version,
android_manifest: metadata.android_manifest,
build_targets: metadata.build_targets.unwrap_or_default(),
build_targets: metadata.build_targets,
assets: metadata.assets,
resources: metadata.resources,
})
Expand All @@ -52,7 +52,8 @@ struct PackageMetadata {
struct AndroidMetadata {
#[serde(flatten)]
android_manifest: AndroidManifest,
build_targets: Option<Vec<Target>>,
#[serde(default)]
build_targets: Vec<Target>,
assets: Option<String>,
resources: Option<String>,
}
2 changes: 2 additions & 0 deletions ndk-build/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ pub enum NdkError {
InvalidSemver,
#[error("Command '{}' had a non-zero exit code.", format!("{:?}", .0).replace('"', ""))]
CmdFailed(Command),
#[error(transparent)]
Serialize(#[from] quick_xml::de::DeError)
}
Loading