1bec91400c494f946eb3d8b3ca85339735ead17b
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 default_outname
(mainmodule
) do return "{mainmodule.name}.apk"
54 redef fun write_files
(compiler
, compile_dir
, cfiles
)
56 var android_project_root
= android_project_root
.as(not null)
57 var project
= toolcontext
.modelbuilder
.android_project_for
(compiler
.mainmodule
)
58 var short_project_name
= compiler
.mainmodule
.name
59 var release
= toolcontext
.opt_release
.value
61 var app_name
= project
.name
62 if app_name
== null then app_name
= compiler
.mainmodule
.name
64 var app_package
= project
.java_package
65 if app_package
== null then app_package
= "org.nitlanguage.{short_project_name}"
67 var app_version
= project
.version
68 if app_version
== null then app_version
= "1.0"
70 var app_min_api
= project
.min_api
71 if app_min_api
== null then app_min_api
= 10
73 var app_target_api
= project
.target_api
74 if app_target_api
== null then app_target_api
= app_min_api
77 if project
.max_api
!= null then app_max_api
= "android:maxSdkVersion=\"{project.max_api.as(not null)}\
""
79 # Clear the previous android project, so there is no "existing project warning"
80 # or conflict between Java files of different projects
81 if android_project_root
.file_exists
then android_project_root
.rmdir
83 var args
= ["android", "-s",
85 "--name", short_project_name
,
86 "--target", "android-{app_target_api}",
87 "--path", android_project_root
,
88 "--package", app_package
,
89 "--activity", short_project_name
]
90 toolcontext
.exec_and_check
(args
, "Android project error")
93 var dir
= "{android_project_root}/jni/"
94 if not dir
.file_exists
then dir
.mkdir
97 if not dir
.file_exists
then dir
.mkdir
99 # compile normal C files
100 super(compiler
, compile_dir
, cfiles
)
102 # Gather extra C files generated elsewhere than in super
103 for f
in compiler
.extern_bodies
do
104 if f
isa ExternCFile then cfiles
.add
(f
.filename
.basename
(""))
107 ## Generate delagating makefile
108 dir
= "{android_project_root}/jni/"
110 include $(call all-subdir-makefiles)
111 """.write_to_file
("{dir}/Android.mk")
113 ### generate makefile into "{compile_dir}/Android.mk"
116 LOCAL_PATH := $(call my-dir)
117 include $(CLEAR_VARS)
119 LOCAL_CFLAGS := -D ANDROID
121 LOCAL_SRC_FILES := \\
122 {{{cfiles.join(" \\\n")}}}
123 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
124 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
126 include $(BUILD_SHARED_LIBRARY)
128 $(call import-module,android/native_app_glue)
129 """.write_to_file
("{dir}/Android.mk")
131 ### generate AndroidManifest.xml
132 dir
= android_project_root
133 """<?xml version="1.0" encoding="utf-8"?>
134 <!-- BEGIN_INCLUDE(manifest) -->
135 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
136 package="{{{app_package}}}"
137 android:versionCode="{{{project.version_code}}}"
138 android:versionName="{{{app_version}}}">
140 <!-- This is the platform API where NativeActivity was introduced. -->
142 android:minSdkVersion="{{{app_min_api}}}"
143 android:targetSdkVersion="{{{app_target_api}}}"
147 android:label="@string/app_name"
148 android:hasCode="true"
149 android:debuggable="{{{not release}}}">
151 <!-- Our activity is the built-in NativeActivity framework class.
152 This will take care of integrating with our NDK code. -->
153 <activity android:name="android.app.NativeActivity"
154 android:label="@string/app_name"
155 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
156 android:configChanges="orientation|keyboardHidden"
157 android:screenOrientation="portrait">
158 <!-- Tell NativeActivity the name of or .so -->
159 <meta-data android:name=\"{{{app_package}}}\"
160 android:value=\"{{{app_name}}}\" />
162 <action android:name="android.intent.action.MAIN" />
163 <category android:name="android.intent.category.LAUNCHER" />
167 {{{project.manifest_application_lines.join("\n")}}}
171 {{{project.manifest_lines.join("\n")}}}
174 <!-- END_INCLUDE(manifest) -->
175 """.write_to_file
("{dir}/AndroidManifest.xml")
177 ### Link to png sources
178 # libpng is not available on Android NDK
179 # FIXME make obtionnal when we have alternatives to mnit
180 var nit_dir
= toolcontext
.nit_dir
181 var share_dir
= "{nit_dir or else ""}/share/"
182 if nit_dir
== null or not share_dir
.file_exists
then
183 print
"Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
186 share_dir
= share_dir
.realpath
187 var target_png_dir
= "{android_project_root}/jni/png"
188 if not target_png_dir
.file_exists
then
189 toolcontext
.exec_and_check
(["ln", "-s", "{share_dir}/png/", target_png_dir
], "Android project error")
192 ### Link to assets (for mnit and others)
193 # This will be accessed from `android_project_root`
195 if compiler
.mainmodule
.location
.file
!= null then
196 # it is a real file, use "{file}/../assets"
197 assets_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../assets"
199 # probably used -m, use "."
200 assets_dir
= "assets"
202 if assets_dir
.file_exists
then
203 assets_dir
= assets_dir
.realpath
204 var target_assets_dir
= "{android_project_root}/assets"
205 if not target_assets_dir
.file_exists
then
206 toolcontext
.exec_and_check
(["ln", "-s", assets_dir
, target_assets_dir
], "Android project error")
210 ### copy resources (for android)
211 # This will be accessed from `android_project_root`
213 if compiler
.mainmodule
.location
.file
!= null then
214 # it is a real file, use "{file}/../res"
215 res_dir
= "{compiler.mainmodule.location.file.filename.dirname}/../res"
217 # probably used -m, use "."
220 if res_dir
.file_exists
then
221 # copy the res folder to .nit_compile
222 res_dir
= res_dir
.realpath
223 var target_res_dir
= "{android_project_root}"
224 toolcontext
.exec_and_check
(["cp", "-R", res_dir
, target_res_dir
], "Android project error")
226 # Create our own custom `res/values/string.xml` with the App name
227 """<?xml version="1.0" encoding="utf-8"?>
229 <string name="app_name">{{{app_name}}}</string>
230 </resources>""".write_to_file
"{dir}/res/values/strings.xml"
234 redef fun write_makefile
(compiler
, compile_dir
, cfiles
)
236 # Do nothing, already done in `write_files`
239 redef fun compile_c_code
(compiler
, compile_dir
)
241 var android_project_root
= android_project_root
.as(not null)
242 var release
= toolcontext
.opt_release
.value
244 # Compile C code (and thus Nit)
245 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "4", "-C", android_project_root
], "Android project error")
248 var args
= ["ant", "-q", "-f", android_project_root
+"/build.xml"]
251 else args
.add
"debug"
252 toolcontext
.exec_and_check
(args
, "Android project error")
254 # Move the apk to the target
255 var outname
= outfile
(compiler
.mainmodule
)
259 src_apk_suffix
= "release-unsigned"
260 else src_apk_suffix
= "debug"
262 toolcontext
.exec_and_check
(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname
], "Android project error")
266 redef class JavaClassTemplate
267 redef fun write_to_files
(compdir
)
269 var jni_path
= "jni/nit_compile/"
270 if compdir
.has_suffix
(jni_path
) then
271 var path
= "{compdir.substring(0, compdir.length-jni_path.length)}/src/"