nitc/android: set the icon for the APK as expected by the Play Store
[nit.git] / src / compiler / android_platform.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_platform
19
20 import platform
21 import 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_libunwind do return false
38
39 redef fun toolchain(toolcontext) do return new AndroidToolchain(toolcontext)
40 end
41
42 class AndroidToolchain
43 super MakefileToolchain
44
45 var android_project_root: nullable String = null
46
47 redef fun compile_dir
48 do
49 var android_project_root = "{super}/android/"
50 self.android_project_root = android_project_root
51 return "{android_project_root}/jni/nit_compile/"
52 end
53
54 redef fun default_outname(mainmodule) do return "{mainmodule.name}.apk"
55
56 redef fun write_files(compiler, compile_dir, cfiles)
57 do
58 var android_project_root = android_project_root.as(not null)
59 var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
60 var short_project_name = compiler.mainmodule.name
61 var release = toolcontext.opt_release.value
62
63 var app_name = project.name
64 if app_name == null then app_name = compiler.mainmodule.name
65
66 var app_package = project.java_package
67 if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
68
69 var app_version = project.version
70 if app_version == null then app_version = "1.0"
71
72 var app_min_api = project.min_api
73 if app_min_api == null then app_min_api = 10
74
75 var app_target_api = project.target_api
76 if app_target_api == null then app_target_api = app_min_api
77
78 var app_max_api = ""
79 if project.max_api != null then app_max_api = "android:maxSdkVersion=\"{project.max_api.as(not null)}\""
80
81 # Clear the previous android project, so there is no "existing project warning"
82 # or conflict between Java files of different projects
83 if android_project_root.file_exists then android_project_root.rmdir
84
85 var args = ["android", "-s",
86 "create", "project",
87 "--name", short_project_name,
88 "--target", "android-{app_target_api}",
89 "--path", android_project_root,
90 "--package", app_package,
91 "--activity", short_project_name]
92 toolcontext.exec_and_check(args, "Android project error")
93
94 # create compile_dir
95 var dir = "{android_project_root}/jni/"
96 if not dir.file_exists then dir.mkdir
97
98 dir = compile_dir
99 if not dir.file_exists then dir.mkdir
100
101 # compile normal C files
102 super(compiler, compile_dir, cfiles)
103
104 # Gather extra C files generated elsewhere than in super
105 for f in compiler.extern_bodies do
106 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
107 end
108
109 # Is there an icon?
110 var resolutions = ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
111 var icon_available = false
112 for res in resolutions do
113 var path = "res/drawable-{res}/icon.png"
114 if path.file_exists then
115 icon_available = true
116 break
117 end
118 end
119
120 var icon_declaration
121 if icon_available then
122 icon_declaration = "android:icon=\"@drawable/icon\""
123 else icon_declaration = ""
124
125 # Also copy over the java files
126 dir = "{android_project_root}/src/"
127 for mmodule in compiler.mainmodule.in_importation.greaters do
128 var extra_java_files = mmodule.extra_java_files
129 if extra_java_files != null then for file in extra_java_files do
130 var path = file.filename
131 path.file_copy_to(dir/path.basename(""))
132 end
133 end
134
135 ## Generate delagating makefile
136 dir = "{android_project_root}/jni/"
137 """
138 include $(call all-subdir-makefiles)
139 """.write_to_file("{dir}/Android.mk")
140
141 ### generate makefile into "{compile_dir}/Android.mk"
142 dir = compile_dir
143 """
144 LOCAL_PATH := $(call my-dir)
145 include $(CLEAR_VARS)
146
147 LOCAL_CFLAGS := -D ANDROID
148 LOCAL_MODULE := main
149 LOCAL_SRC_FILES := \\
150 {{{cfiles.join(" \\\n")}}}
151 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
152 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
153
154 include $(BUILD_SHARED_LIBRARY)
155
156 $(call import-module,android/native_app_glue)
157 """.write_to_file("{dir}/Android.mk")
158
159 ### generate AndroidManifest.xml
160 dir = android_project_root
161 """<?xml version="1.0" encoding="utf-8"?>
162 <!-- BEGIN_INCLUDE(manifest) -->
163 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
164 package="{{{app_package}}}"
165 android:versionCode="{{{project.version_code}}}"
166 android:versionName="{{{app_version}}}">
167
168 <!-- This is the platform API where NativeActivity was introduced. -->
169 <uses-sdk
170 android:minSdkVersion="{{{app_min_api}}}"
171 android:targetSdkVersion="{{{app_target_api}}}"
172 {{{app_max_api}}} />
173
174 <application
175 android:label="@string/app_name"
176 android:hasCode="true"
177 android:debuggable="{{{not release}}}"
178 {{{icon_declaration}}}>
179
180 <!-- Our activity is the built-in NativeActivity framework class.
181 This will take care of integrating with our NDK code. -->
182 <activity android:name="android.app.NativeActivity"
183 android:label="@string/app_name"
184 {{{project.manifest_activity_attributes.join("\n")}}}
185 {{{icon_declaration}}}>
186 <!-- Tell NativeActivity the name of our .so -->
187 <meta-data android:name=\"android.app.lib_name\"
188 android:value=\"main\" />
189 <intent-filter>
190 <action android:name="android.intent.action.MAIN" />
191 <category android:name="android.intent.category.LAUNCHER" />
192 </intent-filter>
193 </activity>
194
195 {{{project.manifest_application_lines.join("\n")}}}
196
197 </application>
198
199 {{{project.manifest_lines.join("\n")}}}
200
201 </manifest>
202 <!-- END_INCLUDE(manifest) -->
203 """.write_to_file("{dir}/AndroidManifest.xml")
204
205 ### Link to png sources
206 # libpng is not available on Android NDK
207 # FIXME make obtionnal when we have alternatives to mnit
208 var nit_dir = toolcontext.nit_dir
209 var share_dir = nit_dir/"share/"
210 if not share_dir.file_exists then
211 print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
212 exit 1
213 end
214 share_dir = share_dir.realpath
215 var target_png_dir = "{android_project_root}/jni/png"
216 if not target_png_dir.file_exists then
217 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir], "Android project error")
218 end
219
220 ### Link to assets (for mnit and others)
221 # This will be accessed from `android_project_root`
222 var assets_dir
223 if compiler.mainmodule.location.file != null then
224 # it is a real file, use "{file}/../assets"
225 assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
226 else
227 # probably used -m, use "."
228 assets_dir = "assets"
229 end
230 if assets_dir.file_exists then
231 assets_dir = assets_dir.realpath
232 var target_assets_dir = "{android_project_root}/assets"
233 if not target_assets_dir.file_exists then
234 toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir], "Android project error")
235 end
236 end
237
238 ### Copy resources and libs where expected by the SDK
239 var project_root
240 if compiler.mainmodule.location.file != null then
241 # it is a real file, use "{file}/../res"
242 project_root = "{compiler.mainmodule.location.file.filename.dirname}/.."
243 else
244 # probably used -m, use "."
245 project_root = "."
246 end
247
248 # Android resources folder
249 var res_dir = project_root / "res"
250 if res_dir.file_exists then
251 # copy the res folder to .nit_compile
252 res_dir = res_dir.realpath
253 toolcontext.exec_and_check(["cp", "-R", res_dir, android_project_root], "Android project error")
254 end
255
256 if not res_dir.file_exists or not "{res_dir}/values/strings.xml".file_exists then
257 # Create our own custom `res/values/string.xml` with the App name
258 """<?xml version="1.0" encoding="utf-8"?>
259 <resources>
260 <string name="app_name">{{{app_name}}}</string>
261 </resources>""".write_to_file "{dir}/res/values/strings.xml"
262 end
263
264 # Android libs folder
265 var libs_dir = project_root / "libs"
266 if libs_dir.file_exists then
267 toolcontext.exec_and_check(["cp", "-r", libs_dir, android_project_root], "Android project error")
268 end
269 end
270
271 redef fun write_makefile(compiler, compile_dir, cfiles)
272 do
273 # Do nothing, already done in `write_files`
274 end
275
276 redef fun compile_c_code(compiler, compile_dir)
277 do
278 var android_project_root = android_project_root.as(not null)
279 var release = toolcontext.opt_release.value
280
281 # Compile C code (and thus Nit)
282 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "-C", android_project_root], "Android project error")
283
284 # Generate the apk
285 var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
286 if release then
287 args.add "release"
288 else args.add "debug"
289 toolcontext.exec_and_check(args, "Android project error")
290
291 # Move the apk to the target
292 var outname = outfile(compiler.mainmodule)
293
294 var src_apk_suffix
295 if release then
296 src_apk_suffix = "release-unsigned"
297 else src_apk_suffix = "debug"
298
299 toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname], "Android project error")
300 end
301 end
302
303 redef class JavaClassTemplate
304 redef fun write_to_files(compdir)
305 do
306 var jni_path = "jni/nit_compile/"
307 if compdir.has_suffix(jni_path) then
308 var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
309 return super(path)
310 else return super
311 end
312 end