nitc iOS: quiet output of `xcodebuild`
[nit.git] / src / platform / android.nit
index e2db002..adb9543 100644 (file)
@@ -36,13 +36,13 @@ class AndroidPlatform
 
        redef fun name do return "android"
 
-       redef fun supports_libgc do return true
+       redef fun supports_libgc do return false
 
        redef fun supports_libunwind do return false
 
        redef fun supports_linker_script do return false
 
-       redef fun toolchain(toolcontext) do return new AndroidToolchain(toolcontext)
+       redef fun toolchain(toolcontext, compiler) do return new AndroidToolchain(toolcontext, compiler)
 end
 
 class AndroidToolchain
@@ -52,30 +52,28 @@ class AndroidToolchain
 
        redef fun compile_dir
        do
-               var android_project_root = "{super}/android/"
+               var android_project_root = "{root_compile_dir}/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 default_outname do return "{super}.apk"
 
-       redef fun write_files(compiler, compile_dir, cfiles)
+       redef fun write_files(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.replace("-", "_")
+               var project = new AndroidProject(toolcontext.modelbuilder, compiler.mainmodule)
                var release = toolcontext.opt_release.value
 
                var app_name = project.name
-               if app_name == null then app_name = compiler.mainmodule.name
                if not release then app_name += " Debug"
 
-               var app_package = project.java_package
-               if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
+               var short_project_name = project.short_name
+
+               var app_package = project.namespace
                if not release then app_package += "_debug"
 
                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
@@ -106,19 +104,65 @@ class AndroidToolchain
                dir = compile_dir
                if not dir.file_exists then dir.mkdir
 
+               # Insert an importation of the generated R class to all Java files from the FFI
+               for mod in compiler.mainmodule.in_importation.greaters do
+                       var java_ffi_file = mod.java_file
+                       if java_ffi_file != null then java_ffi_file.add "import {app_package}.R;"
+               end
+
                # compile normal C files
-               super(compiler, compile_dir, cfiles)
+               super
 
                # Gather extra C files generated elsewhere than in super
                for f in compiler.extern_bodies do
-                       if f isa ExternCFile then cfiles.add(f.filename.basename(""))
+                       if f isa ExternCFile then cfiles.add(f.filename.basename)
+               end
+
+               var project_root = "."
+               var mpackage = compiler.mainmodule.first_real_mmodule.mpackage
+               if mpackage != null then
+                       var root = mpackage.root
+                       if root != null then
+                               var filepath = root.filepath
+                               if filepath != null then
+                                       project_root = filepath
+                               end
+                       end
+               end
+
+               # Set the default pretty application name
+"""<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="app_name">{{{app_name}}}</string>
+</resources>""".write_to_file "{android_project_root}/res/values/strings.xml"
+
+               # Copy assets, resources and libs where expected by the SDK
+
+               ## Collect path to all possible folder where we can find the `android` folder
+               var app_files = [project_root]
+               app_files.add_all project.files
+
+               for path in app_files do
+                       # Copy the assets folder
+                       var assets_dir = path / "assets"
+                       if assets_dir.file_exists then
+                               assets_dir = assets_dir.realpath
+                               toolcontext.exec_and_check(["cp", "-r", assets_dir, android_project_root], "Android project error")
+                       end
+
+                       # Copy the whole `android` folder
+                       var android_dir = path / "android"
+                       if android_dir.file_exists then
+                               android_dir = android_dir.realpath
+                               toolcontext.exec_and_check(["cp", "-r", android_dir, root_compile_dir], "Android project error")
+                       end
                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"
+                       var path = project_root / "android/res/drawable-{res}/icon.png"
                        if path.file_exists then
                                icon_available = true
                                break
@@ -136,14 +180,14 @@ class AndroidToolchain
                        var extra_java_files = mmodule.extra_java_files
                        if extra_java_files != null then for file in extra_java_files do
                                var path = file.filename
-                               path.file_copy_to(dir/path.basename(""))
+                               path.file_copy_to(dir/path.basename)
                        end
                end
 
                ## Generate Application.mk
                dir = "{android_project_root}/jni/"
                """
-APP_ABI := armeabi armeabi-v7a x86 mips
+APP_ABI := armeabi armeabi-v7a x86
 APP_PLATFORM := android-{{{app_target_api}}}
 """.write_to_file "{dir}/Application.mk"
 
@@ -172,7 +216,7 @@ LOCAL_MODULE    := main
 LOCAL_SRC_FILES := \\
 {{{cfiles.join(" \\\n")}}}
 LOCAL_LDLIBS    := {{{ldflags.join(" ")}}} $(TARGET_ARCH)/libgc.a
-LOCAL_STATIC_LIBRARIES := android_native_app_glue png
+LOCAL_STATIC_LIBRARIES := android_native_app_glue
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -199,8 +243,7 @@ $(call import-module,android/native_app_glue)
                android:label="@string/app_name"
                android:hasCode="true"
                android:debuggable="{{{not release}}}"
-               {{{icon_declaration}}}
-               android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation">
+               {{{icon_declaration}}}>
 """
 
                for activity in project.activities do
@@ -231,7 +274,7 @@ $(call import-module,android/native_app_glue)
 
                ### Link to png sources
                # libpng is not available on Android NDK
-               # FIXME make obtionnal when we have alternatives to mnit
+               # FIXME make optional when we have alternatives to mnit
                var nit_dir = toolcontext.nit_dir
                var share_dir =  nit_dir/"share/"
                if not share_dir.file_exists then
@@ -239,10 +282,6 @@ $(call import-module,android/native_app_glue)
                        exit 1
                end
                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], "Android project error")
-               end
 
                # Ensure that android-setup-libgc.sh has been executed
                if not "{share_dir}/libgc/arm/lib".file_exists then
@@ -258,65 +297,15 @@ $(call import-module,android/native_app_glue)
                end
 
                toolcontext.exec_and_check(["ln", "-s", "{share_dir}/libgc/arm/include/gc/",
-                       "{android_project_root}/jni/nit_compile/gc"], "Android project error")
-
-               ### Link to assets (for mnit and others)
-               # This will be accessed from `android_project_root`
-               var assets_dir
-               if compiler.mainmodule.location.file != null then
-                       # it is a real file, use "{file}/../assets"
-                       assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
-               else
-                       # probably used -m, use "."
-                       assets_dir = "assets"
-               end
-               if assets_dir.file_exists then
-                       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], "Android project error")
-                       end
-               end
-
-               ### Copy resources and libs where expected by the SDK
-               var project_root
-               if compiler.mainmodule.location.file != null then
-                       # it is a real file, use "{file}/../res"
-                       project_root = "{compiler.mainmodule.location.file.filename.dirname}/.."
-               else
-                       # probably used -m, use "."
-                       project_root = "."
-               end
-
-               # Android resources folder
-               var res_dir = project_root / "res"
-               if res_dir.file_exists then
-                       # copy the res folder to .nit_compile
-                       res_dir = res_dir.realpath
-                       toolcontext.exec_and_check(["cp", "-R", res_dir, android_project_root], "Android project error")
-               end
-
-               if not res_dir.file_exists or not "{res_dir}/values/strings.xml".file_exists then
-                       # 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 "{android_project_root}/res/values/strings.xml"
-               end
-
-               # Android libs folder
-               var libs_dir = project_root / "libs"
-               if libs_dir.file_exists then
-                       toolcontext.exec_and_check(["cp", "-r", libs_dir, android_project_root], "Android project error")
-               end
+                       "{compile_dir}/gc"], "Android project error")
        end
 
-       redef fun write_makefile(compiler, compile_dir, cfiles)
+       redef fun write_makefile(compile_dir, cfiles)
        do
                # Do nothing, already done in `write_files`
        end
 
-       redef fun compile_c_code(compiler, compile_dir)
+       redef fun compile_c_code(compile_dir)
        do
                var android_project_root = android_project_root.as(not null)
                var short_project_name = compiler.mainmodule.name.replace("-", "_")
@@ -326,7 +315,7 @@ $(call import-module,android/native_app_glue)
                toolcontext.exec_and_check(["ndk-build", "-s", "-j", "-C", android_project_root], "Android project error")
 
                # Generate the apk
-               var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
+               var args = ["ant", "-f", android_project_root+"/build.xml"]
                if release then
                        args.add "release"
                else args.add "debug"
@@ -344,10 +333,16 @@ $(call import-module,android/native_app_glue)
                        var tsa_server= "TSA_SERVER".environ
 
                        if key_alias.is_empty then
-                               toolcontext.fatal_error(null,
-                                       "Fatal Error: the environment variable `KEY_ALIAS` must be set to use the `--release` option on Android projects.")
+                               toolcontext.warning(null, "key-alias",
+                                       "Warning: the environment variable `KEY_ALIAS` is not set, the APK file will not be signed.")
+
+                               # Just move the unsigned APK to outname
+                               args = ["mv", apk_path, outname]
+                               toolcontext.exec_and_check(args, "Android project error")
+                               return
                        end
 
+                       # We have a key_alias, try to sign the APK
                        args = ["jarsigner", "-sigalg", "MD5withRSA", "-digestalg", "SHA1", apk_path, key_alias]
 
                        ## Use a custom keystore