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 name
do return "android"
39 redef fun supports_libgc
do return true
41 redef fun supports_libunwind
do return false
43 redef fun supports_linker_script
do return false
45 redef fun toolchain
(toolcontext
, compiler
) do return new AndroidToolchain(toolcontext
, compiler
)
48 class AndroidToolchain
49 super MakefileToolchain
51 var android_project_root
: nullable String = null
55 var android_project_root
= "{root_compile_dir}/android/"
56 self.android_project_root
= android_project_root
57 return "{android_project_root}/jni/nit_compile/"
60 redef fun default_outname
do return "{super}.apk"
62 redef fun write_files
(compile_dir
, cfiles
)
64 var android_project_root
= android_project_root
.as(not null)
65 var project
= new AndroidProject(toolcontext
.modelbuilder
, compiler
.mainmodule
)
66 var release
= toolcontext
.opt_release
.value
68 var app_name
= project
.name
69 if not release
then app_name
+= " Debug"
71 var short_project_name
= project
.short_name
73 var app_package
= project
.namespace
74 if not release
then app_package
+= "_debug"
76 var app_version
= project
.version
78 var app_min_api
= project
.min_api
79 if app_min_api
== null then app_min_api
= 10
81 var app_target_api
= project
.target_api
82 if app_target_api
== null then app_target_api
= app_min_api
85 if project
.max_api
!= null then app_max_api
= "android:maxSdkVersion=\"{project.max_api.as(not null)}\
""
87 # Clear the previous android project, so there is no "existing project warning"
88 # or conflict between Java files of different projects
89 if android_project_root
.file_exists
then android_project_root
.rmdir
91 var args
= ["android", "-s",
93 "--name", short_project_name
,
94 "--target", "android-{app_target_api}",
95 "--path", android_project_root
,
96 "--package", app_package
,
97 "--activity", short_project_name
]
98 toolcontext
.exec_and_check
(args
, "Android project error")
101 var dir
= "{android_project_root}/jni/"
102 if not dir
.file_exists
then dir
.mkdir
105 if not dir
.file_exists
then dir
.mkdir
107 # compile normal C files
110 # Gather extra C files generated elsewhere than in super
111 for f
in compiler
.extern_bodies
do
112 if f
isa ExternCFile then cfiles
.add
(f
.filename
.basename
)
116 var resolutions
= ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
117 var icon_available
= false
118 for res
in resolutions
do
119 var path
= "res/drawable-{res}/icon.png"
120 if path
.file_exists
then
121 icon_available
= true
127 if icon_available
then
128 icon_declaration
= "android:icon=\"@drawable
/icon\
""
129 else icon_declaration
= ""
131 # Also copy over the java files
132 dir
= "{android_project_root}/src/"
133 for mmodule
in compiler
.mainmodule
.in_importation
.greaters
do
134 var extra_java_files
= mmodule
.extra_java_files
135 if extra_java_files
!= null then for file
in extra_java_files
do
136 var path
= file
.filename
137 path
.file_copy_to
(dir
/path
.basename
)
141 ## Generate Application.mk
142 dir
= "{android_project_root}/jni/"
144 APP_ABI := armeabi armeabi-v7a x86 mips
145 APP_PLATFORM := android-{{{app_target_api}}}
146 """.write_to_file
"{dir}/Application.mk"
148 ## Generate delegating makefile
150 include $(call all-subdir-makefiles)
151 """.write_to_file
"{dir}/Android.mk"
153 # Gather ldflags for Android
154 var ldflags
= new Array[String]
155 var platform_name
= "android"
156 for mmodule
in compiler
.mainmodule
.in_importation
.greaters
do
157 if mmodule
.ldflags
.keys
.has
(platform_name
) then
158 ldflags
.add_all mmodule
.ldflags
[platform_name
]
162 ### generate makefile into "{compile_dir}/Android.mk"
165 LOCAL_PATH := $(call my-dir)
166 include $(CLEAR_VARS)
168 LOCAL_CFLAGS := -D ANDROID -D WITH_LIBGC
170 LOCAL_SRC_FILES := \\
171 {{{cfiles.join(" \\\n")}}}
172 LOCAL_LDLIBS := {{{ldflags.join(" ")}}} $(TARGET_ARCH)/libgc.a
173 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
175 include $(BUILD_SHARED_LIBRARY)
177 $(call import-module,android/native_app_glue)
178 """.write_to_file
("{dir}/Android.mk")
180 ### generate AndroidManifest.xml
181 dir
= android_project_root
182 var manifest_file
= new FileWriter.open
("{dir}/AndroidManifest.xml")
183 manifest_file
.write
"""
184 <?xml version="1.0" encoding="utf-8"?>
185 <!-- BEGIN_INCLUDE(manifest) -->
186 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
187 package="{{{app_package}}}"
188 android:versionCode="{{{project.version_code}}}"
189 android:versionName="{{{app_version}}}">
192 android:minSdkVersion="{{{app_min_api}}}"
193 android:targetSdkVersion="{{{app_target_api}}}"
197 android:label="@string/app_name"
198 android:hasCode="true"
199 android:debuggable="{{{not release}}}"
200 {{{icon_declaration}}}>
203 for activity
in project
.activities
do
204 manifest_file
.write
"""
205 <activity android:name="{{{activity}}}"
206 android:label="@string/app_name"
207 {{{project.manifest_activity_attributes.join("\n")}}}
208 {{{icon_declaration}}}>
210 <action android:name="android.intent.action.MAIN" />
211 <category android:name="android.intent.category.LAUNCHER" />
217 manifest_file
.write
"""
218 {{{project.manifest_application_lines.join("\n")}}}
222 {{{project.manifest_lines.join("\n")}}}
225 <!-- END_INCLUDE(manifest) -->
229 ### Link to png sources
230 # libpng is not available on Android NDK
231 # FIXME make obtionnal when we have alternatives to mnit
232 var nit_dir
= toolcontext
.nit_dir
233 var share_dir
= nit_dir
/"share/"
234 if not share_dir
.file_exists
then
235 print
"Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
238 share_dir
= share_dir
.realpath
239 var target_png_dir
= "{android_project_root}/jni/png"
240 if not target_png_dir
.file_exists
then
241 toolcontext
.exec_and_check
(["ln", "-s", "{share_dir}/png/", target_png_dir
], "Android project error")
244 # Ensure that android-setup-libgc.sh has been executed
245 if not "{share_dir}/libgc/arm/lib".file_exists
then
246 toolcontext
.exec_and_check
(["{share_dir}/libgc/android-setup-libgc.sh"], "Android project error")
250 for arch
in ["arm", "x86", "mips"] do
251 dir
= android_project_root
/arch
253 toolcontext
.exec_and_check
(["cp", "{share_dir}/libgc/{arch}/lib/libgc.a",
254 dir
/"libgc.a"], "Android project error")
257 toolcontext
.exec_and_check
(["ln", "-s", "{share_dir}/libgc/arm/include/gc/",
258 "{compile_dir}/gc"], "Android project error")
260 # Copy assets, resources and libs where expected by the SDK
262 var project_root
= "."
263 var mpackage
= compiler
.mainmodule
.first_real_mmodule
.mpackage
264 if mpackage
!= null then
265 var root
= mpackage
.root
267 var filepath
= root
.filepath
268 if filepath
!= null then
269 project_root
= filepath
274 # Link to assets (for mnit and others)
275 var assets_dir
= project_root
/ "assets"
276 if assets_dir
.file_exists
then
277 assets_dir
= assets_dir
.realpath
278 var target_assets_dir
= "{android_project_root}/assets"
279 if not target_assets_dir
.file_exists
then
280 toolcontext
.exec_and_check
(["ln", "-s", assets_dir
, target_assets_dir
], "Android project error")
284 # Copy the res folder
285 var res_dir
= project_root
/ "res"
286 if res_dir
.file_exists
then
287 # copy the res folder to the compile dir
288 res_dir
= res_dir
.realpath
289 toolcontext
.exec_and_check
(["cp", "-R", res_dir
, android_project_root
], "Android project error")
292 if not res_dir
.file_exists
or not "{res_dir}/values/strings.xml".file_exists
then
293 # Create our own custom `res/values/string.xml` with the App name
294 """<?xml version="1.0" encoding="utf-8"?>
296 <string name="app_name">{{{app_name}}}</string>
297 </resources>""".write_to_file
"{android_project_root}/res/values/strings.xml"
300 # Copy the libs folder
301 var libs_dir
= project_root
/ "libs"
302 if libs_dir
.file_exists
then
303 toolcontext
.exec_and_check
(["cp", "-r", libs_dir
, android_project_root
], "Android project error")
307 redef fun write_makefile
(compile_dir
, cfiles
)
309 # Do nothing, already done in `write_files`
312 redef fun compile_c_code
(compile_dir
)
314 var android_project_root
= android_project_root
.as(not null)
315 var short_project_name
= compiler
.mainmodule
.name
.replace
("-", "_")
316 var release
= toolcontext
.opt_release
.value
318 # Compile C code (and thus Nit)
319 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "-C", android_project_root
], "Android project error")
322 var args
= ["ant", "-q", "-f", android_project_root
+"/build.xml"]
325 else args
.add
"debug"
326 toolcontext
.exec_and_check
(args
, "Android project error")
328 # Move the apk to the target
329 var outname
= outfile
(compiler
.mainmodule
)
332 var apk_path
= "{android_project_root}/bin/{short_project_name}-release-unsigned.apk"
335 var keystore_path
= "KEYSTORE".environ
336 var key_alias
= "KEY_ALIAS".environ
337 var tsa_server
= "TSA_SERVER".environ
339 if key_alias
.is_empty
then
340 toolcontext
.error
(null,
341 "Error: the environment variable `KEY_ALIAS` must be set to use the `--release` option on Android projects.")
345 args
= ["jarsigner", "-sigalg", "MD5withRSA", "-digestalg", "SHA1", apk_path
, key_alias
]
347 ## Use a custom keystore
348 if not keystore_path
.is_empty
then args
.add_all
(["-keystore", keystore_path
])
351 if not tsa_server
.is_empty
then args
.add_all
(["-tsa", tsa_server
])
353 toolcontext
.exec_and_check
(args
, "Android project error")
356 if outname
.to_path
.exists
then outname
.to_path
.delete
359 args
= ["zipalign", "4", apk_path
, outname
]
360 toolcontext
.exec_and_check
(args
, "Android project error")
362 # Move to the expected output path
363 args
= ["mv", "{android_project_root}/bin/{short_project_name}-debug.apk", outname
]
364 toolcontext
.exec_and_check
(args
, "Android project error")
369 redef class JavaClassTemplate
370 redef fun write_to_files
(compdir
)
372 var jni_path
= "jni/nit_compile/"
373 if compdir
.has_suffix
(jni_path
) then
374 var path
= "{compdir.substring(0, compdir.length-jni_path.length)}/src/"