1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Compile program for the Android platform
21 import compiler
::abstract_compiler
23 intrude import ffi
::extra_java_files
24 import android_annotations
26 redef class ToolContext
27 redef fun platform_from_name
(name
)
29 if name
== "android" then return new AndroidPlatform
37 redef fun supports_libunwind
do return false
39 redef fun supports_linker_script
do return false
41 redef fun toolchain
(toolcontext
) do return new AndroidToolchain(toolcontext
)
44 class AndroidToolchain
45 super MakefileToolchain
47 var android_project_root
: nullable String = null
51 var android_project_root
= "{super}/android/"
52 self.android_project_root
= android_project_root
53 return "{android_project_root}/jni/nit_compile/"
56 redef fun default_outname
(mainmodule
) do return "{mainmodule.name}.apk"
58 redef fun write_files
(compiler
, compile_dir
, cfiles
)
60 var android_project_root
= android_project_root
.as(not null)
61 var project
= toolcontext
.modelbuilder
.android_project_for
(compiler
.mainmodule
)
62 var short_project_name
= compiler
.mainmodule
.name
.replace
("-", "_")
63 var release
= toolcontext
.opt_release
.value
65 var app_name
= project
.name
66 if app_name
== null then app_name
= compiler
.mainmodule
.name
67 if not release
then app_name
+= " Debug"
69 var app_package
= project
.java_package
70 if app_package
== null then app_package
= "org.nitlanguage.{short_project_name}"
71 if not release
then app_package
+= "_debug"
73 var app_version
= project
.version
74 if app_version
== null then app_version
= "1.0"
76 var app_min_api
= project
.min_api
77 if app_min_api
== null then app_min_api
= 10
79 var app_target_api
= project
.target_api
80 if app_target_api
== null then app_target_api
= app_min_api
83 if project
.max_api
!= null then app_max_api
= "android:maxSdkVersion=\"{project.max_api.as(not null)}\
""
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
89 var args
= ["android", "-s",
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")
99 var dir
= "{android_project_root}/jni/"
100 if not dir
.file_exists
then dir
.mkdir
103 if not dir
.file_exists
then dir
.mkdir
105 # compile normal C files
106 super(compiler
, compile_dir
, cfiles
)
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
(""))
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
125 if icon_available
then
126 icon_declaration
= "android:icon=\"@drawable
/icon\
""
127 else icon_declaration
= ""
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
(""))
139 ## Generate delagating makefile
140 dir
= "{android_project_root}/jni/"
142 include $(call all-subdir-makefiles)
143 """.write_to_file
("{dir}/Android.mk")
145 ### generate makefile into "{compile_dir}/Android.mk"
148 LOCAL_PATH := $(call my-dir)
149 include $(CLEAR_VARS)
151 LOCAL_CFLAGS := -D ANDROID
153 LOCAL_SRC_FILES := \\
154 {{{cfiles.join(" \\\n")}}}
155 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
156 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
158 include $(BUILD_SHARED_LIBRARY)
160 $(call import-module,android/native_app_glue)
161 """.write_to_file
("{dir}/Android.mk")
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}}}">
172 <!-- This is the platform API where NativeActivity was introduced. -->
174 android:minSdkVersion="{{{app_min_api}}}"
175 android:targetSdkVersion="{{{app_target_api}}}"
179 android:label="@string/app_name"
180 android:hasCode="true"
181 android:debuggable="{{{not release}}}"
182 {{{icon_declaration}}}
183 android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation">
185 <!-- Our activity is the built-in NativeActivity framework class.
186 This will take care of integrating with our NDK code. -->
187 <activity android:name="android.app.NativeActivity"
188 android:label="@string/app_name"
189 {{{project.manifest_activity_attributes.join("\n")}}}
190 {{{icon_declaration}}}>
191 <!-- Tell NativeActivity the name of our .so -->
192 <meta-data android:name=\"android.app.lib_name\"
193 android:value=\"main\" />
195 <action android:name="android.intent.action.MAIN" />
196 <category android:name="android.intent.category.LAUNCHER" />
200 {{{project.manifest_application_lines.join("\n")}}}
204 {{{project.manifest_lines.join("\n")}}}
207 <!-- END_INCLUDE(manifest) -->
208 """.write_to_file
("{dir}/AndroidManifest.xml")
210 ### Link to png sources
211 # libpng is not available on Android NDK
212 # FIXME make obtionnal when we have alternatives to mnit
213 var nit_dir
= toolcontext
.nit_dir
214 var share_dir
= nit_dir
/"share/"
215 if not share_dir
.file_exists
then
216 print
"Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
219 share_dir
= share_dir
.realpath
220 var target_png_dir
= "{android_project_root}/jni/png"
221 if not target_png_dir
.file_exists
then
222 toolcontext
.exec_and_check
(["ln", "-s", "{share_dir}/png/", target_png_dir
], "Android project error")
225 ### Link to assets (for mnit and others)
226 # This will be accessed from `android_project_root`
228 if compiler
.mainmodule
.location
.file
!= null then
229 # it is a real file, use "{file}/../assets"
230 assets_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../assets"
232 # probably used -m, use "."
233 assets_dir
= "assets"
235 if assets_dir
.file_exists
then
236 assets_dir
= assets_dir
.realpath
237 var target_assets_dir
= "{android_project_root}/assets"
238 if not target_assets_dir
.file_exists
then
239 toolcontext
.exec_and_check
(["ln", "-s", assets_dir
, target_assets_dir
], "Android project error")
243 ### Copy resources and libs where expected by the SDK
245 if compiler
.mainmodule
.location
.file
!= null then
246 # it is a real file, use "{file}/../res"
247 project_root
= "{compiler.mainmodule.location.file.filename.dirname}/.."
249 # probably used -m, use "."
253 # Android resources folder
254 var res_dir
= project_root
/ "res"
255 if res_dir
.file_exists
then
256 # copy the res folder to .nit_compile
257 res_dir
= res_dir
.realpath
258 toolcontext
.exec_and_check
(["cp", "-R", res_dir
, android_project_root
], "Android project error")
261 if not res_dir
.file_exists
or not "{res_dir}/values/strings.xml".file_exists
then
262 # Create our own custom `res/values/string.xml` with the App name
263 """<?xml version="1.0" encoding="utf-8"?>
265 <string name="app_name">{{{app_name}}}</string>
266 </resources>""".write_to_file
"{dir}/res/values/strings.xml"
269 # Android libs folder
270 var libs_dir
= project_root
/ "libs"
271 if libs_dir
.file_exists
then
272 toolcontext
.exec_and_check
(["cp", "-r", libs_dir
, android_project_root
], "Android project error")
276 redef fun write_makefile
(compiler
, compile_dir
, cfiles
)
278 # Do nothing, already done in `write_files`
281 redef fun compile_c_code
(compiler
, compile_dir
)
283 var android_project_root
= android_project_root
.as(not null)
284 var short_project_name
= compiler
.mainmodule
.name
.replace
("-", "_")
285 var release
= toolcontext
.opt_release
.value
287 # Compile C code (and thus Nit)
288 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "-C", android_project_root
], "Android project error")
291 var args
= ["ant", "-q", "-f", android_project_root
+"/build.xml"]
294 else args
.add
"debug"
295 toolcontext
.exec_and_check
(args
, "Android project error")
297 # Move the apk to the target
298 var outname
= outfile
(compiler
.mainmodule
)
301 var apk_path
= "{android_project_root}/bin/{short_project_name}-release-unsigned.apk"
304 var keystore_path
= "KEYSTORE".environ
305 var key_alias
= "KEY_ALIAS".environ
306 var tsa_server
= "TSA_SERVER".environ
308 if key_alias
.is_empty
then
309 toolcontext
.fatal_error
(null,
310 "Fatal Error: the environment variable `KEY_ALIAS` must be set to use the `--release` option on Android projects.")
313 args
= ["jarsigner", "-sigalg", "MD5withRSA", "-digestalg", "SHA1", apk_path
, key_alias
]
315 ## Use a custom keystore
316 if not keystore_path
.is_empty
then args
.add_all
(["-keystore", keystore_path
])
319 if not tsa_server
.is_empty
then args
.add_all
(["-tsa", tsa_server
])
321 toolcontext
.exec_and_check
(args
, "Android project error")
324 if outname
.to_path
.exists
then outname
.to_path
.delete
327 args
= ["zipalign", "4", apk_path
, outname
]
328 toolcontext
.exec_and_check
(args
, "Android project error")
330 # Move to the expected output path
331 args
= ["mv", "{android_project_root}/bin/{short_project_name}-debug.apk", outname
]
332 toolcontext
.exec_and_check
(args
, "Android project error")
337 redef class JavaClassTemplate
338 redef fun write_to_files
(compdir
)
340 var jni_path
= "jni/nit_compile/"
341 if compdir
.has_suffix
(jni_path
) then
342 var path
= "{compdir.substring(0, compdir.length-jni_path.length)}/src/"