From: Frédéric Vachon Date: Wed, 16 Jul 2014 01:55:18 +0000 (-0400) Subject: src/android: Added 3 metadata annotations for API Level related matters X-Git-Tag: v0.6.7~44^2~1 X-Git-Url: http://nitlanguage.org src/android: Added 3 metadata annotations for API Level related matters Signed-off-by: Frédéric Vachon --- diff --git a/src/android_annotations.nit b/src/android_annotations.nit index 0ae0078..39037e2 100644 --- a/src/android_annotations.nit +++ b/src/android_annotations.nit @@ -44,6 +44,15 @@ class AndroidProject # Custom lines to add to the AndroidManifest.xml in the node var manifest_application_lines = new Array[String] + # Minimum API level required for the application to run + var min_sdk: nullable Int = null + + # Build target API level + var target_sdk: nullable Int = null + + # Maximum API level on which the application will be allowed to run + var max_sdk: nullable Int = null + redef fun to_s do return """ name: {{{name or else "null"}}} namespace: {{{java_package or else "null"}}} @@ -65,7 +74,16 @@ redef class ModelBuilder annot = priority_annotation_on_modules("java_package", mmodule) if annot != null then project.java_package = annot.arg_as_string(self) - var annots = collect_annotations_on_modules("android_manifest", mmodule) + var annots = collect_annotations_on_modules("min_sdk_version", mmodule) + for an in annots do project.min_sdk = an.arg_as_int(self) + + annots = collect_annotations_on_modules("max_sdk_version", mmodule) + for an in annots do project.max_sdk = an.arg_as_int(self) + + annots = collect_annotations_on_modules("target_sdk_version", mmodule) + for an in annots do project.target_sdk = an.arg_as_int(self) + + annots = collect_annotations_on_modules("android_manifest", mmodule) for an in annots do project.manifest_lines.add an.arg_as_string(self) annots = collect_annotations_on_modules("android_manifest_application", mmodule) @@ -161,6 +179,34 @@ redef class AAnnotation end end + # Get the single argument of `self` as an `Int`. Raise error on any inconsistency. + private fun arg_as_int(modelbuilder: ModelBuilder): nullable Int + do + var annotation_name = n_atid.n_id.text + var format_error = "Annotation error: \"{annotation_name}\" expects a single Int as argument." + + var args = n_args + var platform_name + if args.length != 1 then + modelbuilder.error(self, format_error) + return null + else + var arg = args.first + + if not arg isa AExprAtArg then + modelbuilder.error(self, format_error) + return null + end + + var expr = arg.n_expr + if not expr isa AIntExpr then + modelbuilder.error(self, format_error) + return null + end + return expr.value.as(not null) + end + end + # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`. # # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short diff --git a/src/android_platform.nit b/src/android_platform.nit index d5498d6..5a8c89d 100644 --- a/src/android_platform.nit +++ b/src/android_platform.nit @@ -1,4 +1,3 @@ -# This file is part of NIT ( http://www.nitlanguage.org ) # # Copyright 2014 Alexis Laferrière # @@ -66,6 +65,15 @@ class AndroidToolchain var app_version = project.version if app_version == null then app_version = "1.0" + var app_min_sdk = project.min_sdk + if app_min_sdk == null then app_min_sdk = 10 + + var app_target_sdk = project.target_sdk + if app_target_sdk == null then app_target_sdk = app_min_sdk + + var app_max_sdk = "" + if project.max_sdk != null then app_max_sdk = "android:maxSdkVersion=\"{app_max_sdk}\"" + # 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 @@ -73,7 +81,7 @@ class AndroidToolchain var args = ["android", "-s", "create", "project", "--name", short_project_name, - "--target", "android-10", + "--target", "android-{app_target_sdk}", "--path", android_project_root, "--package", app_package, "--activity", short_project_name] @@ -128,7 +136,10 @@ $(call import-module,android/native_app_glue) android:versionName="{{{app_version}}}"> - +