[go: up one dir, main page]

cargo-apk 0.3.1

Cargo subcommand that allows you to build Android packages
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
use std::os;
use std::collections::{HashSet, HashMap};
use std::env;
use std::fs;
use std::fs::File;
use std::io::{self, BufRead, BufReader, Write};
use std::iter::FromIterator;
use std::path::Path;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use cargo::ops;
use cargo::core::Workspace;
use cargo::util::errors::CargoError;
use cargo::util::errors::internal;
use cargo::util::process_builder::process;

use config::AndroidConfig;
use Options;

pub struct BuildResult {
    /// The absolute path where the apk is located.
    pub apk_path: PathBuf,
}

pub fn build(workspace: &Workspace, config: &AndroidConfig, options: &Options)
            -> Result<BuildResult, CargoError>
{
    // First we detect whether `gradle` works.
    match Command::new(&config.gradle_command).arg("-v").stdout(Stdio::null()).status() {
        Ok(s) if s.success() => (),
        _ => {
            return Err(CargoError::from("Could not execute `gradle`. Did you install it?"));
        }
    }

    // Building the `android-artifacts` directory that will contain all the artifacts.
    // FIXME: don't use into_path_unlocked() but pass a Cargo::Filesystem everywhere
    let android_artifacts_dir = workspace.target_dir().join("android-artifacts").into_path_unlocked();
    build_android_artifacts_dir(workspace, &android_artifacts_dir, &config)?;

    let mut abi_libs: HashMap<&str, Vec<String>> = HashMap::new();

    for build_target in config.build_targets.iter() {
        assert_ne!(build_target, "app");
        let build_target_dir = android_artifacts_dir.join(build_target);

        // Finding the tools in the NDK.
        let (gcc_path, gxx_path, ar_path) = {
            let host_os = if cfg!(target_os = "windows") { "windows" }
                       else if cfg!(target_os = "linux") { "linux" }
                       else if cfg!(target_os = "macos") { "darwin" }
                       else { panic!("Unknown or incompatible host OS") };

            let target_arch = if build_target.starts_with("arm") { "arm-linux-androideabi" }
                       else if build_target.starts_with("aarch64") { "aarch64-linux-android" }
                       else if build_target.starts_with("i") { "x86" }
                       else if build_target.starts_with("x86_64") { "x86_64" }
                       else if build_target.starts_with("mipsel") { "mipsel-linux-android" }
                       // TODO: mips64
                       else { panic!("Unknown or incompatible build target: {}", build_target) };

            // Looks like the tools don't always share the prefix of the target arch
            // Just a macos issue?
            let tool_prefix = if build_target.starts_with("arm") { "arm-linux-androideabi" }
                       else if build_target.starts_with("aarch64") { "aarch64-linux-android" }
                       else if build_target.starts_with("i") { "i686-linux-android" }
                       else if build_target.starts_with("x86_64") { "x86_64-linux-android" }
                       else if build_target.starts_with("mipsel") { "mipsel-linux-android" }
                       // TODO: mips64
                       else { panic!("Unknown or incompatible build target: {}", build_target) };

            let base = config.ndk_path.join(format!("toolchains/{}-4.9/prebuilt/{}-x86_64", target_arch, host_os));
            (base.join(format!("bin/{}-gcc", tool_prefix)),
             base.join(format!("bin/{}-g++", tool_prefix)),
             base.join(format!("bin/{}-ar", tool_prefix)))
        };

        let gcc_sysroot = {
            let arch = if build_target.starts_with("arm") { "arm" }
                       else if build_target.starts_with("aarch64") { "arm64" }
                       else if build_target.starts_with("i") { "x86" }
                       else if build_target.starts_with("x86_64") { "x86_64" }
                       else if build_target.starts_with("mips") { "mips" }
                       // TODO: mips64
                       else { panic!("Unknown or incompatible build target: {}", build_target) };
            config.ndk_path.join(format!("platforms/android-{v}/arch-{a}",
                                         v = config.android_version, a = arch))
        };

        // Create android cpu abi name
        let abi = if build_target.starts_with("arm") { "armeabi" }
                  // TODO: armeabi-v7a
                  else if build_target.starts_with("aarch64") { "arm64-v8a" }
                  else if build_target.starts_with("i") { "x86" }
                  else if build_target.starts_with("x86_64") { "x86_64" }
                  else if build_target.starts_with("mips") { "mips" }
                  // TODO: mips64
                  else { panic!("Unknown or incompatible build target: {}", build_target) };

        // Compiling android_native_app_glue.c
        {
            workspace.config().shell().say("Compiling android_native_app_glue.c", 10)?;
            let mut cmd = process(&gcc_path);
            cmd.arg(config.ndk_path.join("sources/android/native_app_glue/android_native_app_glue.c"))
               .arg("-c");
            if config.release {
                cmd.arg("-O3");
            }
            cmd.arg("-o").arg(build_target_dir.join("android_native_app_glue.o"))
               .arg("--sysroot").arg(&gcc_sysroot)
               .exec()?;
        }

        // Compiling injected-glue
        let injected_glue_lib = {
            workspace.config().shell().say("Compiling injected-glue", 10)?;
            let mut cmd = workspace.config().rustc()?.process();
            cmd.arg(android_artifacts_dir.join("injected-glue/lib.rs"))
               .arg("--crate-type").arg("rlib");
            if config.release {
                cmd.arg("-C")
                   .arg("opt-level=3");
            }
            cmd.arg("--crate-name").arg("cargo_apk_injected_glue")
               .arg("--target").arg(build_target)
               .arg("--out-dir").arg(&build_target_dir);

            cmd.exec()?;

            let stdout = cmd.arg("--print").arg("file-names")
                            .exec_with_output()?;
            let stdout = String::from_utf8(stdout.stdout).unwrap();

            build_target_dir.join(stdout.lines().next().unwrap())
        };

        // Compiling glue_obj.rs
        {
            let mut file = File::create(build_target_dir.join("glue_obj.rs")).unwrap();
            file.write_all(&include_bytes!("../../glue_obj.rs")[..]).unwrap();
        }
        
        {
            workspace.config().shell().say("Compiling glue_obj", 10)?;
            let mut cmd = workspace.config().rustc()?.process();
            cmd.arg(build_target_dir.join("glue_obj.rs"))
               .arg("--crate-type").arg("staticlib");
            if config.release {
                cmd.arg("-C")
                   .arg("opt-level=3");
            }
            cmd.arg("--target").arg(build_target)
               .arg("--extern").arg(format!("cargo_apk_injected_glue={}", injected_glue_lib.to_string_lossy()))
               .arg("--emit").arg("obj")
               .arg("-o").arg(build_target_dir.join("glue_obj.o"))
               .exec()?;
        }

        // Directory where we will put the native libraries for gradle to pick them up.
        let native_libraries_dir = android_artifacts_dir.join(format!("app/lib/{}", abi));

        if fs::metadata(&native_libraries_dir).is_err() {
            fs::DirBuilder::new().recursive(true).create(&native_libraries_dir).unwrap();
        }

        // Compiling the crate thanks to `cargo rustc`. We set the linker to `linker_exe`, a hacky
        // linker that will tweak the options passed to `gcc`.
        {
            workspace.config().shell().say("Compiling crate", 10)?;

            // Set the current environment variables so that they are picked up by gcc-rs when
            // compiling.
            env::set_var(&format!("CC_{}", build_target), gcc_path.as_os_str());
            env::set_var(&format!("CXX_{}", build_target), gxx_path.as_os_str());
            env::set_var(&format!("AR_{}", build_target), ar_path.as_os_str());
            env::set_var(&format!("CFLAGS_{}", build_target), &format!("--sysroot {}", gcc_sysroot.to_string_lossy()));
            env::set_var(&format!("CXXFLAGS_{}", build_target), &format!("--sysroot {}", gcc_sysroot.to_string_lossy()));

            let extra_args = vec![
                "-C".to_owned(), format!("linker={}", android_artifacts_dir.join(if cfg!(target_os = "windows") { "linker_exe.exe" } else { "linker_exe" }).to_string_lossy()),
                "--extern".to_owned(), format!("cargo_apk_injected_glue={}", injected_glue_lib.to_string_lossy()),
                "-C".to_owned(),"link-arg=--cargo-apk-gcc".to_owned(),
                "-C".to_owned(), format!("link-arg={}", gcc_path.as_os_str().to_str().unwrap().to_owned()),
                "-C".to_owned(),"link-arg=--cargo-apk-gcc-sysroot".to_owned(),
                "-C".to_owned(), format!("link-arg={}", gcc_sysroot.as_os_str().to_str().unwrap().to_owned()),
                "-C".to_owned(), "link-arg=--cargo-apk-native-app-glue".to_owned(),
                "-C".to_owned(), format!("link-arg={}", build_target_dir.join("android_native_app_glue.o").into_os_string().into_string().unwrap()),
                "-C".to_owned(), "link-arg=--cargo-apk-glue-obj".to_owned(),
                "-C".to_owned(), format!("link-arg={}", build_target_dir.join("glue_obj.o").into_os_string().into_string().unwrap()),
                "-C".to_owned(), "link-arg=--cargo-apk-glue-lib".to_owned(),
                "-C".to_owned(), format!("link-arg={}", injected_glue_lib.into_os_string().into_string().unwrap()),
                "-C".to_owned(), "link-arg=--cargo-apk-linker-output".to_owned(),
                "-C".to_owned(), format!("link-arg={}", native_libraries_dir.join("libmain.so").into_os_string().into_string().unwrap()),
                "-C".to_owned(), "link-arg=--cargo-apk-libs-path-output".to_owned(),
                "-C".to_owned(), format!("link-arg={}", build_target_dir.join("lib_paths").into_os_string().into_string().unwrap()),
                "-C".to_owned(), "link-arg=--cargo-apk-libs-output".to_owned(),
                "-C".to_owned(), format!("link-arg={}", build_target_dir.join("libs").into_os_string().into_string().unwrap()),
            ];

            let packages = Vec::from_iter(options.flag_package.iter().cloned());
            let spec = ops::Packages::Packages(&packages);

            let (mut examples, mut bins) = (Vec::new(), Vec::new());
            if let Some(ref s) = options.flag_bin {
                bins.push(s.clone());
            }
            if let Some(ref s) = options.flag_example {
                examples.push(s.clone());
            }
            if !bins.is_empty() && !examples.is_empty() {
                return Err(CargoError::from("You can only specify either a --bin or an --example but not both"));
            }

            let pkg = match spec {
                ops::Packages::All => unreachable!("cargo apk supports single package only"),
                ops::Packages::OptOut(_) => unreachable!("cargo apk supports single package only"),
                ops::Packages::Packages(xs) => match xs.len() {
                    0 => workspace.current()?,
                    1 => workspace.members()
                        .find(|pkg| pkg.name() == xs[0])
                        .ok_or_else(|| 
                            CargoError::from(
                                format!("package `{}` is not a member of the workspace", xs[0]))
                        )?,
                    _ => unreachable!("cargo apk supports single package only"),
                }
            };

            if bins.is_empty() && examples.is_empty() {
                bins = pkg.manifest().targets().iter().filter(|a| {
                    !a.is_lib() && !a.is_custom_build() && a.is_bin()
                }).map(|a| a.name().to_owned()).collect();
                if bins.len() >= 2 {
                    return Err(CargoError::from("`cargo apk` can run at most one executable, but \
                        multiple exist"));
                } else if bins.is_empty() {
                    return Err(CargoError::from("a bin target must be available for `cargo apk`"));
                }
            }

            let opts = ops::CompileOptions {
                config: workspace.config(),
                jobs: options.flag_jobs,
                target: Some(build_target),
                features: &options.flag_features,
                all_features: options.flag_all_features,
                no_default_features: options.flag_no_default_features,
                spec: spec,
                mode: ops::CompileMode::Build,
                release: options.flag_release,
                filter: ops::CompileFilter::new(false,
                                            &bins, false,
                                            &[], false,
                                            &examples, false,
                                            &[], false),
                message_format: options.flag_message_format,
                target_rustdoc_args: None,
                target_rustc_args: Some(&extra_args),
            };
            
            ops::compile(workspace, &opts)?;
        }

        // Determine the list of library paths and libraries, and copy them to the right location.
        let shared_objects_to_load = {
            let lib_paths: Vec<String> = {
                if let Ok(f) = File::open(build_target_dir.join("lib_paths")) {
                    let l = BufReader::new(f);
                    l.lines().map(|l| l.unwrap()).collect()
                } else {
                    vec![]
                }
            };

            let libs_list: HashSet<String> = {
                if let Ok(f) = File::open(build_target_dir.join("libs")) {
                    let l = BufReader::new(f);
                    l.lines().map(|l| l.unwrap()).collect()
                } else {
                    HashSet::new()
                }
            };

            let mut shared_objects_to_load = Vec::new();

            for dir in lib_paths.iter() {
                fs::read_dir(&dir).and_then(|paths| {
                    for path in paths {
                        let path = path.unwrap().path();
                        match (path.file_name(), path.extension()) {
                            (Some(filename), Some(ext)) => {
                                let filename = filename.to_str().unwrap();
                                if filename.starts_with("lib") && ext == "so" &&
                                   libs_list.contains(filename)
                                {
                                    shared_objects_to_load.push(filename.to_owned());
                                    fs::copy(&path, native_libraries_dir.join(filename)).unwrap();
                                }
                            }
                            _ => {}
                        }
                    }

                    Ok(())
                }).ok();
            }

            shared_objects_to_load
        };

        abi_libs.insert(abi, shared_objects_to_load);
    }

    // Write the Java source
    build_java_src(workspace, &android_artifacts_dir, &config, &abi_libs)?;

    // Invoking `gradle` from within `android-artifacts` in order to compile the project.
    workspace.config().shell().say("Invoking gradle", 10)?;
    let mut cmd = process(&config.gradle_command);
    if config.release {
        cmd.arg("assembleRelease");
    } else {
        cmd.arg("assembleDebug");
    }
    cmd.cwd(&android_artifacts_dir)
       .exec()?;

    Ok(BuildResult {
        apk_path: {
            let apk_name = if options.flag_release {
                "app/build/outputs/apk/app-release-unsigned.apk"
            } else {
                "app/build/outputs/apk/app-debug.apk"
            };

            android_artifacts_dir.join(apk_name)
        },
    })
}

