Skip to content

Commit

Permalink
fix: only build specified rust targets for aab/apk build (#6625)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Apr 5, 2023
1 parent 052c582 commit d03e47d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-android-specified-targets-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'cli.rs': 'patch'
'cli.js': 'patch'
---

Build only specified rust targets for `tauri android build` instead of all.
4 changes: 2 additions & 2 deletions tooling/cli/Cargo.lock

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

2 changes: 1 addition & 1 deletion tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ path = "src/main.rs"
openssl-vendored = [ "tauri-mobile/openssl-vendored" ]

[dependencies]
tauri-mobile = { version = "0.2.5", default-features = false }
tauri-mobile = { version = "0.3", default-features = false }
textwrap = { version = "0.11.0", features = [ "term_size" ] }
jsonrpsee = { version = "0.16", features = [ "server" ] }
jsonrpsee-core = "0.16"
Expand Down
21 changes: 8 additions & 13 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,15 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
}

pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
let (merge_config, merge_config_path) = if let Some(config) = &options.config {
if config.starts_with('{') {
(Some(config.to_string()), None)
} else {
(
Some(
std::fs::read_to_string(config).with_context(|| "failed to read custom configuration")?,
),
Some(config.clone()),
)
}
} else {
(None, None)
let (merge_config, merge_config_path) = match &options.config {
Some(config) if config.starts_with('{') => (Some(config.to_string()), None),
Some(config) => (
Some(std::fs::read_to_string(config).with_context(|| "failed to read custom configuration")?),
Some(config.clone()),
),
None => (None, None),
};

options.config = merge_config;

let tauri_path = tauri_dir();
Expand Down
46 changes: 21 additions & 25 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fn run_build(
env,
noise_level,
profile,
get_targets_or_all(Vec::new())?,
get_targets(options.targets.clone().unwrap_or_default())?,
options.split_per_abi,
)?
} else {
Expand All @@ -194,7 +194,7 @@ fn run_build(
env,
noise_level,
profile,
get_targets_or_all(Vec::new())?,
get_targets(options.targets.unwrap_or_default())?,
options.split_per_abi,
)?
} else {
Expand All @@ -207,28 +207,24 @@ fn run_build(
Ok(())
}

fn get_targets_or_all<'a>(targets: Vec<String>) -> Result<Vec<&'a Target<'a>>> {
if targets.is_empty() {
Ok(Target::all().iter().map(|t| t.1).collect())
} else {
let mut outs = Vec::new();

let possible_targets = Target::all()
.keys()
.map(|key| key.to_string())
.collect::<Vec<String>>()
.join(",");

for t in targets {
let target = Target::for_name(&t).ok_or_else(|| {
anyhow::anyhow!(
"Target {} is invalid; the possible targets are {}",
t,
possible_targets
)
})?;
outs.push(target);
}
Ok(outs)
fn get_targets<'a>(targets: Vec<String>) -> Result<Vec<&'a Target<'a>>> {
let mut outs = Vec::new();

let possible_targets = Target::all()
.keys()
.map(|key| key.to_string())
.collect::<Vec<String>>()
.join(",");

for t in targets {
let target = Target::for_name(&t).ok_or_else(|| {
anyhow::anyhow!(
"Target {} is invalid; the possible targets are {}",
t,
possible_targets
)
})?;
outs.push(target);
}
Ok(outs)
}
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn ensure_init(project_dir: PathBuf, target: Target) -> Result<()> {
target.command_name(),
)
} else {
#[allow(irrefutable_let_patterns)]
if let Target::Android = target {
create_dir_all(project_dir.join(".tauri").join("plugins"))?;
}
Expand Down
18 changes: 8 additions & 10 deletions tooling/cli/templates/mobile/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ android {
isMinifyEnabled = false
packagingOptions {
{{~#each targets}}

jniLibs.keepDebugSymbols.add("*/{{this.abi}}/*.so")
{{/each}}
}
Expand All @@ -46,17 +45,14 @@ android {
flavorDimensions.add("abi")
productFlavors {
create("universal") {
val abiList = findProperty("abiList") as? String

dimension = "abi"
ndk {
abiFilters += abiList?.split(",")?.map { it.trim() } ?: listOf(
abiFilters += (findProperty("abiList") as? String)?.split(",") ?: listOf(
{{~#each targets}}
"{{this.abi}}",{{/each}}
)
}
}

{{~#each targets}}

create("{{this.arch}}") {
Expand All @@ -74,8 +70,8 @@ android {

rust {
rootDirRel = "{{root-dir-rel}}"
targets = listOf({{quote-and-join target-names}})
arches = listOf({{quote-and-join arches}})
targets = (findProperty("targetList") as? String)?.split(",") ?: listOf({{quote-and-join target-names}})
arches = (findProperty("archList") as? String)?.split(",") ?: listOf({{quote-and-join arches}})
}

dependencies {
Expand All @@ -98,9 +94,11 @@ afterEvaluate {
android.applicationVariants.all {
tasks["mergeUniversalReleaseJniLibFolders"].dependsOn(tasks["rustBuildRelease"])
tasks["mergeUniversalDebugJniLibFolders"].dependsOn(tasks["rustBuildDebug"])
productFlavors.filter{ it.name != "universal" }.forEach { _ ->
val archAndBuildType = name.capitalize()
tasks["merge${archAndBuildType}JniLibFolders"].dependsOn(tasks["rustBuild${archAndBuildType}"])
if (findProperty("targetList") == null) {
productFlavors.filter{ it.name != "universal" }.forEach { _ ->
val archAndBuildType = name.capitalize()
tasks["merge${archAndBuildType}JniLibFolders"].dependsOn(tasks["rustBuild${archAndBuildType}"])
}
}
}
}

0 comments on commit d03e47d

Please sign in to comment.