78ba8205c113d787ccb6367b503a9a64a73a1ed8
[nit.git] / src / platform / android.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Compile program for the Android platform
18 module android
19
20 import platform
21 import compiler::abstract_compiler
22 import ffi
23 intrude import ffi::extra_java_files
24 import android_annotations
25
26 redef class ToolContext
27 redef fun platform_from_name(name)
28 do
29 if name == "android" then return new AndroidPlatform
30 return super
31 end
32 end
33
34 class AndroidPlatform
35 super Platform
36
37 redef fun supports_libgc do return true
38
39 redef fun supports_libunwind do return false
40
41 redef fun supports_linker_script do return false
42
43 redef fun toolchain(toolcontext) do return new AndroidToolchain(toolcontext)
44 end
45
46 class AndroidToolchain
47 super MakefileToolchain
48
49 var android_project_root: nullable String = null
50
51 redef fun compile_dir
52 do
53 var android_project_root = "{super}/android/"
54 self.android_project_root = android_project_root
55 return "{android_project_root}/jni/nit_compile/"
56 end
57
58 redef fun default_outname(mainmodule) do return "{mainmodule.name}.apk"
59
60 redef fun write_files(compiler, compile_dir, cfiles)
61 do
62 var android_project_root = android_project_root.as(not null)
63 var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
64 var short_project_name = compiler.mainmodule.name
65 var release = toolcontext.opt_release.value
66
67 var app_name = project.name
68 if app_name == null then app_name = compiler.mainmodule.name
69
70 var app_package = project.java_package
71 if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
72
73 var app_version = project.version
74 if app_version == null then app_version = "1.0"
75
76 var app_min_api = project.min_api
77 if app_min_api == null then app_min_api = 10
78
79 var app_target_api = project.target_api
80 if app_target_api == null then app_target_api = app_min_api
81
82 var app_max_api = ""
83 if project.max_api != null then app_max_api = "android:maxSdkVersion=\"{project.max_api.as(not null)}\""
84
85 # Clear the previous android project, so there is no "existing project warning"
86 # or conflict between Java files of different projects
87 if android_project_root.file_exists then android_project_root.rmdir
88
89 var args = ["android", "-s",
90 "create", "project",
91 "--name", short_project_name,
92 "--target", "android-{app_target_api}",
93 "--path", android_project_root,
94 "--package", app_package,
95 "--activity", short_project_name]
96 toolcontext.exec_and_check(args, "Android project error")
97
98 # create compile_dir
99 var dir = "{android_project_root}/jni/"
100 if not dir.file_exists then dir.mkdir
101
102 dir = compile_dir
103 if not dir.file_exists then dir.mkdir
104
105 # compile normal C files
106 super(compiler, compile_dir, cfiles)
107
108 # Gather extra C files generated elsewhere than in super
109 for f in compiler.extern_bodies do
110 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
111 end
112
113 # Is there an icon?
114 var resolutions = ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
115 var icon_available = false
116 for res in resolutions do
117 var path = "res/drawable-{res}/icon.png"
118 if path.file_exists then
119 icon_available = true
120 break
121 end
122 end
123
124 var icon_declaration
125 if icon_available then
126 icon_declaration = "android:icon=\"@drawable/icon\""
127 else icon_declaration = ""
128
129 # Also copy over the java files
130 dir = "{android_project_root}/src/"
131 for mmodule in compiler.mainmodule.in_importation.greaters do
132 var extra_java_files = mmodule.extra_java_files
133 if extra_java_files != null then for file in extra_java_files do
134 var path = file.filename
135 path.file_copy_to(dir/path.basename(""))
136 end
137 end
138
139 ## Generate delagating makefile
140 dir = "{android_project_root}/jni/"
141 """
142 include $(call all-subdir-makefiles)
143 """.write_to_file("{dir}/Android.mk")
144
145 ### generate makefile into "{compile_dir}/Android.mk"
146 dir = compile_dir
147 """
148 LOCAL_PATH := $(call my-dir)
149 include $(CLEAR_VARS)
150
151 LOCAL_CFLAGS := -D ANDROID -D WITH_LIBGC
152 LOCAL_MODULE := main
153 LOCAL_SRC_FILES := \\
154 {{{cfiles.join(" \\\n")}}}
155 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz libgc.a
156 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
157
158 include $(BUILD_SHARED_LIBRARY)
159
160 $(call import-module,android/native_app_glue)
161 """.write_to_file("{dir}/Android.mk")
162
163 ### generate AndroidManifest.xml
164 dir = android_project_root
165 """<?xml version="1.0" encoding="utf-8"?>
166 <!-- BEGIN_INCLUDE(manifest) -->
167 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
168 package="{{{app_package}}}"
169 android:versionCode="{{{project.version_code}}}"
170 android:versionName="{{{app_version}}}">
171
172 <!-- This is the platform API where NativeActivity was introduced. -->
173 <uses-sdk
174 android:minSdkVersion="{{{app_min_api}}}"
175 android:targetSdkVersion="{{{app_target_api}}}"
176 {{{app_max_api}}} />
177
178 <application
179 android:label="@string/app_name"
180 android:hasCode="true"
181 android:debuggable="{{{not release}}}"
182 {{{icon_declaration}}}>
183
184 <!-- Our activity is the built-in NativeActivity framework class.
185 This will take care of integrating with our NDK code. -->
186 <activity android:name="android.app.NativeActivity"
187 android:label="@string/app_name"
188 {{{project.manifest_activity_attributes.join("\n")}}}
189 {{{icon_declaration}}}>
190 <!-- Tell NativeActivity the name of our .so -->
191 <meta-data android:name=\"android.app.lib_name\"
192 android:value=\"main\" />
193 <intent-filter>
194 <action android:name="android.intent.action.MAIN" />
195 <category android:name="android.intent.category.LAUNCHER" />
196 </intent-filter>
197 </activity>
198
199 {{{project.manifest_application_lines.join("\n")}}}
200
201 </application>
202
203 {{{project.manifest_lines.join("\n")}}}
204
205 </manifest>
206 <!-- END_INCLUDE(manifest) -->
207 """.write_to_file("{dir}/AndroidManifest.xml")
208
209 ### Link to png sources
210 # libpng is not available on Android NDK
211 # FIXME make obtionnal when we have alternatives to mnit
212 var nit_dir = toolcontext.nit_dir
213 var share_dir = nit_dir/"share/"
214 if not share_dir.file_exists then
215 print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
216 exit 1
217 end
218 share_dir = share_dir.realpath
219 var target_png_dir = "{android_project_root}/jni/png"
220 if not target_png_dir.file_exists then
221 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir], "Android project error")
222 end
223
224 # Ensure that android-setup-libgc.sh has been executed
225 if not "{share_dir}/libgc/lib".file_exists then
226 toolcontext.exec_and_check(["{share_dir}/libgc/android-setup-libgc.sh"], "Android project error")
227 end
228
229 # Copy GC files
230 toolcontext.exec_and_check(["cp", "{share_dir}/libgc/lib/libgc.a", "{android_project_root}/libgc.a"], "Android project error")
231 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/libgc/include/gc/", "{android_project_root}/jni/nit_compile/gc"], "Android project error")
232
233 ### Link to assets (for mnit and others)
234 # This will be accessed from `android_project_root`
235 var assets_dir
236 if compiler.mainmodule.location.file != null then
237 # it is a real file, use "{file}/../assets"
238 assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
239 else
240 # probably used -m, use "."
241 assets_dir = "assets"
242 end
243 if assets_dir.file_exists then
244 assets_dir = assets_dir.realpath
245 var target_assets_dir = "{android_project_root}/assets"
246 if not target_assets_dir.file_exists then
247 toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir], "Android project error")
248 end
249 end
250
251 ### Copy resources and libs where expected by the SDK
252 var project_root
253 if compiler.mainmodule.location.file != null then
254 # it is a real file, use "{file}/../res"
255 project_root = "{compiler.mainmodule.location.file.filename.dirname}/.."
256 else
257 # probably used -m, use "."
258 project_root = "."
259 end
260
261 # Android resources folder
262 var res_dir = project_root / "res"
263 if res_dir.file_exists then
264 # copy the res folder to .nit_compile
265 res_dir = res_dir.realpath
266 toolcontext.exec_and_check(["cp", "-R", res_dir, android_project_root], "Android project error")
267 end
268
269 if not res_dir.file_exists or not "{res_dir}/values/strings.xml".file_exists then
270 # Create our own custom `res/values/string.xml` with the App name
271 """<?xml version="1.0" encoding="utf-8"?>
272 <resources>
273 <string name="app_name">{{{app_name}}}</string>
274 </resources>""".write_to_file "{dir}/res/values/strings.xml"
275 end
276
277 # Android libs folder
278 var libs_dir = project_root / "libs"
279 if libs_dir.file_exists then
280 toolcontext.exec_and_check(["cp", "-r", libs_dir, android_project_root], "Android project error")
281 end
282 end
283
284 redef fun write_makefile(compiler, compile_dir, cfiles)
285 do
286 # Do nothing, already done in `write_files`
287 end
288
289 redef fun compile_c_code(compiler, compile_dir)
290 do
291 var android_project_root = android_project_root.as(not null)
292 var release = toolcontext.opt_release.value
293
294 # Compile C code (and thus Nit)
295 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "-C", android_project_root], "Android project error")
296
297 # Generate the apk
298 var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
299 if release then
300 args.add "release"
301 else args.add "debug"
302 toolcontext.exec_and_check(args, "Android project error")
303
304 # Move the apk to the target
305 var outname = outfile(compiler.mainmodule)
306
307 if release then
308 var apk_path = "{android_project_root}/bin/{compiler.mainmodule.name}-release-unsigned.apk"
309
310 # Sign APK
311 var keystore_path= "KEYSTORE".environ
312 var key_alias= "KEY_ALIAS".environ
313 var tsa_server= "TSA_SERVER".environ
314
315 if key_alias.is_empty then
316 toolcontext.fatal_error(null,
317 "Fatal Error: the environment variable `KEY_ALIAS` must be set to use the `--release` option on Android projects.")
318 end
319
320 args = ["jarsigner", "-sigalg", "MD5withRSA", "-digestalg", "SHA1", apk_path, key_alias]
321
322 ## Use a custom keystore
323 if not keystore_path.is_empty then args.add_all(["-keystore", keystore_path])
324
325 ## Use a TSA server
326 if not tsa_server.is_empty then args.add_all(["-tsa", tsa_server])
327
328 toolcontext.exec_and_check(args, "Android project error")
329
330 # Clean output file
331 if outname.to_path.exists then outname.to_path.delete
332
333 # Align APK
334 args = ["zipalign", "4", apk_path, outname]
335 toolcontext.exec_and_check(args, "Android project error")
336 else
337 # Move to the expected output path
338 args = ["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-debug.apk", outname]
339 toolcontext.exec_and_check(args, "Android project error")
340 end
341 end
342 end
343
344 redef class JavaClassTemplate
345 redef fun write_to_files(compdir)
346 do
347 var jni_path = "jni/nit_compile/"
348 if compdir.has_suffix(jni_path) then
349 var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
350 return super(path)
351 else return super
352 end
353 end