Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 xbuild/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xbuild"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "Builds rust mobile/desktop projects."
repository = "https://github.com/rust-mobile/xbuild"
Expand Down
25 changes: 25 additions & 0 deletions xbuild/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ pub struct AndroidDebugConfig {
pub reverse: HashMap<String, String>,
}

fn default_java_version() -> usize {
8
}

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AndroidConfig {
Expand All @@ -430,6 +434,27 @@ pub struct AndroidConfig {
pub manifest: AndroidManifest,
#[serde(default)]
pub dependencies: Vec<String>,
/// Java version used for compilation
/// For Java 1.8 and below specify `8` and below
#[serde(default = "default_java_version")]
pub java_version: usize,
/// The gradle plugin version which shall be set in the main build.gradle file
/// Make sure it is compatible with the installed gradle version!
/// Depending on the plugin version a minimum gradle version must be available
/// Depending on the api level a minimum gradle plugin version is required
/// https://developer.android.com/build/releases/gradle-plugin?#kts
///
/// Recommendation:
/// 1) Select desired api level
/// 2) Determine minium gradle plugin version and use at least that version (See url above)
/// 3) Determin minium gradle version (See url above)
#[serde(default)]
pub gradle_plugin_version: Option<String>,
/// The kotlin version which shall be set in the main build.gradle file
/// Determine version from the gradle plugin version using https://developer.android.com/build/kotlin-support
/// Important: This version of Kotlin must be installed on the system!
#[serde(default)]
pub kotlin_version: Option<String>,
/// Defaults to [`false`], but uses [`true`] when the user builds a format that requires
/// `gradle` (i.e. [`Format::Aab`]).
#[serde(default)]
Expand Down
10 changes: 0 additions & 10 deletions xbuild/src/gradle/build.gradle

This file was deleted.

34 changes: 31 additions & 3 deletions xbuild/src/gradle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use apk::Target;
use std::path::{Path, PathBuf};
use std::process::Command;

static BUILD_GRADLE: &[u8] = include_bytes!("./build.gradle");
static GRADLE_PROPERTIES: &[u8] = include_bytes!("./gradle.properties");
static SETTINGS_GRADLE: &[u8] = include_bytes!("./settings.gradle");
static IC_LAUNCHER: &[u8] = include_bytes!("./ic_launcher.xml");
Expand Down Expand Up @@ -40,13 +39,30 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R
let kotlin = main.join("kotlin");
let jnilibs = main.join("jniLibs");
let res = main.join("res");
let config = env.config().android();

let gradle_plugin_version = config.gradle_plugin_version.as_ref().map(|s| s.as_str()).unwrap_or("7.3.0");
let kotlin_version = config.kotlin_version.as_ref().map(|s| s.as_str()).unwrap_or("1.7.20");

let build_gradle = format!(r#"
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {{
id 'com.android.application' version '{gradle_plugin_version}' apply false
id 'com.android.library' version '{gradle_plugin_version}' apply false
id 'org.jetbrains.kotlin.android' version '{kotlin_version}' apply false
}}

task clean(type: Delete) {{
delete rootProject.buildDir
}}
"#
);

std::fs::create_dir_all(&kotlin)?;
std::fs::write(gradle.join("build.gradle"), BUILD_GRADLE)?;
std::fs::write(gradle.join("build.gradle"), build_gradle)?;
std::fs::write(gradle.join("gradle.properties"), GRADLE_PROPERTIES)?;
std::fs::write(gradle.join("settings.gradle"), SETTINGS_GRADLE)?;

let config = env.config().android();
let mut manifest = config.manifest.clone();

let package = manifest.package.take().unwrap_or_default();
Expand All @@ -72,6 +88,9 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R
r#"assetPacks = [":baseAssets"]"#
};

let java_version = config.java_version;
let gradle_java_version_rep = if java_version <= 8 { format!("1_{java_version}") } else {format!("{java_version}")};

let app_build_gradle = format!(
r#"
plugins {{
Expand All @@ -88,6 +107,15 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R
versionCode {version_code}
versionName '{version_name}'
}}

compileOptions {{
sourceCompatibility JavaVersion.VERSION_{gradle_java_version_rep}
targetCompatibility JavaVersion.VERSION_{gradle_java_version_rep}
}}

kotlin {{
jvmToolchain({java_version})
}}
{asset_packs}
}}
dependencies {{
Expand Down
Loading