Add a script and documentation for building the Viewer APK.
authorDerek Sollenberger <djsollen@google.com>
Fri, 6 Jan 2017 16:41:56 +0000 (11:41 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Fri, 6 Jan 2017 17:14:31 +0000 (17:14 +0000)
BUG=skia:6009

DOCS_PREVIEW= https://skia.org/?cl=6660

Change-Id: Ibcf0d1e7f3519e703bc74f0329d0ac92110e70ec
Reviewed-on: https://skia-review.googlesource.com/6660
Commit-Queue: Derek Sollenberger <djsollen@google.com>
Reviewed-by: Mike Klein <mtklein@chromium.org>
platform_tools/android/apps/build.gradle
platform_tools/android/bin/android_build_app [new file with mode: 0755]
site/user/sample/viewer.md

index 5c49269..ee279e8 100644 (file)
@@ -23,13 +23,13 @@ def setupSkiaLibraryBuild(project, appVariants, appName) {
     appVariants.all{ variant ->
         def buildNativeLib = project.task("${variant.name}_BuildSkiaLib", type:Exec) {
             workingDir '../../../..' // top-level skia directory
-            commandLine constructBuildCommand(variant, appName).split()
+            commandLine constructBuildCommand(project, variant, appName).split()
         }
         buildNativeLib.onlyIf { !project.hasProperty("suppressNativeBuild") }
 
         def copyNativeLib = project.task("${variant.name}_CopySkiaLib", type:Copy) {
-            from getVariantOutDir(variant).skiaOut
-            into getVariantOutDir(variant).androidOut
+            from getVariantOutDir(project, variant).skiaOut
+            into getVariantOutDir(project, variant).androidOut
             include "${appName}.so"
         }
 
@@ -56,7 +56,7 @@ def getLocalProperties() {
     return properties
 }
 
-def getVariantOutDir(variant) {
+def getVariantOutDir(project, variant) {
     String variantPrefix = null
     String androidLibDir = null
     if (variant.name.startsWith("arm64")) {
@@ -79,11 +79,19 @@ def getVariantOutDir(variant) {
         androidLibDir = "mips64"
     }
 
-    return [skiaOut: getLocalProperties().getProperty("${variantPrefix}.out.dir", "missing_variant_out"),
+    String skiaOutDir = null
+    String propName = "${variantPrefix}.out.dir"
+    if (project.hasProperty(propName)) {
+        skiaOutDir = project.getProperties().getAt(propName)
+    } else {
+        skiaOutDir = getLocalProperties().getProperty(propName, "missing_variant_out")
+    }
+
+    return [skiaOut: skiaOutDir,
             androidOut: "src/main/libs/${androidLibDir}"]
 }
 
-def constructBuildCommand(variant, appName) {
+def constructBuildCommand(project, variant, appName) {
     String depotToolsDir = null
     for (String entry : System.getenv("PATH").split(":")) {
         if (entry.contains("depot_tools")) {
@@ -100,6 +108,6 @@ def constructBuildCommand(variant, appName) {
                 " depot_tools or define depot_tools.dir in local.properties")
     }
 
-    String out_dir = getVariantOutDir(variant).skiaOut
+    String out_dir = getVariantOutDir(project, variant).skiaOut
     return "${depotToolsDir}/ninja -C $out_dir $appName"
 }
diff --git a/platform_tools/android/bin/android_build_app b/platform_tools/android/bin/android_build_app
new file mode 100755 (executable)
index 0000000..cd044c1
--- /dev/null
@@ -0,0 +1,66 @@
+#!/usr/bin/python
+#
+# Copyright 2017 Google Inc.
+#
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import argparse
+import os
+import re
+import shutil
+import subprocess
+import sys
+
+parser = argparse.ArgumentParser(description='builds skia android apps')
+parser.add_argument('-C', '--output_dir', help='ninja out dir')
+parser.add_argument('app_name')
+
+args = parser.parse_args()
+
+target_cpu = "arm64"
+android_variant = ""
+android_buildtype = "debug"
+
+if args.output_dir == None:
+  sys.exit("unknown out directory")
+
+args_gn_path = os.path.join(args.output_dir, "args.gn")
+if os.path.exists(args_gn_path):
+  for line in open(args_gn_path):
+    m = re.match('target_cpu = "(.*)"', line.strip())
+    if m:
+      target_cpu = m.group(1)
+
+if target_cpu == "arm":
+  android_variant = "arm"
+elif target_cpu == "arm64":
+  android_variant = "arm64"
+elif target_cpu == "x86":
+  android_variant = "x86"
+elif target_cpu == "x64":
+  android_variant = "x86_64"
+elif target_cpu == "mipsel":
+  android_variant = "mips"
+elif target_cpu == "mips64el":
+  android_variant = "mips64"
+else:
+  sys.exit("unknown target_cpu")
+
+# build the apk using gradle
+try:
+    subprocess.check_call(['./apps/gradlew',
+      ':viewer:assemble' + android_variant + android_buildtype,
+      '-papps/' + args.app_name,
+      '-P' + target_cpu + 'out.dir=' + args.output_dir,
+      '--daemon'], cwd=os.path.join(os.path.dirname(__file__), ".."))
+except subprocess.CalledProcessError as error:
+  print error
+  sys.exit("gradle build failed")
+
+# copy apk back into the main out directory
+current_dir = os.path.dirname(__file__)
+apk_src = os.path.join(current_dir, "..", "apps", args.app_name, "build", "outputs", "apk",
+                       args.app_name + "-"  + android_variant + "-"  + android_buildtype + ".apk")
+apk_dst = os.path.join(args.output_dir, args.app_name + ".apk")
+shutil.copyfile(apk_src, apk_dst)
index 758858d..d41590a 100644 (file)
@@ -22,7 +22,13 @@ The Viewer can be built using the regular GN build process, e.g.
 
 Android
 -------
-GN support for the Android Viewer is in the process of being addressed.
+The Viewer APK must be built by gradle which can be invoked on the command line with the following script...
+
+    ./platform_tools/android/bin/android_build_app -C <out_dir> viewer
+
+*   **out_dir** is the ninja out directory that you want to use to build the app
+
+Upon completion of the script the APK can be found at <out_dir>/viewer.apk
 
 iOS
 ---