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