src/android: Added 3 metadata annotations for API Level related matters
[nit.git] / src / android_platform.nit
1 #
2 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Compile program for the Android platform
17 module android_platform
18
19 import platform
20 import abstract_compiler
21 import common_ffi
22 import android_annotations
23
24 redef class ToolContext
25 redef fun platform_from_name(name)
26 do
27 if name == "android" then return new AndroidPlatform
28 return super
29 end
30 end
31
32 class AndroidPlatform
33 super Platform
34
35 redef fun supports_libunwind do return false
36
37 redef fun toolchain(toolcontext) do return new AndroidToolchain(toolcontext)
38 end
39
40 class AndroidToolchain
41 super MakefileToolchain
42
43 var android_project_root: nullable String = null
44
45 redef fun compile_dir
46 do
47 var android_project_root = "{super}/android/"
48 self.android_project_root = android_project_root
49 return "{android_project_root}/jni/nit_compile/"
50 end
51
52 redef fun write_files(compiler, compile_dir, cfiles)
53 do
54 var android_project_root = android_project_root.as(not null)
55 var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
56 var short_project_name = compiler.mainmodule.name
57 var release = toolcontext.opt_release.value
58
59 var app_name = project.name
60 if app_name == null then app_name = compiler.mainmodule.name
61
62 var app_package = project.java_package
63 if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
64
65 var app_version = project.version
66 if app_version == null then app_version = "1.0"
67
68 var app_min_sdk = project.min_sdk
69 if app_min_sdk == null then app_min_sdk = 10
70
71 var app_target_sdk = project.target_sdk
72 if app_target_sdk == null then app_target_sdk = app_min_sdk
73
74 var app_max_sdk = ""
75 if project.max_sdk != null then app_max_sdk = "android:maxSdkVersion=\"{app_max_sdk}\""
76
77 # Clear the previous android project, so there is no "existing project warning"
78 # or conflict between Java files of different projects
79 if android_project_root.file_exists then android_project_root.rmdir
80
81 var args = ["android", "-s",
82 "create", "project",
83 "--name", short_project_name,
84 "--target", "android-{app_target_sdk}",
85 "--path", android_project_root,
86 "--package", app_package,
87 "--activity", short_project_name]
88 toolcontext.exec_and_check(args, "Android project error")
89
90 # create compile_dir
91 var dir = "{android_project_root}/jni/"
92 if not dir.file_exists then dir.mkdir
93
94 dir = compile_dir
95 if not dir.file_exists then dir.mkdir
96
97 # compile normal C files
98 super(compiler, compile_dir, cfiles)
99
100 # Gather extra C files generated elsewhere than in super
101 for f in compiler.extern_bodies do
102 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
103 end
104
105 ## Generate delagating makefile
106 dir = "{android_project_root}/jni/"
107 """
108 include $(call all-subdir-makefiles)
109 """.write_to_file("{dir}/Android.mk")
110
111 ### generate makefile into "{compile_dir}/Android.mk"
112 dir = compile_dir
113 """
114 LOCAL_PATH := $(call my-dir)
115 include $(CLEAR_VARS)
116
117 LOCAL_CFLAGS := -D ANDROID
118 LOCAL_MODULE := main
119 LOCAL_SRC_FILES := \\
120 {{{cfiles.join(" \\\n")}}}
121 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
122 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
123
124 include $(BUILD_SHARED_LIBRARY)
125
126 $(call import-module,android/native_app_glue)
127 """.write_to_file("{dir}/Android.mk")
128
129 ### generate AndroidManifest.xml
130 dir = android_project_root
131 """<?xml version="1.0" encoding="utf-8"?>
132 <!-- BEGIN_INCLUDE(manifest) -->
133 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
134 package="{{{app_package}}}"
135 android:versionCode="{{{project.version_code}}}"
136 android:versionName="{{{app_version}}}">
137
138 <!-- This is the platform API where NativeActivity was introduced. -->
139 <uses-sdk
140 android:minSdkVersion="{{{app_min_sdk}}}"
141 android:targetSdkVersion="{{{app_target_sdk}}}"
142 {{{app_max_sdk}}} />
143
144 <application
145 android:label="@string/app_name"
146 android:hasCode="true"
147 android:debuggable="{{{not release}}}">
148
149 <!-- Our activity is the built-in NativeActivity framework class.
150 This will take care of integrating with our NDK code. -->
151 <activity android:name="android.app.NativeActivity"
152 android:label="@string/app_name"
153 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
154 android:configChanges="orientation|keyboardHidden"
155 android:screenOrientation="portrait">
156 <!-- Tell NativeActivity the name of or .so -->
157 <meta-data android:name=\"{{{app_package}}}\"
158 android:value=\"{{{app_name}}}\" />
159 <intent-filter>
160 <action android:name="android.intent.action.MAIN" />
161 <category android:name="android.intent.category.LAUNCHER" />
162 </intent-filter>
163 </activity>
164
165 {{{project.manifest_application_lines.join("\n")}}}
166
167 </application>
168
169 {{{project.manifest_lines.join("\n")}}}
170
171 </manifest>
172 <!-- END_INCLUDE(manifest) -->
173 """.write_to_file("{dir}/AndroidManifest.xml")
174
175 ### Link to png sources
176 # libpng is not available on Android NDK
177 # FIXME make obtionnal when we have alternatives to mnit
178 var nit_dir = toolcontext.nit_dir
179 var share_dir = "{nit_dir or else ""}/share/"
180 if nit_dir == null or not share_dir.file_exists then
181 print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
182 exit 1
183 end
184 share_dir = share_dir.realpath
185 var target_png_dir = "{android_project_root}/jni/png"
186 if not target_png_dir.file_exists then
187 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir], "Android project error")
188 end
189
190 ### Link to assets (for mnit and others)
191 # This will be accessed from `android_project_root`
192 var assets_dir
193 if compiler.mainmodule.location.file != null then
194 # it is a real file, use "{file}/../assets"
195 assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
196 else
197 # probably used -m, use "."
198 assets_dir = "assets"
199 end
200 if assets_dir.file_exists then
201 assets_dir = assets_dir.realpath
202 var target_assets_dir = "{android_project_root}/assets"
203 if not target_assets_dir.file_exists then
204 toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir], "Android project error")
205 end
206 end
207
208 ### copy resources (for android)
209 # This will be accessed from `android_project_root`
210 var res_dir
211 if compiler.mainmodule.location.file != null then
212 # it is a real file, use "{file}/../res"
213 res_dir = "{compiler.mainmodule.location.file.filename.dirname}/../res"
214 else
215 # probably used -m, use "."
216 res_dir = "res"
217 end
218 if res_dir.file_exists then
219 res_dir = res_dir.realpath
220 var target_res_dir = "{android_project_root}"
221 if target_res_dir.file_exists then
222 # copy the res folder to .nit_compile
223 toolcontext.exec_and_check(["cp", "-R", res_dir, target_res_dir], "Android project error")
224 end
225 end
226
227 end
228
229 redef fun write_makefile(compiler, compile_dir, cfiles)
230 do
231 # Do nothing, already done in `write_files`
232 end
233
234 redef fun compile_c_code(compiler, compile_dir)
235 do
236 var android_project_root = android_project_root.as(not null)
237 var release = toolcontext.opt_release.value
238
239 # Compile C code (and thus Nit)
240 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root], "Android project error")
241
242 # Generate the apk
243 var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
244 if release then
245 args.add "release"
246 else args.add "debug"
247 toolcontext.exec_and_check(args, "Android project error")
248
249 # Move the apk to the target
250 var outname = toolcontext.opt_output.value
251 if outname == null then outname = "{compiler.mainmodule.name}.apk"
252
253 var src_apk_suffix
254 if release then
255 src_apk_suffix = "release-unsigned"
256 else src_apk_suffix = "debug"
257
258 toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname], "Android project error")
259 end
260 end
261
262 redef class JavaClassTemplate
263 redef fun write_to_files(compdir)
264 do
265 var jni_path = "jni/nit_compile/"
266 if compdir.has_suffix(jni_path) then
267 var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
268 return super(path)
269 else return super
270 end
271 end