android: search for an icon in the res folder
[nit.git] / src / android_platform.nit
index 4052d20..e3afc71 100644 (file)
@@ -1,4 +1,3 @@
-# This file is part of NIT ( http://www.nitlanguage.org )t
 #
 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
 #
@@ -28,30 +27,6 @@ redef class ToolContext
                if name == "android" then return new AndroidPlatform
                return super
        end
-
-       fun exec_and_check(args: Array[String])
-       do
-               var prog = args.first
-               args.remove_at 0
-
-               # Is the wanted program available?
-               var proc_which = new IProcess.from_a("which", [prog])
-               proc_which.wait
-               var res = proc_which.status
-               if res != 0 then
-                       print "Android project error: executable \"{prog}\" not found"
-                       exit 1
-               end
-
-               # Execute the wanted program
-               var proc = new Process.from_a(prog, args)
-               proc.wait
-               res = proc.status
-               if res != 0 then
-                       print "Android project error: execution of \"{prog} {args.join(" ")}\" failed"
-                       exit 1
-               end
-       end
 end
 
 class AndroidPlatform
@@ -69,20 +44,22 @@ class AndroidToolchain
 
        redef fun compile_dir
        do
-               var normal_compile_dir = super
-               android_project_root = normal_compile_dir
-               return "{normal_compile_dir}/jni/nit_compile/"
+               var android_project_root = "{super}/android/"
+               self.android_project_root = android_project_root
+               return "{android_project_root}/jni/nit_compile/"
        end
 
+       redef fun default_outname(mainmodule) do return "{mainmodule.name}.apk"
+
        redef fun write_files(compiler, compile_dir, cfiles)
        do
                var android_project_root = android_project_root.as(not null)
                var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
                var short_project_name = compiler.mainmodule.name
+               var release = toolcontext.opt_release.value
 
                var app_name = project.name
                if app_name == null then app_name = compiler.mainmodule.name
-               print app_name
 
                var app_package = project.java_package
                if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
@@ -90,14 +67,27 @@ class AndroidToolchain
                var app_version = project.version
                if app_version == null then app_version = "1.0"
 
+               var app_min_api = project.min_api
+               if app_min_api == null then app_min_api = 10
+
+               var app_target_api = project.target_api
+               if app_target_api == null then app_target_api = app_min_api
+
+               var app_max_api = ""
+               if project.max_api != null then app_max_api = "android:maxSdkVersion=\"{project.max_api.as(not null)}\""
+
+               # Clear the previous android project, so there is no "existing project warning"
+               # or conflict between Java files of different projects
+               if android_project_root.file_exists then android_project_root.rmdir
+
                var args = ["android", "-s",
                        "create", "project",
                        "--name", short_project_name,
-                       "--target", "android-10",
+                       "--target", "android-{app_target_api}",
                        "--path", android_project_root,
                        "--package", app_package,
                        "--activity", short_project_name]
-               toolcontext.exec_and_check(args)
+               toolcontext.exec_and_check(args, "Android project error")
 
                # create compile_dir
                var dir = "{android_project_root}/jni/"
@@ -114,18 +104,31 @@ class AndroidToolchain
                        if f isa ExternCFile then cfiles.add(f.filename.basename(""))
                end
 
+               # Is there an icon?
+               var resolutions = ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
+               var icon_available = false
+               for res in resolutions do
+                       var path = "res/drawable-{res}/icon.png"
+                       if path.file_exists then
+                               icon_available = true
+                               break
+                       end
+               end
+
+               var icon_declaration
+               if icon_available then
+                       icon_declaration = "android:icon=\"@drawable/icon\""
+               else icon_declaration = ""
+
                ## Generate delagating makefile
                dir = "{android_project_root}/jni/"
-               var file = new OFStream.open("{dir}/Android.mk")
-               file.write """
+               """
 include $(call all-subdir-makefiles)
-"""
-               file.close
+               """.write_to_file("{dir}/Android.mk")
 
                ### generate makefile into "{compile_dir}/Android.mk"
                dir = compile_dir
-               file = new OFStream.open("{dir}/Android.mk")
-               file.write """
+               """
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
@@ -139,27 +142,27 @@ LOCAL_STATIC_LIBRARIES := android_native_app_glue png
 include $(BUILD_SHARED_LIBRARY)
 
 $(call import-module,android/native_app_glue)
-"""
-               file.close
+               """.write_to_file("{dir}/Android.mk")
 
                ### generate AndroidManifest.xml
                dir = android_project_root
-               file = new OFStream.open("{dir}/AndroidManifest.xml")
-               file.write """<?xml version="1.0" encoding="utf-8"?>
+               """<?xml version="1.0" encoding="utf-8"?>
 <!-- BEGIN_INCLUDE(manifest) -->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="{{{app_package}}}"
         android:versionCode="{{{project.version_code}}}"
-        android:versionName="{{{app_version}}}"
-        android:debuggable="true">
+        android:versionName="{{{app_version}}}">
 
     <!-- This is the platform API where NativeActivity was introduced. -->
