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
18 module android_platform
21 import 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
63 var release
= toolcontext
.opt_release
.value
65 var app_name
= project
.name
66 if app_name
== null then app_name
= compiler
.mainmodule
.name
68 var app_package
= project
.java_package
69 if app_package
== null then app_package
= "org.nitlanguage.{short_project_name}"
71 var app_version
= project
.version
72 if app_version
== null then app_version
= "1.0"
74 var app_min_api
= project
.min_api
75 if app_min_api
== null then app_min_api
= 10
77 var app_target_api
= project
.target_api
78 if app_target_api
== null then app_target_api
= app_min_api
81 if project
.max_api
!= null then app_max_api
= "android:maxSdkVersion=\"{project.max_api.as(not null)}\
""
83 # Clear the previous android project, so there is no "existing project warning"
84 # or conflict between Java files of different projects
85 if android_project_root
.file_exists
then android_project_root
.rmdir
87 var args
= ["android", "-s",
89 "--name", short_project_name
,
90 "--target", "android-{app_target_api}",
91 "--path", android_project_root
,
92 "--package", app_package
,
93 "--activity", short_project_name
]
94 toolcontext
.exec_and_check
(args
, "Android project error")
97 var dir
= "{android_project_root}/jni/"
98 if not dir
.file_exists
then dir
.mkdir
101 if not dir
.file_exists
then dir
.mkdir
103 # compile normal C files
104 super(compiler
, compile_dir
, cfiles
)
106 # Gather extra C files generated elsewhere than in super
107 for f
in compiler
.extern_bodies
do
108 if f
isa ExternCFile then cfiles
.add
(f
.filename
.basename
(""))
112 var resolutions
= ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
113 var icon_available
= false
114 for res
in resolutions
do
115 var path
= "res/drawable-{res}/icon.png"
116 if path
.file_exists
then
117 icon_available
= true
123 if icon_available
then
124 icon_declaration
= "android:icon=\"@drawable
/icon\
""
125 else icon_declaration
= ""
127 # Also copy over the java files
128 dir
= "{android_project_root}/src/"
129 for mmodule
in compiler
.mainmodule
.in_importation
.greaters
do
130 var extra_java_files
= mmodule
.extra_java_files
131 if extra_java_files
!= null then for file
in extra_java_files
do
132 var path
= file
.filename
133 path
.file_copy_to
(dir
/path
.basename
(""))
137 ## Generate delagating makefile
138 dir
= "{android_project_root}/jni/"
140 include $(call all-subdir-makefiles)
141 """.write_to_file
("{dir}/Android.mk")
143 ### generate makefile into "{compile_dir}/Android.mk"
146 LOCAL_PATH := $(call my-dir)
147 include $(CLEAR_VARS)
149 LOCAL_CFLAGS := -D ANDROID
151 LOCAL_SRC_FILES := \\
152 {{{cfiles.join(" \\\n")}}}
153 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
154 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
156 include $(BUILD_SHARED_LIBRARY)
158 $(call import-module,android/native_app_glue)
159 """.write_to_file
("{dir}/Android.mk")
161 ### generate AndroidManifest.xml
162 dir
= android_project_root
163 """<?xml version="1.0" encoding="utf-8"?>
164 <!-- BEGIN_INCLUDE(manifest) -->
165 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
166 package="{{{app_package}}}"
167 android:versionCode="{{{project.version_code}}}"
168 android:versionName="{{{app_version}}}">
170 <!-- This is the platform API where NativeActivity was introduced. -->
172 android:minSdkVersion="{{{app_min_api}}}"
173 android:targetSdkVersion="{{{app_target_api}}}"
177 android:label="@string/app_name"
178 android:hasCode="true"
179 android:debuggable="{{{not release}}}">
181 <!-- Our activity is the built-in NativeActivity framework class.
182 This will take care of integrating with our NDK code. -->
183 <activity android:name="android.app.NativeActivity"
184 android:label="@string/app_name"
185 {{{project.manifest_activity_attributes.join("\n")}}}
186 {{{icon_declaration}}}>
187 <!-- Tell NativeActivity the name of our .so -->
188 <meta-data android:name=\"android.app.lib_name\"
189 android:value=\"main\" />
191 <action android:name="android.intent.action.MAIN" />
192 <category android:name="android.intent.category.LAUNCHER" />
196 {{{project.manifest_application_lines.join("\n")}}}
200 {{{project.manifest_lines.join("\n")}}}
203 <!-- END_INCLUDE(manifest) -->
204 """.write_to_file
("{dir}/AndroidManifest.xml")
206 ### Link to png sources
207 # libpng is not available on Android NDK
208 # FIXME make obtionnal when we have alternatives to mnit
209 var nit_dir
= toolcontext
.nit_dir
210 var share_dir
= nit_dir
/"share/"
211 if not share_dir
.file_exists
then
212 print
"Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
215 share_dir
= share_dir
.realpath
216 var target_png_dir
= "{android_project_root}/jni/png"
217 if not target_png_dir
.file_exists
then
218 toolcontext
.exec_and_check
(["ln", "-s", "{share_dir}/png/", target_png_dir
], "Android project error")
221 ### Link to assets (for mnit and others)
222 # This will be accessed from `android_project_root`
224 if compiler
.mainmodule
.location
.file
!= null then
225 # it is a real file, use "{file}/../assets"
226 assets_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../assets"
228 # probably used -m, use "."
229 assets_dir
= "assets"
231 if assets_dir
.file_exists
then
232 assets_dir
= assets_dir
.realpath
233 var target_assets_dir
= "{android_project_root}/assets"
234 if not target_assets_dir
.file_exists
then
235 toolcontext
.exec_and_check
(["ln", "-s", assets_dir
, target_assets_dir
], "Android project error")
239 ### Copy resources and libs where expected by the SDK
241 if compiler
.mainmodule
.location
.file
!= null then
242 # it is a real file, use "{file}/../res"
243 project_root
= "{compiler.mainmodule.location.file.filename.dirname}/.."
245 # probably used -m, use "."
249 # Android resources folder
250 var res_dir
= project_root
/ "res"
251 if res_dir
.file_exists
then
252 # copy the res folder to .nit_compile
253 res_dir
= res_dir
.realpath
254 toolcontext
.exec_and_check
(["cp", "-R", res_dir
, android_project_root
], "Android project error")
257 if not res_dir
.file_exists
or not "{res_dir}/values/strings.xml".file_exists
then
258 # Create our own custom `res/values/string.xml` with the App name
259 """<?xml version="1.0" encoding="utf-8"?>
261 <string name="app_name">{{{app_name}}}</string>
262 </resources>""".write_to_file
"{dir}/res/values/strings.xml"
265 # Android libs folder
266 var libs_dir
= project_root
/ "libs"
267 if libs_dir
.file_exists
then
268 toolcontext
.exec_and_check
(["cp", "-r", libs_dir
, android_project_root
], "Android project error")
272 redef fun write_makefile
(compiler
, compile_dir
, cfiles
)
274 # Do nothing, already done in `write_files`
277 redef fun compile_c_code
(compiler
, compile_dir
)
279 var android_project_root
= android_project_root
.as(not null)
280 var release
= toolcontext
.opt_release
.value
282 # Compile C code (and thus Nit)
283 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "-C", android_project_root
], "Android project error")
286 var args
= ["ant", "-q", "-f", android_project_root
+"/build.xml"]
289 else args
.add
"debug"
290 toolcontext
.exec_and_check
(args
, "Android project error")
292 # Move the apk to the target
293 var outname
= outfile
(compiler
.mainmodule
)
297 src_apk_suffix
= "release-unsigned"
298 else src_apk_suffix
= "debug"
300 toolcontext
.exec_and_check
(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname
], "Android project error")
304 redef class JavaClassTemplate
305 redef fun write_to_files
(compdir
)
307 var jni_path
= "jni/nit_compile/"
308 if compdir
.has_suffix
(jni_path
) then
309 var path
= "{compdir.substring(0, compdir.length-jni_path.length)}/src/"