src: use `as_notnullable` in code
[nit.git] / src / android_platform.nit
index 6f762e0..1bec914 100644 (file)
@@ -1,4 +1,3 @@
-# This file is part of NIT ( http://www.nitlanguage.org )
 #
 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
 #
@@ -45,11 +44,13 @@ 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)
@@ -66,10 +67,23 @@ 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]
@@ -124,7 +138,10 @@ $(call import-module,android/native_app_glue)
         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"
@@ -157,22 +174,11 @@ $(call import-module,android/native_app_glue)
 <!-- END_INCLUDE(manifest) -->
                """.write_to_file("{dir}/AndroidManifest.xml")
 
-               ### 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
-               """<?xml version="1.0" encoding="utf-8"?>
-<resources>
-    <string name="app_name">{{{app_name}}}</string>
-</resources>
-               """.write_to_file("{dir}/strings.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
@@ -200,6 +206,29 @@ $(call import-module,android/native_app_glue)
                                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)
@@ -223,8 +252,7 @@ $(call import-module,android/native_app_glue)
                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"
+               var outname = outfile(compiler.mainmodule)
 
                var src_apk_suffix
                if release then