android: accept adaptive icons and other previous icon formats
authorAlexis Laferrière <alexis.laf@xymus.net>
Wed, 15 Nov 2017 16:08:44 +0000 (11:08 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 28 Nov 2017 14:31:29 +0000 (09:31 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/android/README.md
src/platform/android.nit

index 457f426..ffd961c 100644 (file)
@@ -102,6 +102,20 @@ Importing `android::landscape` or `android::portrait` locks the generated
 application in the specified orientation. This can be useful for games and
 other multimedia applications.
 
+## Resources and application icon
+
+Resources specific to the Android platform should be placed in an `android/` folder at the root of the project.
+The folder should adopt the structure of a normal Android project, e.g., a custom XML resource file can be placed
+at `android/res/values/color.xml` to be compiled with the Android application.
+
+The application icon should also be placed in the `android/` folder.
+Place the classic bitmap version at `android/res/mipmap-hdpi/ic_launcher.png` (and others),
+and the adaptive version at `android/res/mipmap-anydpi-v26/ic_launcher.xml`.
+The Nit compiler detects these files and uses them as the application icon.
+
+Additional `android/` folders may be placed next to more specific Nit modules to change the Android resources
+for application variants. The more specific resources will have priority over the project level `android/` files.
+
 # Compilation modes
 
 There are two compilation modes for the Android platform, debug and release.
index f41f9a4..7109e09 100644 (file)
@@ -378,19 +378,41 @@ target_link_libraries(nit_app gc-lib
                # Generate AndroidManifest.xml
 
                # Is there an icon?
-               var resolutions = ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
-               var icon_available = false
+               var resolutions = ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi", "anydpi", "anydpi-v26"]
+               var icon_name = null
+               var has_round = false
+
                for res in resolutions do
-                       var path = project_root / "android/res/drawable-{res}/icon.png"
-                       if path.file_exists then
-                               icon_available = true
+                       # New style mipmap
+                       if "{project_root}/android/res/mipmap-{res}/ic_launcher_round.png".file_exists then
+                               has_round = true
+                       end
+                       if "{project_root}/android/res/mipmap-{res}/ic_launcher.png".file_exists then
+                               icon_name = "@mipmap/ic_launcher"
+                               break
+                       end
+                       if "{project_root}/android/res/mipmap-{res}/ic_launcher.xml".file_exists then
+                               icon_name = "@mipmap/ic_launcher"
                                break
                        end
                end
+               if icon_name == null then
+                       # Old style drawable-hdpi/icon.png
+                       for res in resolutions do
+                               var path = project_root / "android/res/drawable-{res}/icon.png"
+                               if path.file_exists then
+                                       icon_name = "@drawable/icon"
+                                       break
+                               end
+                       end
+               end
 
                var icon_declaration
-               if icon_available then
-                       icon_declaration = "android:icon=\"@drawable/icon\""
+               if icon_name != null then
+                       icon_declaration = "android:icon=\"{icon_name}\""
+                       if app_target_api >= 25 and has_round then
+                               icon_declaration += "\n\t\tandroid:roundIcon=\"@mipmap/ic_launcher_round\""
+                       end
                else icon_declaration = ""
 
                # TODO android:roundIcon