fn build_android_artifacts_dir(workspace: &Workspace, path: &Path, config: &AndroidConfig) -> Result<(), CargoError> {
    fs::create_dir_all(path.join("app").join("src").join("main")).unwrap();

    {
        fs::create_dir_all(path.join("injected-glue")).unwrap();

        let mut lib = File::create(path.join("injected-glue/lib.rs")).unwrap();
        lib.write_all(&include_bytes!("../../injected-glue/lib.rs")[..]).unwrap();

        let mut ffi = File::create(path.join("injected-glue/ffi.rs")).unwrap();
        ffi.write_all(&include_bytes!("../../injected-glue/ffi.rs")[..]).unwrap();
    }

    build_linker(workspace, path)?;
    build_manifest(workspace, path, config)?;
    build_build_gradle_root(workspace, path, config)?;
    build_build_gradle_proj(workspace, path, config)?;
    build_settings_dot_gradle(workspace, path, config)?;
    build_gradle_properties(workspace, path, config)?;
    build_local_properties(workspace, path, config)?;
    build_assets(workspace, path, config)?;
    build_res(workspace, path, config)?;

    for target in config.build_targets.iter() {
        if fs::metadata(path.join(target)).is_err() {
            fs::DirBuilder::new().recursive(true).create(path.join(target)).unwrap();
        }
    }

    Ok(())
}

