2 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # Compile program for the Android platform
17 module android_platform
20 import abstract_compiler
22 intrude import common_ffi
::extra_java_files
23 import android_annotations
25 redef class ToolContext
26 redef fun platform_from_name
(name
)
28 if name
== "android" then return new AndroidPlatform
36 redef fun supports_libunwind
do return false
38 redef fun toolchain
(toolcontext
) do return new AndroidToolchain(toolcontext
)
41 class AndroidToolchain
42 super MakefileToolchain
44 var android_project_root
: nullable String = null
48 var android_project_root
= "{super}/android/"
49 self.android_project_root
= android_project_root
50 return "{android_project_root}/jni/nit_compile/"
53 redef fun default_outname
(mainmodule
) do return "{mainmodule.name}.apk"
55 redef fun write_files
(compiler
, compile_dir
, cfiles
)
57 var android_project_root
= android_project_root
.as(not null)
58 var project
= toolcontext
.modelbuilder
.android_project_for
(compiler
.mainmodule
)
59 var short_project_name
= compiler
.mainmodule
.name
60 var release
= toolcontext
.opt_release
.value
62 var app_name
= project
.name
63 if app_name
== null then app_name
= compiler
.mainmodule
.name
65 var app_package
= project
.java_package
66 if app_package
== null then app_package
= "org.nitlanguage.{short_project_name}"
68 var app_version
= project
.version
69 if app_version
== null then app_version
= "1.0"
71 var app_min_api
= project
.min_api
72 if app_min_api
== null then app_min_api
= 10
74 var app_target_api
= project
.target_api
75 if app_target_api
== null then app_target_api
= app_min_api
78 if project
.max_api
!= null then app_max_api
= "android:maxSdkVersion=\"{project.max_api.as(not null)}\
""
80 # Clear the previous android project, so there is no "existing project warning"
81 # or conflict between Java files of different projects
82 if android_project_root
.file_exists
then android_project_root
.rmdir
84 var args
= ["android", "-s",
86 "--name", short_project_name
,
87 "--target", "android-{app_target_api}",
88 "--path", android_project_root
,
89 "--package", app_package
,
90 "--activity", short_project_name
]
91 toolcontext
.exec_and_check
(args
, "Android project error")
94 var dir
= "{android_project_root}/jni/"
95 if not dir
.file_exists
then dir
.mkdir
98 if not dir
.file_exists
then dir
.mkdir
100 # compile normal C files
101 super(compiler
, compile_dir
, cfiles
)
103 # Gather extra C files generated elsewhere than in super
104 for f
in compiler
.extern_bodies
do
105 if f
isa ExternCFile then cfiles
.add
(f
.filename
.basename
(""))
109 var resolutions
= ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
110 var icon_available
= false
111 for res
in resolutions
do
112 var path
= "res/drawable-{res}/icon.png"
113 if path
.file_exists
then
114 icon_available
= true
120 if icon_available
then
121 icon_declaration
= "android:icon=\"@drawable
/icon\
""
122 else icon_declaration
= ""
124 # Also copy over the java files
125 dir
= "{android_project_root}/src/"
126 var extra_java_files
= compiler
.mainmodule
.extra_java_files
127 if extra_java_files
!= null then for file
in extra_java_files
do
128 var path
= file
.filename
129 path
.file_copy_to
("{dir}/{path.basename("")}")
132 ## Generate delagating makefile
133 dir
= "{android_project_root}/jni/"
135 include $(call all-subdir-makefiles)
136 """.write_to_file
("{dir}/Android.mk")
138 ### generate makefile into "{compile_dir}/Android.mk"
141 LOCAL_PATH := $(call my-dir)
142 include $(CLEAR_VARS)
144 LOCAL_CFLAGS := -D ANDROID
146 LOCAL_SRC_FILES := \\
147 {{{cfiles.join(" \\\n")}}}
148 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
149 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
151 include $(BUILD_SHARED_LIBRARY)
153 $(call import-module,android/native_app_glue)
154 """.write_to_file
("{dir}/Android.mk")
156 ### generate AndroidManifest.xml
157 dir
= android_project_root
158 """<?xml version="1.0" encoding="utf-8"?>
159 <!-- BEGIN_INCLUDE(manifest) -->
160 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
161 package="{{{app_package}}}"
162 android:versionCode="{{{project.version_code}}}"
163 android:versionName="{{{app_version}}}">
165 <!-- This is the platform API where NativeActivity was introduced. -->
167 android:minSdkVersion="{{{app_min_api}}}"
168 android:targetSdkVersion="{{{app_target_api}}}"
172 android:label="@string/app_name"
173 android:hasCode="true"
174 android:debuggable="{{{not release}}}">
176 <!-- Our activity is the built-in NativeActivity framework class.
177 This will take care of integrating with our NDK code. -->
178 <activity android:name="android.app.NativeActivity"
179 android:label="@string/app_name"
180 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
181 android:configChanges="orientation|keyboardHidden"
182 android:screenOrientation="portrait"
183 {{{icon_declaration}}}>
184 <!-- Tell NativeActivity the name of or .so -->
185 <meta-data android:name=\"{{{app_package}}}\"
186 android:value=\"{{{app_name}}}\" />
188 <action android:name="android.intent.action.MAIN" />
189 <category android:name="android.intent.category.LAUNCHER" />
193 {{{project.manifest_application_lines.join("\n")}}}
197 {{{project.manifest_lines.join("\n")}}}
200 <!-- END_INCLUDE(manifest) -->
201 """.write_to_file
("{dir}/AndroidManifest.xml")
203 ### Link to png sources
204 # libpng is not available on Android NDK
205 # FIXME make obtionnal when we have alternatives to mnit
206 var nit_dir
= toolcontext
.nit_dir
207 var share_dir
= "{nit_dir or else ""}/share/"
208 if nit_dir
== null or not share_dir
.file_exists
then
209 print
"Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
212 share_dir
= share_dir
.realpath
213 var target_png_dir
= "{android_project_root}/jni/png"
214 if not target_png_dir
.file_exists
then
215 toolcontext
.exec_and_check
(["ln", "-s", "{share_dir}/png/", target_png_dir
], "Android project error")
218 ### Link to assets (for mnit and others)
219 # This will be accessed from `android_project_root`
221 if compiler
.mainmodule
.location
.file
!= null then
222 # it is a real file, use "{file}/../assets"
223 assets_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../assets"
225 # probably used -m, use "."
226 assets_dir
= "assets"
228 if assets_dir
.file_exists
then
229 assets_dir
= assets_dir
.realpath
230 var target_assets_dir
= "{android_project_root}/assets"
231 if not target_assets_dir
.file_exists
then
232 toolcontext
.exec_and_check
(["ln", "-s", assets_dir
, target_assets_dir
], "Android project error")
236 ### copy resources (for android)
237 # This will be accessed from `android_project_root`
239 if compiler
.mainmodule
.location
.file
!= null then
240 # it is a real file, use "{file}/../res"
241 res_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../res"
243 # probably used -m, use "."
246 if res_dir
.file_exists
then
247 # copy the res folder to .nit_compile
248 res_dir
= res_dir
.realpath
249 var target_res_dir
= "{android_project_root}"
250 toolcontext
.exec_and_check
(["cp", "-R", res_dir
, target_res_dir
], "Android project error")
253 if not res_dir
.file_exists
or not "{res_dir}/values/strings.xml".file_exists
then
254 # Create our own custom `res/values/string.xml` with the App name
255 """<?xml version="1.0" encoding="utf-8"?>
257 <string name="app_name">{{{app_name}}}</string>
258 </resources>""".write_to_file
"{dir}/res/values/strings.xml"
262 redef fun write_makefile
(compiler
, compile_dir
, cfiles
)
264 # Do nothing, already done in `write_files`
267 redef fun compile_c_code
(compiler
, compile_dir
)
269 var android_project_root
= android_project_root
.as(not null)
270 var release
= toolcontext
.opt_release
.value
272 # Compile C code (and thus Nit)
273 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "-C", android_project_root
], "Android project error")
276 var args
= ["ant", "-q", "-f", android_project_root
+"/build.xml"]
279 else args
.add
"debug"
280 toolcontext
.exec_and_check
(args
, "Android project error")
282 # Move the apk to the target
283 var outname
= outfile
(compiler
.mainmodule
)
287 src_apk_suffix
= "release-unsigned"
288 else src_apk_suffix
= "debug"
290 toolcontext
.exec_and_check
(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname
], "Android project error")
294 redef class JavaClassTemplate
295 redef fun write_to_files
(compdir
)
297 var jni_path
= "jni/nit_compile/"
298 if compdir
.has_suffix
(jni_path
) then
299 var path
= "{compdir.substring(0, compdir.length-jni_path.length)}/src/"