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 import android_annotations
24 redef class ToolContext
25 redef fun platform_from_name
(name
)
27 if name
== "android" then return new AndroidPlatform
35 redef fun supports_libunwind
do return false
37 redef fun toolchain
(toolcontext
) do return new AndroidToolchain(toolcontext
)
40 class AndroidToolchain
41 super MakefileToolchain
43 var android_project_root
: nullable String = null
47 var android_project_root
= "{super}/android/"
48 self.android_project_root
= android_project_root
49 return "{android_project_root}/jni/nit_compile/"
52 redef fun write_files
(compiler
, compile_dir
, cfiles
)
54 var android_project_root
= android_project_root
.as(not null)
55 var project
= toolcontext
.modelbuilder
.android_project_for
(compiler
.mainmodule
)
56 var short_project_name
= compiler
.mainmodule
.name
57 var release
= toolcontext
.opt_release
.value
59 var app_name
= project
.name
60 if app_name
== null then app_name
= compiler
.mainmodule
.name
62 var app_package
= project
.java_package
63 if app_package
== null then app_package
= "org.nitlanguage.{short_project_name}"
65 var app_version
= project
.version
66 if app_version
== null then app_version
= "1.0"
68 var app_min_sdk
= project
.min_sdk
69 if app_min_sdk
== null then app_min_sdk
= 10
71 var app_target_sdk
= project
.target_sdk
72 if app_target_sdk
== null then app_target_sdk
= app_min_sdk
75 if project
.max_sdk
!= null then app_max_sdk
= "android:maxSdkVersion=\"{app_max_sdk}\
""
77 # Clear the previous android project, so there is no "existing project warning"
78 # or conflict between Java files of different projects
79 if android_project_root
.file_exists
then android_project_root
.rmdir
81 var args
= ["android", "-s",
83 "--name", short_project_name
,
84 "--target", "android-{app_target_sdk}",
85 "--path", android_project_root
,
86 "--package", app_package
,
87 "--activity", short_project_name
]
88 toolcontext
.exec_and_check
(args
, "Android project error")
91 var dir
= "{android_project_root}/jni/"
92 if not dir
.file_exists
then dir
.mkdir
95 if not dir
.file_exists
then dir
.mkdir
97 # compile normal C files
98 super(compiler
, compile_dir
, cfiles
)
100 # Gather extra C files generated elsewhere than in super
101 for f
in compiler
.extern_bodies
do
102 if f
isa ExternCFile then cfiles
.add
(f
.filename
.basename
(""))
105 ## Generate delagating makefile
106 dir
= "{android_project_root}/jni/"
108 include $(call all-subdir-makefiles)
109 """.write_to_file
("{dir}/Android.mk")
111 ### generate makefile into "{compile_dir}/Android.mk"
114 LOCAL_PATH := $(call my-dir)
115 include $(CLEAR_VARS)
117 LOCAL_CFLAGS := -D ANDROID
119 LOCAL_SRC_FILES := \\
120 {{{cfiles.join(" \\\n")}}}
121 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
122 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
124 include $(BUILD_SHARED_LIBRARY)
126 $(call import-module,android/native_app_glue)
127 """.write_to_file
("{dir}/Android.mk")
129 ### generate AndroidManifest.xml
130 dir
= android_project_root
131 """<?xml version="1.0" encoding="utf-8"?>
132 <!-- BEGIN_INCLUDE(manifest) -->
133 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
134 package="{{{app_package}}}"
135 android:versionCode="{{{project.version_code}}}"
136 android:versionName="{{{app_version}}}">
138 <!-- This is the platform API where NativeActivity was introduced. -->
140 android:minSdkVersion="{{{app_min_sdk}}}"
141 android:targetSdkVersion="{{{app_target_sdk}}}"
145 android:label="@string/app_name"
146 android:hasCode="true"
147 android:debuggable="{{{not release}}}">
149 <!-- Our activity is the built-in NativeActivity framework class.
150 This will take care of integrating with our NDK code. -->
151 <activity android:name="android.app.NativeActivity"
152 android:label="@string/app_name"
153 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
154 android:configChanges="orientation|keyboardHidden"
155 android:screenOrientation="portrait">
156 <!-- Tell NativeActivity the name of or .so -->
157 <meta-data android:name=\"{{{app_package}}}\"
158 android:value=\"{{{app_name}}}\" />
160 <action android:name="android.intent.action.MAIN" />
161 <category android:name="android.intent.category.LAUNCHER" />
165 {{{project.manifest_application_lines.join("\n")}}}
169 {{{project.manifest_lines.join("\n")}}}
172 <!-- END_INCLUDE(manifest) -->
173 """.write_to_file
("{dir}/AndroidManifest.xml")
175 ### Link to png sources
176 # libpng is not available on Android NDK
177 # FIXME make obtionnal when we have alternatives to mnit
178 var nit_dir
= toolcontext
.nit_dir
179 var share_dir
= "{nit_dir or else ""}/share/"
180 if nit_dir
== null or not share_dir
.file_exists
then
181 print
"Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
184 share_dir
= share_dir
.realpath
185 var target_png_dir
= "{android_project_root}/jni/png"
186 if not target_png_dir
.file_exists
then
187 toolcontext
.exec_and_check
(["ln", "-s", "{share_dir}/png/", target_png_dir
], "Android project error")
190 ### Link to assets (for mnit and others)
191 # This will be accessed from `android_project_root`
193 if compiler
.mainmodule
.location
.file
!= null then
194 # it is a real file, use "{file}/../assets"
195 assets_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../assets"
197 # probably used -m, use "."
198 assets_dir
= "assets"
200 if assets_dir
.file_exists
then
201 assets_dir
= assets_dir
.realpath
202 var target_assets_dir
= "{android_project_root}/assets"
203 if not target_assets_dir
.file_exists
then
204 toolcontext
.exec_and_check
(["ln", "-s", assets_dir
, target_assets_dir
], "Android project error")
208 ### copy resources (for android)
209 # This will be accessed from `android_project_root`
211 if compiler
.mainmodule
.location
.file
!= null then
212 # it is a real file, use "{file}/../res"
213 res_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../res"
215 # probably used -m, use "."
218 if res_dir
.file_exists
then
219 res_dir
= res_dir
.realpath
220 var target_res_dir
= "{android_project_root}"
221 if target_res_dir
.file_exists
then
222 # copy the res folder to .nit_compile
223 toolcontext
.exec_and_check
(["cp", "-R", res_dir
, target_res_dir
], "Android project error")
229 redef fun write_makefile
(compiler
, compile_dir
, cfiles
)
231 # Do nothing, already done in `write_files`
234 redef fun compile_c_code
(compiler
, compile_dir
)
236 var android_project_root
= android_project_root
.as(not null)
237 var release
= toolcontext
.opt_release
.value
239 # Compile C code (and thus Nit)
240 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "4", "-C", android_project_root
], "Android project error")
243 var args
= ["ant", "-q", "-f", android_project_root
+"/build.xml"]
246 else args
.add
"debug"
247 toolcontext
.exec_and_check
(args
, "Android project error")
249 # Move the apk to the target
250 var outname
= toolcontext
.opt_output
.value
251 if outname
== null then outname
= "{compiler.mainmodule.name}.apk"
255 src_apk_suffix
= "release-unsigned"
256 else src_apk_suffix
= "debug"
258 toolcontext
.exec_and_check
(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname
], "Android project error")
262 redef class JavaClassTemplate
263 redef fun write_to_files
(compdir
)
265 var jni_path
= "jni/nit_compile/"
266 if compdir
.has_suffix
(jni_path
) then
267 var path
= "{compdir.substring(0, compdir.length-jni_path.length)}/src/"