The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. //==============================================================================
  22. class AndroidProjectExporter : public ProjectExporter
  23. {
  24. public:
  25. //==============================================================================
  26. static const char* getNameAndroid() { return "Android Project"; }
  27. static const char* getValueTreeTypeName() { return "ANDROID"; }
  28. static AndroidProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  29. {
  30. if (settings.hasType (getValueTreeTypeName()))
  31. return new AndroidProjectExporter (project, settings);
  32. return 0;
  33. }
  34. //==============================================================================
  35. AndroidProjectExporter (Project& project_, const ValueTree& settings_)
  36. : ProjectExporter (project_, settings_)
  37. {
  38. name = getNameAndroid();
  39. if (getTargetLocation().toString().isEmpty())
  40. getTargetLocation() = getDefaultBuildsRootFolder() + "Android";
  41. if (getSDKPath().toString().isEmpty())
  42. getSDKPath() = "${user.home}/SDKs/android-sdk-mac_x86";
  43. if (getNDKPath().toString().isEmpty())
  44. getNDKPath() = "${user.home}/SDKs/android-ndk-r5";
  45. if (getInternetNeeded().toString().isEmpty())
  46. getInternetNeeded() = true;
  47. }
  48. //==============================================================================
  49. int getLaunchPreferenceOrderForCurrentOS()
  50. {
  51. #if JUCE_ANDROID
  52. return 1;
  53. #else
  54. return 0;
  55. #endif
  56. }
  57. bool isPossibleForCurrentProject() { return projectType.isGUIApplication(); }
  58. bool usesMMFiles() const { return false; }
  59. void launchProject()
  60. {
  61. }
  62. void createPropertyEditors (Array <PropertyComponent*>& props)
  63. {
  64. ProjectExporter::createPropertyEditors (props);
  65. props.add (new TextPropertyComponent (getSDKPath(), "Android SDK Path", 1024, false));
  66. props.getLast()->setTooltip ("The path to the Android SDK folder on the target build machine");
  67. props.add (new TextPropertyComponent (getNDKPath(), "Android NDK Path", 1024, false));
  68. props.getLast()->setTooltip ("The path to the Android NDK folder on the target build machine");
  69. props.add (new BooleanPropertyComponent (getInternetNeeded(), "Internet Access", "Specify internet access permission in the manifest"));
  70. props.getLast()->setTooltip ("If enabled, this will set the android.permission.INTERNET flag in the manifest.");
  71. }
  72. Value getSDKPath() const { return getSetting (Ids::androidSDKPath); }
  73. Value getNDKPath() const { return getSetting (Ids::androidNDKPath); }
  74. Value getInternetNeeded() const { return getSetting (Ids::androidInternetNeeded); }
  75. //==============================================================================
  76. void create()
  77. {
  78. const File target (getTargetFolder());
  79. const File jniFolder (target.getChildFile ("jni"));
  80. createDirectoryOrThrow (target.getChildFile ("src/com"));
  81. createDirectoryOrThrow (jniFolder);
  82. createDirectoryOrThrow (target.getChildFile ("res/drawable-hdpi"));
  83. createDirectoryOrThrow (target.getChildFile ("res/drawable-mdpi"));
  84. createDirectoryOrThrow (target.getChildFile ("res/drawable-ldpi"));
  85. createDirectoryOrThrow (target.getChildFile ("res/values"));
  86. createDirectoryOrThrow (target.getChildFile ("libs"));
  87. createDirectoryOrThrow (target.getChildFile ("bin"));
  88. {
  89. ScopedPointer<XmlElement> manifest (createManifestXML());
  90. writeXmlOrThrow (*manifest, target.getChildFile ("AndroidManifest.xml"), "utf-8", 100);
  91. }
  92. writeApplicationMk (jniFolder.getChildFile ("Application.mk"));
  93. writeAndroidMk (jniFolder.getChildFile ("Android.mk"));
  94. {
  95. ScopedPointer<XmlElement> antBuildXml (createAntBuildXML());
  96. writeXmlOrThrow (*antBuildXml, target.getChildFile ("build.xml"), "UTF-8", 100);
  97. }
  98. writeBuildPropertiesFile (target.getChildFile ("build.properties"));
  99. writeDefaultPropertiesFile (target.getChildFile ("default.properties"));
  100. writeLocalPropertiesFile (target.getChildFile ("local.properties"));
  101. writeIcon (target.getChildFile ("res/drawable-hdpi/icon.png"), 72);
  102. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), 48);
  103. writeIcon (target.getChildFile ("res/drawable-ldpi/icon.png"), 36);
  104. writeStringsFile (target.getChildFile ("res/values/strings.xml"));
  105. }
  106. private:
  107. //==============================================================================
  108. XmlElement* createManifestXML()
  109. {
  110. XmlElement* manifest = new XmlElement ("manifest");
  111. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  112. manifest->setAttribute ("android:versionCode", "1");
  113. manifest->setAttribute ("android:versionName", "1.0");
  114. manifest->setAttribute ("package", "com.juce");
  115. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  116. screens->setAttribute ("android:smallScreens", "true");
  117. screens->setAttribute ("android:normalScreens", "true");
  118. screens->setAttribute ("android:largeScreens", "true");
  119. screens->setAttribute ("android:xlargeScreens", "true");
  120. screens->setAttribute ("android:anyDensity", "true");
  121. if (getInternetNeeded().getValue())
  122. {
  123. XmlElement* permission = manifest->createNewChildElement ("uses-permission");
  124. permission->setAttribute ("android:name", "android.permission.INTERNET");
  125. }
  126. XmlElement* app = manifest->createNewChildElement ("application");
  127. app->setAttribute ("android:label", "@string/app_name");
  128. app->setAttribute ("android:icon", "@drawable/icon");
  129. XmlElement* act = app->createNewChildElement ("activity");
  130. act->setAttribute ("android:name", "JuceAppActivity");
  131. act->setAttribute ("android:label", "@string/app_name");
  132. XmlElement* intent = act->createNewChildElement ("intent-filter");
  133. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  134. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  135. return manifest;
  136. }
  137. //==============================================================================
  138. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  139. {
  140. if (projectItem.isGroup())
  141. {
  142. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  143. findAllFilesToCompile (projectItem.getChild(i), results);
  144. }
  145. else
  146. {
  147. if (projectItem.shouldBeCompiled())
  148. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  149. }
  150. }
  151. void writeApplicationMk (const File& file)
  152. {
  153. MemoryOutputStream mo;
  154. mo << "# Automatically generated makefile, created by the Introjucer" << newLine
  155. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  156. << newLine
  157. << "APP_STL := gnustl_static" << newLine
  158. << "APP_CPPFLAGS += -fsigned-char -fexceptions -frtti" << newLine;
  159. overwriteFileIfDifferentOrThrow (file, mo);
  160. }
  161. void writeAndroidMk (const File& file)
  162. {
  163. Array<RelativePath> files;
  164. for (int i = 0; i < groups.size(); ++i)
  165. findAllFilesToCompile (groups.getReference(i), files);
  166. MemoryOutputStream mo;
  167. writeAndroidMk (mo, files);
  168. overwriteFileIfDifferentOrThrow (file, mo);
  169. }
  170. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files)
  171. {
  172. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  173. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  174. << newLine
  175. << "LOCAL_PATH := $(call my-dir)" << newLine
  176. << newLine
  177. << "include $(CLEAR_VARS)" << newLine
  178. << newLine
  179. << "LOCAL_CPP_EXTENSION := cpp" << newLine
  180. << "LOCAL_MODULE := juce_jni" << newLine
  181. << "LOCAL_SRC_FILES := \\" << newLine;
  182. for (int i = 0; i < files.size(); ++i)
  183. out << " ../" << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  184. out << newLine
  185. << "ifeq ($(CONFIG),Debug)" << newLine
  186. << " LOCAL_CPPFLAGS += " << createCPPFlags (true) << newLine
  187. << "else" << newLine
  188. << " LOCAL_CPPFLAGS += " << createCPPFlags (false) << newLine
  189. << "endif" << newLine
  190. << newLine
  191. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  192. }
  193. String createCPPFlags (bool forDebug)
  194. {
  195. String flags ("-fsigned-char -fexceptions -frtti");
  196. if (forDebug)
  197. flags << " -g";
  198. StringPairArray defines;
  199. defines.set ("JUCE_ANDROID", "1");
  200. if (forDebug)
  201. {
  202. defines.set ("DEBUG", "1");
  203. defines.set ("_DEBUG", "1");
  204. }
  205. else
  206. {
  207. defines.set ("NDEBUG", "1");
  208. }
  209. for (int i = 0; i < configs.size(); ++i)
  210. {
  211. const Project::BuildConfiguration& config = configs.getReference(i);
  212. if (config.isDebug() == forDebug)
  213. {
  214. flags << " -O" << config.getGCCOptimisationFlag();
  215. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  216. break;
  217. }
  218. }
  219. return flags + createGCCPreprocessorFlags (defines);
  220. }
  221. //==============================================================================
  222. XmlElement* createAntBuildXML()
  223. {
  224. XmlElement* proj = new XmlElement ("project");
  225. proj->setAttribute ("name", projectName);
  226. proj->setAttribute ("default", "debug");
  227. proj->createNewChildElement ("property")->setAttribute ("file", "local.properties");
  228. proj->createNewChildElement ("property")->setAttribute ("file", "build.properties");
  229. proj->createNewChildElement ("property")->setAttribute ("file", "default.properties");
  230. XmlElement* path = proj->createNewChildElement ("path");
  231. path->setAttribute ("id", "android.antlibs");
  232. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/anttasks.jar");
  233. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/sdklib.jar");
  234. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/androidprefs.jar");
  235. XmlElement* taskdef = proj->createNewChildElement ("taskdef");
  236. taskdef->setAttribute ("name", "setup");
  237. taskdef->setAttribute ("classname", "com.android.ant.SetupTask");
  238. taskdef->setAttribute ("classpathref", "android.antlibs");
  239. addNDKBuildStep (proj, "clean", "clean");
  240. //addLinkStep (proj, "${basedir}/" + rebaseFromProjectFolderToBuildTarget (RelativePath()).toUnixStyle() + "/", "jni/app");
  241. addLinkStep (proj, "${basedir}/" + getJucePathFromTargetFolder().toUnixStyle() + "/src/native/android/java/", "src/com/juce");
  242. addNDKBuildStep (proj, "debug", "CONFIG=Debug");
  243. addNDKBuildStep (proj, "release", "CONFIG=Release");
  244. proj->createNewChildElement ("setup");
  245. return proj;
  246. }
  247. static void addNDKBuildStep (XmlElement* project, const String& type, const String& arg)
  248. {
  249. XmlElement* target = project->createNewChildElement ("target");
  250. target->setAttribute ("name", type);
  251. XmlElement* executable = target->createNewChildElement ("exec");
  252. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  253. executable->setAttribute ("dir", "${basedir}");
  254. executable->setAttribute ("failonerror", "true");
  255. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  256. executable->createNewChildElement ("arg")->setAttribute ("value", arg);
  257. }
  258. static void addLinkStep (XmlElement* project, const String& from, const String& to)
  259. {
  260. XmlElement* executable = project->createNewChildElement ("exec");
  261. executable->setAttribute ("executable", "ln");
  262. executable->setAttribute ("dir", "${basedir}");
  263. executable->setAttribute ("failonerror", "false");
  264. executable->createNewChildElement ("arg")->setAttribute ("value", "-s");
  265. executable->createNewChildElement ("arg")->setAttribute ("value", from);
  266. executable->createNewChildElement ("arg")->setAttribute ("value", to);
  267. }
  268. void writeBuildPropertiesFile (const File& file)
  269. {
  270. MemoryOutputStream mo;
  271. mo << "# This file is used to override default values used by the Ant build system." << newLine;
  272. overwriteFileIfDifferentOrThrow (file, mo);
  273. }
  274. void writeDefaultPropertiesFile (const File& file)
  275. {
  276. MemoryOutputStream mo;
  277. mo << "# This file is used to override default values used by the Ant build system." << newLine
  278. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  279. << newLine
  280. << "target=android-9"
  281. << newLine;
  282. overwriteFileIfDifferentOrThrow (file, mo);
  283. }
  284. void writeLocalPropertiesFile (const File& file)
  285. {
  286. MemoryOutputStream mo;
  287. mo << "# This file is used to override default values used by the Ant build system." << newLine
  288. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  289. << newLine
  290. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPath().toString())) << newLine
  291. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPath().toString())) << newLine
  292. << newLine;
  293. overwriteFileIfDifferentOrThrow (file, mo);
  294. }
  295. void writeIcon (const File& file, int size)
  296. {
  297. Image im (getBestIconForSize (size, false));
  298. if (im.isValid())
  299. {
  300. PNGImageFormat png;
  301. MemoryOutputStream mo;
  302. if (! png.writeImageToStream (im, mo))
  303. throw SaveError ("Can't generate Android icon file");
  304. overwriteFileIfDifferentOrThrow (file, mo);
  305. }
  306. }
  307. void writeStringsFile (const File& file)
  308. {
  309. XmlElement strings ("resources");
  310. XmlElement* name = strings.createNewChildElement ("string");
  311. name->setAttribute ("name", "app_name");
  312. name->addTextElement (projectName);
  313. writeXmlOrThrow (strings, file, "utf-8", 100);
  314. }
  315. //==============================================================================
  316. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter);
  317. };
  318. #endif // __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__