-    <uses-sdk android:minSdkVersion="9" />
+    <uses-sdk
+        android:minSdkVersion="{{{app_min_api}}}"
+        android:targetSdkVersion="{{{app_target_api}}}"
+        {{{app_max_api}}} />
 
     <application
                android:label="@string/app_name"
                android:hasCode="true"
-               android:debuggable="true">
+               android:debuggable="{{{not release}}}">
 
         <!-- Our activity is the built-in NativeActivity framework class.
              This will take care of integrating with our NDK code. -->
@@ -167,7 +170,8 @@ $(call import-module,android/native_app_glue)
                 android:label="@string/app_name"
                 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                 android:configChanges="orientation|keyboardHidden"
-                android:screenOrientation="portrait">
+                android:screenOrientation="portrait"
+                {{{icon_declaration}}}>
             <!-- Tell NativeActivity the name of or .so -->
             <meta-data android:name=\"{{{app_package}}}\"
                     android:value=\"{{{app_name}}}\" />
@@ -183,28 +187,15 @@ $(call import-module,android/native_app_glue)
 
 {{{project.manifest_lines.join("\n")}}}
 
-</manifest> 
+</manifest>
 <!-- END_INCLUDE(manifest) -->
-"""
-               file.close
-
-               ### generate res/values/strings.xml
-               dir = "{android_project_root}/res/"
-               if not dir.file_exists then dir.mkdir
-               dir = "{dir}/values/"
-               if not dir.file_exists then dir.mkdir
-               file = new OFStream.open("{dir}/strings.xml")
-               file.write """<?xml version="1.0" encoding="utf-8"?>
-<resources>
-    <string name="app_name">{{{app_name}}}</string>
-</resources>"""
-               file.close
+               """.write_to_file("{dir}/AndroidManifest.xml")
 
                ### Link to png sources
                # libpng is not available on Android NDK
                # FIXME make obtionnal when we have alternatives to mnit
                var nit_dir = toolcontext.nit_dir
-               var share_dir =  "{nit_dir}/share/"
+               var share_dir =  "{nit_dir or else ""}/share/"
                if nit_dir == null or not share_dir.file_exists then
                        print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
                        exit 1
@@ -212,7 +203,7 @@ $(call import-module,android/native_app_glue)
                share_dir = share_dir.realpath
                var target_png_dir = "{android_project_root}/jni/png"
                if not target_png_dir.file_exists then
-                       toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir])
+                       toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir], "Android project error")
                end
 
                ### Link to assets (for mnit and others)
@@ -229,9 +220,32 @@ $(call import-module,android/native_app_glue)
                        assets_dir = assets_dir.realpath
                        var target_assets_dir = "{android_project_root}/assets"
                        if not target_assets_dir.file_exists then
-                               toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir])
+                               toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir], "Android project error")
                        end
                end
+
+               ### copy resources  (for android)
+               # This will be accessed from `android_project_root`
+               var res_dir
+               if compiler.mainmodule.location.file != null then
+                       # it is a real file, use "{file}/../res"
+                       res_dir = "{compiler.mainmodule.location.file.filename.dirname}/../res"
+               else
+                       # probably used -m, use "."
+                       res_dir = "res"
+               end
+               if res_dir.file_exists then
+                       # copy the res folder to .nit_compile
+                       res_dir = res_dir.realpath
+                       var target_res_dir = "{android_project_root}"
+                       toolcontext.exec_and_check(["cp", "-R", res_dir, target_res_dir], "Android project error")
+               else
+                       # Create our own custom `res/values/string.xml` with the App name
+"""<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="app_name">{{{app_name}}}</string>
+</resources>""".write_to_file "{dir}/res/values/strings.xml"
+               end
        end
 
        redef fun write_makefile(compiler, compile_dir, cfiles)
@@ -242,16 +256,27 @@ $(call import-module,android/native_app_glue)
        redef fun compile_c_code(compiler, compile_dir)
        do
                var android_project_root = android_project_root.as(not null)
+               var release = toolcontext.opt_release.value
+
                # Compile C code (and thus Nit)
-               toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root])
+               toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root], "Android project error")
 
                # Generate the apk
-               toolcontext.exec_and_check(["ant", "-q", "debug", "-f", android_project_root+"/build.xml"])
+               var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
+               if release then
+                       args.add "release"
+               else args.add "debug"
+               toolcontext.exec_and_check(args, "Android project error")
 
                # Move the apk to the target
-               var outname = toolcontext.opt_output.value
-               if outname == null then outname = "{compiler.mainmodule.name}.apk"
-               toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-debug.apk", outname])
+               var outname = outfile(compiler.mainmodule)
+
+               var src_apk_suffix
+               if release then
+                       src_apk_suffix = "release-unsigned"
+               else src_apk_suffix = "debug"
+
+               toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname], "Android project error")
        end
 end