fn build_linker(workspace: &Workspace, path: &Path) -> Result<(), CargoError> {
    let exe_file = path.join(if cfg!(target_os = "windows") { "linker_exe.exe" } else { "linker_exe" });

    /*if fs::metadata(&exe_file).is_ok() {
        return;
    }*/

    let mut command = workspace.config().rustc()?.process().arg("-").arg("-o").arg(&exe_file).build_command();

    let mut child = command.stdin(Stdio::piped())
        .spawn()?;
    child.stdin.take().unwrap().write_all(&include_bytes!("../../linker.rs")[..])?;

    let status = child.wait()?;
    assert!(status.success());

    assert!(fs::metadata(&exe_file).is_ok());

    Ok(())
}

fn build_java_src(_: &Workspace, path: &Path, config: &AndroidConfig, abi_libs: &HashMap<&str, Vec<String>>) -> Result<(), CargoError>
{
    let file = path.join("app/src/main/java/rust").join(config.project_name.replace("-", "_"))
                   .join("MainActivity.java");
    fs::create_dir_all(file.parent().unwrap())?;
    //if fs::metadata(&file).is_ok() { return; }
    let mut file = File::create(&file)?;

    let mut libs_string = String::new();

    for (abi, libs) in abi_libs {

        libs_string.push_str(format!("            if (abi.equals(\"{}\")) {{\n",abi).as_str());
        libs_string.push_str(format!("                matched_an_abi = true;\n").as_str());

        for name in libs {
            // Strip off the 'lib' prefix and ".so" suffix.
            let line = format!("                System.loadLibrary(\"{}\");\n",
                name.trim_left_matches("lib").trim_right_matches(".so"));
            libs_string.push_str(&*line);
        }

        libs_string.push_str(format!("                break;\n").as_str());
        libs_string.push_str(format!("            }}\n").as_str());
    }

    write!(file, r#"package rust.{package_name};

import java.lang.UnsupportedOperationException;
import android.os.Build;
import android.util.Log;

public class MainActivity extends android.app.NativeActivity {{

    static {{

        String[] supported_abis;

        try {{
            supported_abis = (String[]) Build.class.getField("SUPPORTED_ABIS").get(null);
        }} catch (Exception e) {{
            // Assume that this is an older phone; use backwards-compatible targets.
            supported_abis = new String[]{{Build.CPU_ABI, Build.CPU_ABI2}};
        }}

        boolean matched_an_abi = false;

        for (String abi : supported_abis) {{
{libs}
        }}

        if (!matched_an_abi) {{
            throw new UnsupportedOperationException("Could not find a native abi target to load");
        }}

    }}
}}"#, libs = libs_string, package_name = config.project_name.replace("-", "_"))?;
    Ok(())
}

fn build_manifest(_: &Workspace, path: &Path, config: &AndroidConfig) -> Result<(), CargoError> {
    fs::create_dir_all(path.join("app/src/main")).unwrap();
    let file = path.join("app/src/main/AndroidManifest.xml");
    //if fs::metadata(&file).is_ok() { return; }
    let mut file = File::create(&file)?;

    // Building application attributes
    let application_attrs = format!(r#"
            android:label="{0}"{1}{2}{3}"#,
        config.package_label,
        config.package_icon.as_ref().map_or(String::new(), |a| format!(r#"
            android:icon="{}""#, a)),
        if config.fullscreen { r#"
            android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen""#
        } else { "" },
        config.application_attributes.as_ref().map_or(String::new(), |a| a.replace("\n","\n            "))
    );

    // Buidling activity attributes
    let activity_attrs = format!(r#"
                android:name="rust.{1}.MainActivity"
                android:label="{0}"
                android:configChanges="orientation|keyboardHidden|screenSize" {2}"#,
        config.package_label,
        config.project_name.replace("-", "_"),
        config.activity_attributes.as_ref().map_or(String::new(), |a| a.replace("\n","\n                "))
    );

    // Write final AndroidManifest
    write!(
        file, r#"<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="{package}"
        android:versionCode="1"
        android:versionName="1.0">

    <uses-sdk android:minSdkVersion="{minSdkVersion}" />

    <uses-feature android:glEsVersion="{glEsVersion}" android:required="true"></uses-feature>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application {application_attrs} >
        <activity {activity_attrs} >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<!-- END_INCLUDE(manifest) -->
"#,
        package = config.package_name.replace("-", "_"),
        minSdkVersion = config.android_version,
        glEsVersion = format!("0x{:04}{:04}", config.opengles_version_major, config.opengles_version_minor),
        application_attrs = application_attrs,
        activity_attrs = activity_attrs
    )?;
    Ok(())
}

fn build_assets(_: &Workspace, path: &Path, config: &AndroidConfig) -> Result<(), CargoError> {
    let src_path = match config.assets_path {
        None => return Ok(()),
        Some(ref p) => p,
    };
    let dst_path = path.join("app").join("src").join("main").join("assets");
    if !dst_path.exists() {
        create_dir_symlink(&src_path, &dst_path)?;
    }
    Ok(())
}

fn build_res(_: &Workspace, path: &Path, config: &AndroidConfig) -> Result<(), CargoError> {
    let src_path = match config.res_path {
        None => return Ok(()),
        Some(ref p) => p,
    };

    if !src_path.exists() {
        return Err(CargoError::from("Resources directory doesn't exist"));
    }

    let dst_path = path.join("app").join("src").join("main").join("res");
    if !dst_path.exists() {
        create_dir_symlink(&src_path, &dst_path)?;
    }

    Ok(())
}

#[cfg(target_os = "windows")]
fn create_dir_symlink(src_path: &Path, dst_path: &Path) -> io::Result<()> {
    os::windows::fs::symlink_dir(&src_path, &dst_path)
}

#[cfg(not(target_os = "windows"))]
fn create_dir_symlink(src_path: &Path, dst_path: &Path) -> io::Result<()> {
    os::unix::fs::symlink(&src_path, &dst_path)
}

fn build_build_gradle_root(_: &Workspace, path: &Path, config: &AndroidConfig) -> Result<(), CargoError> {
    let file = path.join("build.gradle");
    //if fs::metadata(&file).is_ok() { return; }
    let mut file = File::create(&file).unwrap();

    write!(file, r#"
buildscript {{
    repositories {{
        jcenter()
    }}
    dependencies {{
        classpath 'com.android.tools.build:gradle:2.3.3'
    }}
}}
allprojects {{
    repositories {{
        jcenter()
    }}
}}
ext {{
    compileSdkVersion = {android_version}
    buildToolsVersion = "{build_tools_version}"
}}
"#, android_version = config.android_version,
    build_tools_version = config.build_tools_version)?;
    Ok(())
}

fn build_build_gradle_proj(_: &Workspace, path: &Path, config: &AndroidConfig) -> Result<(), CargoError> {
    let file = path.join("app/build.gradle");
    //if fs::metadata(&file).is_ok() { return; }
    let mut file = File::create(&file).unwrap();

    write!(file, r#"
apply plugin: 'com.android.application'

android {{
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    sourceSets {{
        main {{
            jniLibs.srcDirs 'lib/'
        }}
    }}
}}
"#)?;
    Ok(())
}

fn build_settings_dot_gradle(_: &Workspace, path: &Path, _: &AndroidConfig) -> Result<(), CargoError> {
    let file = path.join("settings.gradle");
    //if fs::metadata(&file).is_ok() { return; }
    let mut file = File::create(&file)?;
    write!(file, r"include ':app'")?;
    Ok(())
}

fn build_gradle_properties(_: &Workspace, path: &Path, _: &AndroidConfig) -> Result<(), CargoError> {
    let file = path.join("gradle.properties");
    //if fs::metadata(&file).is_ok() { return; }
    let mut file = File::create(&file)?;
    write!(file, r"android.builder.sdkDownload=false")?;
    Ok(())
}

fn build_local_properties(_: &Workspace, path: &Path, config: &AndroidConfig) -> Result<(), CargoError> {
    let file = path.join("local.properties");
    //if fs::metadata(&file).is_ok() { return; }
    let mut file = File::create(&file)?;

    let sdk_abs_dir = if config.sdk_path.is_absolute() {
        config.sdk_path.clone()
    } else {
        env::current_dir()?.join(&config.sdk_path)
    };

    let ndk_abs_dir = if config.ndk_path.is_absolute() {
        config.ndk_path.clone()
    } else {
        env::current_dir()?.join(&config.ndk_path)
    };

    if cfg!(target_os = "windows") {
        writeln!(file, r"sdk.dir={}", sdk_abs_dir.to_str().unwrap().replace("\\", "\\\\"))?;
    } else {
        writeln!(file, r"sdk.dir={}", sdk_abs_dir.to_str().unwrap())?;
    }

    if cfg!(target_os = "windows") {
        writeln!(file, r"ndk.dir={}", ndk_abs_dir.to_str().unwrap().replace("\\", "\\\\"))?;
    } else {
        writeln!(file, r"ndk.dir={}", ndk_abs_dir.to_str().unwrap())?;
    }

    Ok(())
}