Skip to content

Commit

Permalink
Reorganize tools structure (#132)
Browse files Browse the repository at this point in the history
* Reorganize tools structure

* Downgrade bundletool version

* Fix import

Co-authored-by: David Ackerman <enfipy@gmail.com>
  • Loading branch information
Heezay and enfipy committed Aug 4, 2022
1 parent dba8005 commit 7a78050
Show file tree
Hide file tree
Showing 43 changed files with 112 additions and 100 deletions.
41 changes: 22 additions & 19 deletions crossbundle/cli/src/commands/build/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use android_manifest::AndroidManifest;
use android_tools::java_tools::{JarSigner, Key};
use clap::Parser;
use crossbundle_tools::{
commands::android::{self, rust_compile},
commands::android::{
common::{extract_archive::extract_archive, *},
gradle::*,
native::*,
},
error::CommandExt,
tools::*,
tools::{AndroidNdk, AndroidSdk},
types::*,
utils::Config,
};
Expand Down Expand Up @@ -67,7 +71,7 @@ impl AndroidBuildCommand {
let (_, _, gradle_project_path) =
self.build_gradle(config, &context, &self.export_path)?;
config.status("Building Gradle project")?;
let mut gradle = android::gradle_init()?;
let mut gradle = gradle_init()?;
gradle
.arg("build")
.arg("-p")
Expand Down Expand Up @@ -103,7 +107,7 @@ impl AndroidBuildCommand {
}

config.status("Generating gradle project")?;
let gradle_project_path = android::gen_gradle_project(
let gradle_project_path = gen_gradle_project(
&android_build_dir,
&context.config.get_android_assets(),
&context.config.android.res,
Expand All @@ -113,7 +117,7 @@ impl AndroidBuildCommand {
config.status_message("Reading", "AndroidManifest.xml")?;
let manifest = Self::get_android_manifest(context, AndroidStrategy::GradleApk)?;
config.status_message("Generating", "AndroidManifest.xml")?;
android::save_android_manifest(&gradle_project_path, &manifest)?;
save_android_manifest(&gradle_project_path, &manifest)?;

let lib_name = "crossbow_android";
self.build_rust_lib(config, context, lib_name, Some(android_build_dir))?;
Expand Down Expand Up @@ -201,7 +205,7 @@ impl AndroidBuildCommand {
config.status_message("Reading", "AndroidManifest.xml")?;
let manifest = Self::get_android_manifest(context, AndroidStrategy::NativeApk)?;
config.status_message("Generating", "AndroidManifest.xml")?;
let manifest_path = android::save_android_manifest(&native_build_dir, &manifest)?;
let manifest_path = save_android_manifest(&native_build_dir, &manifest)?;

config.status_message("Compiling", "lib")?;
let target_sdk_version = Self::target_sdk_version(&manifest, &sdk);
Expand All @@ -219,7 +223,7 @@ impl AndroidBuildCommand {
)?;

config.status_message("Generating", "unaligned APK file")?;
let unaligned_apk_path = android::gen_unaligned_apk(
let unaligned_apk_path = gen_unaligned_apk(
&sdk,
&project_path,
&native_build_dir,
Expand All @@ -232,7 +236,7 @@ impl AndroidBuildCommand {

config.status("Adding libs into APK file")?;
for (compiled_lib, build_target) in compiled_libs {
android::add_libs_into_apk(
add_libs_into_apk(
&sdk,
&ndk,
&unaligned_apk_path,
Expand All @@ -247,7 +251,7 @@ impl AndroidBuildCommand {

config.status("Aligning APK file")?;
let aligned_apk_path =
android::align_apk(&sdk, &unaligned_apk_path, &package_name, &outputs_build_dir)?;
align_apk(&sdk, &unaligned_apk_path, &package_name, &outputs_build_dir)?;

config.status_message("Generating", "debug signing key")?;
let key = Self::find_keystore(
Expand All @@ -257,7 +261,7 @@ impl AndroidBuildCommand {
)?;

config.status("Signing APK file")?;
android::sign_apk(&sdk, &aligned_apk_path, key)?;
sign_apk(&sdk, &aligned_apk_path, key)?;
config.status("Build finished successfully")?;
Ok((manifest, sdk, aligned_apk_path))
}
Expand All @@ -284,7 +288,7 @@ impl AndroidBuildCommand {
config.status_message("Reading", "AndroidManifest.xml")?;
let manifest = Self::get_android_manifest(context, AndroidStrategy::NativeAab)?;
config.status_message("Generating", "AndroidManifest.xml")?;
let manifest_path = android::save_android_manifest(&native_build_dir, &manifest)?;
let manifest_path = save_android_manifest(&native_build_dir, &manifest)?;

config.status_message("Compiling", "lib")?;
let target_sdk_version = Self::target_sdk_version(&manifest, &sdk);
Expand Down Expand Up @@ -334,11 +338,11 @@ impl AndroidBuildCommand {

config.status("Extracting apk files")?;
let output_dir = native_build_dir.join("extracted_apk_files");
let extracted_apk_path = android::extract_archive(&apk_path, &output_dir)?;
let extracted_apk_path = extract_archive(&apk_path, &output_dir)?;

config.status("Adding libs")?;
for (compiled_lib, build_target) in compiled_libs {
android::add_libs_into_aapt2(
add_libs_into_aapt2(
&ndk,
&compiled_lib,
build_target,
Expand All @@ -352,7 +356,7 @@ impl AndroidBuildCommand {

config.status("Generating ZIP module from extracted files")?;
let gen_zip_modules =
android::gen_zip_modules(&native_build_dir, &package_name, &extracted_apk_path)?;
gen_zip_modules(&native_build_dir, &package_name, &extracted_apk_path)?;

for entry in std::fs::read_dir(&native_build_dir)? {
let entry = entry?;
Expand All @@ -363,8 +367,7 @@ impl AndroidBuildCommand {
}

config.status("Generating aab from modules")?;
let aab_path =
android::gen_aab_from_modules(&package_name, &[gen_zip_modules], &outputs_build_dir)?;
let aab_path = gen_aab_from_modules(&package_name, &[gen_zip_modules], &outputs_build_dir)?;

config.status_message("Generating", "debug signing key")?;
let key = Self::find_keystore(
Expand Down Expand Up @@ -430,7 +433,7 @@ impl AndroidBuildCommand {
if aab_key.key_path.exists() {
aab_key
} else {
android::gen_key(
gen_key(
Some(aab_key.key_path),
Some(aab_key.key_pass),
Some(aab_key.key_alias),
Expand All @@ -441,7 +444,7 @@ impl AndroidBuildCommand {
if aab_key.key_path.exists() {
aab_key
} else {
android::gen_key(
gen_key(
Some(aab_key.key_path),
Some(aab_key.key_pass),
Some(aab_key.key_alias),
Expand Down Expand Up @@ -535,7 +538,7 @@ impl AndroidBuildCommand {
strategy: AndroidStrategy,
) -> Result<AndroidManifest> {
if let Some(manifest_path) = &context.config.android.manifest_path {
return Ok(android::read_android_manifest(manifest_path)?);
return Ok(read_android_manifest(manifest_path)?);
}
let mut manifest = if let Some(manifest) = &context.config.android.manifest {
manifest.clone()
Expand Down
4 changes: 2 additions & 2 deletions crossbundle/cli/src/commands/install/bundletool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::path::PathBuf;
#[derive(Parser, Clone, Debug, Default)]
pub struct BundletoolInstallCommand {
/// Required. Version of download bundletool. For example:
/// --version 1.11.0
#[clap(long, short, default_value = "1.11.0")]
/// --version 1.8.2
#[clap(long, short, default_value = "1.8.2")]
version: String,
/// Path to install bundletool. By default bundletool will be downloaded and saved in home directory
#[clap(long, short)]
Expand Down
6 changes: 3 additions & 3 deletions crossbundle/cli/src/commands/install/command_line_tools.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use clap::Parser;
use crossbundle_tools::{
commands::android::{self, remove},
commands::android::common::{extract_archive::extract_archive, remove},
tools::AndroidSdk,
utils::Config,
};
Expand Down Expand Up @@ -46,13 +46,13 @@ impl CommandLineToolsInstallCommand {
"Extracting zip archive contents into",
path.to_str().unwrap(),
)?;
android::extract_archive(&file_path, path)?;
extract_archive(&file_path, path)?;
} else {
config.status_message(
"Extracting zip archive contents into",
&sdk_path.to_str().unwrap(),
)?;
android::extract_archive(&file_path, Path::new(&sdk_path))?;
extract_archive(&file_path, Path::new(&sdk_path))?;
}

config.status("Deleting zip archive was left after installation")?;
Expand Down
6 changes: 4 additions & 2 deletions crossbundle/cli/src/commands/log/android.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::error::*;
use clap::Parser;
use crossbundle_tools::{commands::android, tools::AndroidSdk, utils::Config};
use crossbundle_tools::{
commands::android::common::attach_logger_only_rust, tools::AndroidSdk, utils::Config,
};

#[derive(Parser, Clone, Debug)]
pub struct AndroidLogCommand;

impl AndroidLogCommand {
pub fn run(&self, _config: &Config) -> Result<()> {
let sdk = AndroidSdk::from_env()?;
android::attach_logger_only_rust(&sdk)?;
attach_logger_only_rust(&sdk)?;
Ok(())
}
}
15 changes: 7 additions & 8 deletions crossbundle/cli/src/commands/run/android.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::commands::build::{android::AndroidBuildCommand, BuildContext};
use crate::error::Result;
use android_tools::error::CommandExt;
use crate::error::*;
use clap::Parser;
use crossbundle_tools::{
commands::android,
commands::android::gradle_init,
commands::android::{common::start_app, gradle::gradle_init, native::install_apk},
error::CommandExt,
tools::{BuildApks, InstallApks},
types::AndroidStrategy,
utils::Config,
Expand Down Expand Up @@ -56,7 +55,7 @@ impl AndroidRunCommand {
config.status("Installing APKs file")?;
InstallApks::new(&apks_path).run()?;
config.status("Starting APK file")?;
android::start_apk(
start_app(
&sdk,
&android_manifest.package,
"android.app.NativeActivity",
Expand All @@ -69,9 +68,9 @@ impl AndroidRunCommand {
let (android_manifest, sdk, apk_path) = self.build_command.execute_apk(config, context)?;
config.status("Starting run process")?;
config.status("Installing APK file")?;
android::install_apk(&sdk, &apk_path)?;
install_apk(&sdk, &apk_path)?;
config.status("Starting APK file")?;
android::start_apk(
start_app(
&sdk,
&android_manifest.package,
"android.app.NativeActivity",
Expand All @@ -92,7 +91,7 @@ impl AndroidRunCommand {
.arg(dunce::simplified(&gradle_project_path));
gradle.output_err(true)?;
config.status("Starting APK file")?;
android::start_apk(
start_app(
&sdk,
&android_manifest.package,
"com.crossbow.game.CrossbowApp",
Expand Down
2 changes: 1 addition & 1 deletion crossbundle/cli/src/types/android_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crossbundle_tools::{
commands::android::AndroidGradlePlugins,
commands::android::gradle::AndroidGradlePlugins,
types::{android_manifest::AndroidManifest, AndroidTarget, AppWrapper},
};
use serde::{Deserialize, Serialize};
Expand Down
19 changes: 19 additions & 0 deletions crossbundle/tools/src/commands/android/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub mod attach_logger;
pub mod detect_abi;
pub mod extract_archive;
pub mod gen_key;
pub mod helper_functions;
pub mod read_manifest;
pub mod rust_compile;
pub mod save_manifest;
pub mod start_app;
pub mod write_zip;

pub use attach_logger::*;
pub use detect_abi::*;
pub use gen_key::*;
pub use helper_functions::*;
pub use read_manifest::*;
pub use rust_compile::*;
pub use save_manifest::*;
pub use start_app::*;
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::error::*;
use crate::tools::AndroidSdk;

/// Starts installed APK or AAB on emulator or connected device.
/// Installing APK or AAB on emulator or connected device.
/// Runs `adb shell am start ...` command
pub fn start_apk(sdk: &AndroidSdk, package: &str, activity: &str) -> Result<()> {
pub fn start_app(sdk: &AndroidSdk, package: &str, activity: &str) -> Result<()> {
let mut adb = sdk.platform_tool(bin!("adb"))?;
adb.arg("shell")
.arg("am")
Expand Down
13 changes: 0 additions & 13 deletions crossbundle/tools/src/commands/android/generate/mod.rs

This file was deleted.

5 changes: 5 additions & 0 deletions crossbundle/tools/src/commands/android/gradle/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod gen_gradle_project;
pub mod gradle_init;

pub use gen_gradle_project::*;
pub use gradle_init::*;
39 changes: 3 additions & 36 deletions crossbundle/tools/src/commands/android/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,3 @@
///! Commands for compiling rust code for `Android`,
///! generation/aligning/signing/installing/starting on device APKs and AAB,
///! generation `AndroidManifest.xml` and so on.
mod add_libs_into_aapt2;
mod add_libs_into_apk;
mod align_apk;
mod attach_logger;
mod detect_abi;
mod extract_apk;
pub mod generate;
mod gradle_command;
mod helper_functions;
mod install_apk;
mod read_manifest;
mod rust_compile;
mod save_manifest;
mod sign_apk;
mod start_apk;
mod write_zip;

pub use add_libs_into_aapt2::*;
pub use add_libs_into_apk::*;
pub use align_apk::*;
pub use attach_logger::*;
pub use detect_abi::*;
pub use extract_apk::*;
pub use generate::*;
pub use gradle_command::*;
pub use helper_functions::*;
pub use install_apk::*;
pub use read_manifest::*;
pub use rust_compile::*;
pub use save_manifest::*;
pub use sign_apk::*;
pub use start_apk::*;
pub use write_zip::*;
pub mod common;
pub mod gradle;
pub mod native;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
commands::android::{get_libs_in_dir, recursively_define_needed_libs, search_dylibs},
commands::android::native::{get_libs_in_dir, recursively_define_needed_libs, search_dylibs},
error::*,
tools::AndroidNdk,
types::{AndroidTarget, IntoRustTriple, Profile},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
commands::android::common::{extract_archive::extract_archive, remove, save_android_manifest},
error::*,
tools::AndroidSdk,
types::{update_android_manifest_with_default, AndroidStrategy},
Expand All @@ -24,7 +25,7 @@ pub fn gen_minimal_unsigned_aab(
AndroidStrategy::NativeAab,
);

let manifest_path = super::super::save_android_manifest(aab_build_dir, &manifest)?;
let manifest_path = save_android_manifest(aab_build_dir, &manifest)?;
let apk_path = aab_build_dir.join(format!("{}_module.apk", package_name));
if !aab_build_dir.exists() {
std::fs::create_dir_all(&aab_build_dir)?;
Expand All @@ -41,14 +42,14 @@ pub fn gen_minimal_unsigned_aab(
aapt2_link.run()?;

let output_dir = aab_build_dir.join("extracted_apk_files");
let extracted_apk_path = super::super::extract_archive(&apk_path, &output_dir)?;
let extracted_apk_path = extract_archive(&apk_path, &output_dir)?;

let gen_zip_modules = super::gen_zip_modules(aab_build_dir, package_name, &extracted_apk_path)?;

let aab_path =
super::gen_aab_from_modules(package_name, &[gen_zip_modules.clone()], aab_build_dir)?;

super::super::remove(vec![gen_zip_modules, extracted_apk_path])?;
remove(vec![gen_zip_modules, extracted_apk_path])?;
Ok(aab_path)
}

Expand Down
Loading

0 comments on commit 7a78050

Please sign in to comment.