-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wry build support for android target.
- Loading branch information
Showing
10 changed files
with
172 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Top-level build file where you can add configuration options common to all sub-projects/modules. | ||
plugins { | ||
id 'com.android.application' version '7.3.0' apply false | ||
id 'com.android.library' version '7.3.0' apply false | ||
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false | ||
} | ||
|
||
task clean(type: Delete) { | ||
delete rootProject.buildDir | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Project-wide Gradle settings. | ||
# IDE (e.g. Android Studio) users: | ||
# Gradle settings configured through the IDE *will override* | ||
# any settings specified in this file. | ||
# For more details on how to configure your build environment visit | ||
# http://www.gradle.org/docs/current/userguide/build_environment.html | ||
# Specifies the JVM arguments used for the daemon process. | ||
# The setting is particularly useful for tweaking memory settings. | ||
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 | ||
# When configured, Gradle will run in incubating parallel mode. | ||
# This option should only be used with decoupled projects. More details, visit | ||
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects | ||
# org.gradle.parallel=true | ||
# AndroidX package structure to make it clearer which packages are bundled with the | ||
# Android operating system, and which are packaged with your app"s APK | ||
# https://developer.android.com/topic/libraries/support-library/androidx-rn | ||
android.useAndroidX=true | ||
# Enables namespacing of each library's R class so that its R class includes only the | ||
# resources declared in the library itself and none from the library's dependencies, | ||
# thereby reducing the size of the R class for that library | ||
android.nonTransitiveRClass=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::cargo::CrateType; | ||
use crate::{task, BuildEnv}; | ||
use anyhow::Result; | ||
use std::path::Path; | ||
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"); | ||
|
||
pub fn setup_env(env: &BuildEnv) -> Result<()> { | ||
let gradle = env.platform_dir().join("gradle"); | ||
let app = gradle.join("app"); | ||
let kotlin = app.join("src").join("main").join("kotlin"); | ||
|
||
let package = env.manifest().android().package.clone().unwrap_or_default(); | ||
let (package, name) = package.rsplit_once('.').unwrap(); | ||
|
||
if !kotlin.exists() { | ||
std::fs::create_dir_all(&kotlin)?; | ||
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 manifest = env.manifest().android(); | ||
let target_sdk = manifest.sdk.target_sdk_version.unwrap(); | ||
let min_sdk = manifest.sdk.target_sdk_version.unwrap(); | ||
let version_code = manifest.version_code.unwrap(); | ||
let version_name = manifest.version_name.as_ref().unwrap(); | ||
|
||
let app_build_gradle = format!( | ||
r#" | ||
plugins {{ | ||
id 'com.android.application' | ||
id 'org.jetbrains.kotlin.android' | ||
}} | ||
android {{ | ||
namespace '{package}' | ||
compileSdk {target_sdk} | ||
defaultConfig {{ | ||
minSdk {min_sdk} | ||
targetSdk {target_sdk} | ||
versionCode {version_code} | ||
versionName '{version_name}' | ||
}} | ||
}} | ||
dependencies {{ | ||
implementation 'androidx.appcompat:appcompat:1.4.1' | ||
}} | ||
"#, | ||
package=package, | ||
target_sdk=target_sdk, | ||
min_sdk=min_sdk, | ||
version_code=version_code, | ||
version_name=version_name, | ||
); | ||
std::fs::write(app.join("build.gradle"), app_build_gradle)?; | ||
|
||
let main_activity = format!( | ||
r#" | ||
package {}.{} | ||
class MainActivity : TauriActivity() | ||
"#, | ||
package, name | ||
); | ||
std::fs::write(kotlin.join("MainActivity.kt"), main_activity)?; | ||
} | ||
|
||
std::env::set_var("WRY_ANDROID_REVERSED_DOMAIN", package); | ||
std::env::set_var("WRY_ANDROID_APP_NAME_SNAKE_CASE", name); | ||
std::env::set_var("WRY_ANDROID_KOTLIN_FILES_OUT_DIR", kotlin); | ||
Ok(()) | ||
} | ||
|
||
pub fn build_apk(env: &BuildEnv, apk: &Path) -> Result<()> { | ||
let platform_dir = env.platform_dir(); | ||
let gradle = platform_dir.join("gradle"); | ||
let main = gradle.join("app").join("src").join("main"); | ||
let jnilibs = main.join("jniLibs"); | ||
|
||
let manifest = env.manifest().android(); | ||
std::fs::write( | ||
main.join("AndroidManifest.xml"), | ||
quick_xml::se::to_string(manifest)?, | ||
)?; | ||
|
||
for target in env.target().compile_targets() { | ||
let arch_dir = platform_dir.join(target.arch().to_string()); | ||
let lib = env.cargo_artefact(&arch_dir.join("cargo"), target, CrateType::Cdylib)?; | ||
let lib_name = lib.file_name().unwrap(); | ||
let lib_dir = jnilibs.join(target.android_abi().android_abi()); | ||
std::fs::create_dir_all(&lib_dir)?; | ||
std::fs::copy(&lib, lib_dir.join(lib_name))?; | ||
} | ||
|
||
let mut cmd = Command::new("gradle"); | ||
cmd.current_dir(&gradle).arg("build"); | ||
task::run(cmd, env.verbose())?; | ||
let out = gradle | ||
.join("app") | ||
.join("build") | ||
.join("outputs") | ||
.join("apk") | ||
.join("debug") | ||
.join("app-debug.apk"); | ||
std::fs::copy(out, apk)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
google() | ||
mavenCentral() | ||
} | ||
} | ||
dependencyResolutionManagement { | ||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
} | ||
|
||
include ':app' |