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.

548 lines
22KB

  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 nullptr;
  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 (getActivityClassPath().toString().isEmpty())
  42. getActivityClassPath() = createDefaultClassName();
  43. if (getSDKPath().toString().isEmpty())
  44. getSDKPath() = "${user.home}/SDKs/android-sdk-macosx";
  45. if (getNDKPath().toString().isEmpty())
  46. getNDKPath() = "${user.home}/SDKs/android-ndk-r7";
  47. if (getInternetNeeded().toString().isEmpty())
  48. getInternetNeeded() = true;
  49. }
  50. //==============================================================================
  51. int getLaunchPreferenceOrderForCurrentOS()
  52. {
  53. #if JUCE_ANDROID
  54. return 1;
  55. #else
  56. return 0;
  57. #endif
  58. }
  59. bool isPossibleForCurrentProject() { return projectType.isGUIApplication(); }
  60. bool usesMMFiles() const { return false; }
  61. bool canCopeWithDuplicateFiles() { return false; }
  62. void launchProject()
  63. {
  64. }
  65. void createPropertyEditors (PropertyListBuilder& props)
  66. {
  67. ProjectExporter::createPropertyEditors (props);
  68. props.add (new TextPropertyComponent (getActivityClassPath(), "Android Activity class name", 256, false),
  69. "The full java class name to use for the app's Activity class.");
  70. props.add (new TextPropertyComponent (getSDKPath(), "Android SDK Path", 1024, false),
  71. "The path to the Android SDK folder on the target build machine");
  72. props.add (new TextPropertyComponent (getNDKPath(), "Android NDK Path", 1024, false),
  73. "The path to the Android NDK folder on the target build machine");
  74. props.add (new BooleanPropertyComponent (getInternetNeeded(), "Internet Access", "Specify internet access permission in the manifest"),
  75. "If enabled, this will set the android.permission.INTERNET flag in the manifest.");
  76. }
  77. Value getActivityClassPath() const { return getSetting (Ids::androidActivityClass); }
  78. Value getSDKPath() const { return getSetting (Ids::androidSDKPath); }
  79. Value getNDKPath() const { return getSetting (Ids::androidNDKPath); }
  80. Value getInternetNeeded() const { return getSetting (Ids::androidInternetNeeded); }
  81. String createDefaultClassName() const
  82. {
  83. String s (project.getBundleIdentifier().toString().toLowerCase());
  84. if (s.length() > 5
  85. && s.containsChar ('.')
  86. && s.containsOnly ("abcdefghijklmnopqrstuvwxyz_.")
  87. && ! s.startsWithChar ('.'))
  88. {
  89. if (! s.endsWithChar ('.'))
  90. s << ".";
  91. }
  92. else
  93. {
  94. s = "com.yourcompany.";
  95. }
  96. return s + CodeHelpers::makeValidIdentifier (project.getProjectFilenameRoot(), false, true, false);
  97. }
  98. //==============================================================================
  99. void create (const OwnedArray<LibraryModule>& modules)
  100. {
  101. const File target (getTargetFolder());
  102. const File jniFolder (target.getChildFile ("jni"));
  103. copyActivityJavaFiles (modules);
  104. createDirectoryOrThrow (jniFolder);
  105. createDirectoryOrThrow (target.getChildFile ("res").getChildFile ("values"));
  106. createDirectoryOrThrow (target.getChildFile ("libs"));
  107. createDirectoryOrThrow (target.getChildFile ("bin"));
  108. {
  109. ScopedPointer<XmlElement> manifest (createManifestXML());
  110. writeXmlOrThrow (*manifest, target.getChildFile ("AndroidManifest.xml"), "utf-8", 100, true);
  111. }
  112. writeApplicationMk (jniFolder.getChildFile ("Application.mk"));
  113. writeAndroidMk (jniFolder.getChildFile ("Android.mk"));
  114. {
  115. ScopedPointer<XmlElement> antBuildXml (createAntBuildXML());
  116. writeXmlOrThrow (*antBuildXml, target.getChildFile ("build.xml"), "UTF-8", 100);
  117. }
  118. writeProjectPropertiesFile (target.getChildFile ("project.properties"));
  119. writeLocalPropertiesFile (target.getChildFile ("local.properties"));
  120. writeStringsFile (target.getChildFile ("res/values/strings.xml"));
  121. const Image bigIcon (getBigIcon());
  122. const Image smallIcon (getSmallIcon());
  123. if (bigIcon.isValid() && smallIcon.isValid())
  124. {
  125. const int step = jmax (bigIcon.getWidth(), bigIcon.getHeight()) / 8;
  126. writeIcon (target.getChildFile ("res/drawable-xhdpi/icon.png"), getBestIconForSize (step * 8, false));
  127. writeIcon (target.getChildFile ("res/drawable-hdpi/icon.png"), getBestIconForSize (step * 6, false));
  128. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), getBestIconForSize (step * 4, false));
  129. writeIcon (target.getChildFile ("res/drawable-ldpi/icon.png"), getBestIconForSize (step * 3, false));
  130. }
  131. else
  132. {
  133. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), bigIcon.isValid() ? bigIcon : smallIcon);
  134. }
  135. }
  136. protected:
  137. //==============================================================================
  138. class AndroidBuildConfiguration : public BuildConfiguration
  139. {
  140. public:
  141. AndroidBuildConfiguration (Project& project, const ValueTree& settings)
  142. : BuildConfiguration (project, settings)
  143. {
  144. androidDynamicLibs.add ("GLESv1_CM");
  145. androidDynamicLibs.add ("GLESv2");
  146. if (getArchitectures().toString().isEmpty())
  147. getArchitectures() = "armeabi armeabi-v7a";
  148. }
  149. Value getArchitectures() const { return getValue (Ids::androidArchitectures); }
  150. void createPropertyEditors (PropertyListBuilder& props)
  151. {
  152. createBasicPropertyEditors (props);
  153. props.add (new TextPropertyComponent (getArchitectures(), "Architectures", 256, false),
  154. "A list of the ARM architectures to build (for a fat binary).");
  155. }
  156. StringArray androidDynamicLibs;
  157. };
  158. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  159. {
  160. return new AndroidBuildConfiguration (project, settings);
  161. }
  162. private:
  163. //==============================================================================
  164. XmlElement* createManifestXML()
  165. {
  166. XmlElement* manifest = new XmlElement ("manifest");
  167. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  168. manifest->setAttribute ("android:versionCode", "1");
  169. manifest->setAttribute ("android:versionName", "1.0");
  170. manifest->setAttribute ("package", getActivityClassPackage());
  171. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  172. screens->setAttribute ("android:smallScreens", "true");
  173. screens->setAttribute ("android:normalScreens", "true");
  174. screens->setAttribute ("android:largeScreens", "true");
  175. //screens->setAttribute ("android:xlargeScreens", "true");
  176. screens->setAttribute ("android:anyDensity", "true");
  177. if (getInternetNeeded().getValue())
  178. {
  179. XmlElement* permission = manifest->createNewChildElement ("uses-permission");
  180. permission->setAttribute ("android:name", "android.permission.INTERNET");
  181. }
  182. XmlElement* app = manifest->createNewChildElement ("application");
  183. app->setAttribute ("android:label", "@string/app_name");
  184. app->setAttribute ("android:icon", "@drawable/icon");
  185. XmlElement* act = app->createNewChildElement ("activity");
  186. act->setAttribute ("android:name", getActivityName());
  187. act->setAttribute ("android:label", "@string/app_name");
  188. XmlElement* intent = act->createNewChildElement ("intent-filter");
  189. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  190. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  191. return manifest;
  192. }
  193. //==============================================================================
  194. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  195. {
  196. if (projectItem.isGroup())
  197. {
  198. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  199. findAllFilesToCompile (projectItem.getChild(i), results);
  200. }
  201. else
  202. {
  203. if (projectItem.shouldBeCompiled())
  204. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  205. }
  206. }
  207. //==============================================================================
  208. String getActivityName() const
  209. {
  210. return getActivityClassPath().toString().fromLastOccurrenceOf (".", false, false);
  211. }
  212. String getActivityClassPackage() const
  213. {
  214. return getActivityClassPath().toString().upToLastOccurrenceOf (".", false, false);
  215. }
  216. String getJNIActivityClassName() const
  217. {
  218. return getActivityClassPath().toString().replaceCharacter ('.', '/');
  219. }
  220. static LibraryModule* getCoreModule (const OwnedArray<LibraryModule>& modules)
  221. {
  222. for (int i = modules.size(); --i >= 0;)
  223. if (modules.getUnchecked(i)->getID() == "juce_core")
  224. return modules.getUnchecked(i);
  225. return nullptr;
  226. }
  227. void copyActivityJavaFiles (const OwnedArray<LibraryModule>& modules)
  228. {
  229. const String className (getActivityName());
  230. const String package (getActivityClassPackage());
  231. String path (package.replaceCharacter ('.', File::separator));
  232. if (path.isEmpty() || className.isEmpty())
  233. throw SaveError ("Invalid Android Activity class name: " + getActivityClassPath().toString());
  234. const File classFolder (getTargetFolder().getChildFile ("src")
  235. .getChildFile (path));
  236. createDirectoryOrThrow (classFolder);
  237. LibraryModule* const coreModule = getCoreModule (modules);
  238. if (coreModule == nullptr)
  239. throw SaveError ("To build an Android app, the juce_core module must be included in your project!");
  240. File javaDestFile (classFolder.getChildFile (className + ".java"));
  241. File javaSourceFile (coreModule->getFolder().getChildFile ("native")
  242. .getChildFile ("java")
  243. .getChildFile ("JuceAppActivity.java"));
  244. MemoryOutputStream newFile;
  245. newFile << javaSourceFile.loadFileAsString()
  246. .replace ("JuceAppActivity", className)
  247. .replace ("package com.juce;", "package " + package + ";");
  248. overwriteFileIfDifferentOrThrow (javaDestFile, newFile);
  249. }
  250. void writeApplicationMk (const File& file)
  251. {
  252. MemoryOutputStream mo;
  253. mo << "# Automatically generated makefile, created by the Introjucer" << newLine
  254. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  255. << newLine
  256. << "APP_STL := gnustl_static" << newLine
  257. << "APP_CPPFLAGS += -fsigned-char -fexceptions -frtti" << newLine
  258. << "APP_PLATFORM := android-7" << newLine;
  259. overwriteFileIfDifferentOrThrow (file, mo);
  260. }
  261. void writeAndroidMk (const File& file)
  262. {
  263. Array<RelativePath> files;
  264. for (int i = 0; i < groups.size(); ++i)
  265. findAllFilesToCompile (groups.getReference(i), files);
  266. MemoryOutputStream mo;
  267. writeAndroidMk (mo, files);
  268. overwriteFileIfDifferentOrThrow (file, mo);
  269. }
  270. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files)
  271. {
  272. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  273. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  274. << newLine
  275. << "LOCAL_PATH := $(call my-dir)" << newLine
  276. << newLine
  277. << "include $(CLEAR_VARS)" << newLine
  278. << newLine
  279. << "LOCAL_MODULE := juce_jni" << newLine
  280. << "LOCAL_SRC_FILES := \\" << newLine;
  281. for (int i = 0; i < files.size(); ++i)
  282. out << " ../" << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  283. String debugSettings, releaseSettings;
  284. out << newLine
  285. << "ifeq ($(CONFIG),Debug)" << newLine;
  286. writeConfigSettings (out, true);
  287. out << "else" << newLine;
  288. writeConfigSettings (out, false);
  289. out << "endif" << newLine
  290. << newLine
  291. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  292. }
  293. void writeConfigSettings (OutputStream& out, bool forDebug)
  294. {
  295. for (ConfigIterator config (*this); config.next();)
  296. {
  297. if (config->isDebug() == forDebug)
  298. {
  299. const AndroidBuildConfiguration& androidConfig = dynamic_cast <const AndroidBuildConfiguration&> (*config);
  300. out << " LOCAL_CPPFLAGS += " << createCPPFlags (*config) << newLine
  301. << " APP_ABI := " << androidConfig.getArchitectures().toString() << newLine
  302. << getDynamicLibs (androidConfig);
  303. break;
  304. }
  305. }
  306. }
  307. String getDynamicLibs (const AndroidBuildConfiguration& config)
  308. {
  309. if (config.androidDynamicLibs.size() == 0)
  310. return String::empty;
  311. String flags (" LOCAL_LDLIBS :=");
  312. flags << config.getGCCLibraryPathFlags();
  313. for (int i = 0; i < config.androidDynamicLibs.size(); ++i)
  314. flags << " -l" << config.androidDynamicLibs[i];
  315. return flags + newLine;
  316. }
  317. String createIncludePathFlags (const BuildConfiguration& config)
  318. {
  319. String flags;
  320. StringArray searchPaths (extraSearchPaths);
  321. searchPaths.addArray (config.getHeaderSearchPaths());
  322. searchPaths.removeDuplicates (false);
  323. for (int i = 0; i < searchPaths.size(); ++i)
  324. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  325. return flags;
  326. }
  327. String createCPPFlags (const BuildConfiguration& config)
  328. {
  329. StringPairArray defines;
  330. defines.set ("JUCE_ANDROID", "1");
  331. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSNAME", getJNIActivityClassName().replaceCharacter ('/', '_'));
  332. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSPATH", "\\\"" + getJNIActivityClassName() + "\\\"");
  333. String flags ("-fsigned-char -fexceptions -frtti");
  334. if (config.isDebug().getValue())
  335. {
  336. flags << " -g";
  337. defines.set ("DEBUG", "1");
  338. defines.set ("_DEBUG", "1");
  339. }
  340. else
  341. {
  342. defines.set ("NDEBUG", "1");
  343. }
  344. flags << createIncludePathFlags (config)
  345. << " -O" << config.getGCCOptimisationFlag();
  346. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  347. return flags + createGCCPreprocessorFlags (defines);
  348. }
  349. //==============================================================================
  350. XmlElement* createAntBuildXML()
  351. {
  352. XmlElement* proj = new XmlElement ("project");
  353. proj->setAttribute ("name", projectName);
  354. proj->setAttribute ("default", "debug");
  355. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "local.properties");
  356. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "project.properties");
  357. XmlElement* path = proj->createNewChildElement ("path");
  358. path->setAttribute ("id", "android.antlibs");
  359. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/anttasks.jar");
  360. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/sdklib.jar");
  361. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/androidprefs.jar");
  362. XmlElement* taskdef = proj->createNewChildElement ("taskdef");
  363. taskdef->setAttribute ("name", "setup");
  364. taskdef->setAttribute ("classname", "com.android.ant.SetupTask");
  365. taskdef->setAttribute ("classpathref", "android.antlibs");
  366. addNDKBuildStep (proj, "clean", "clean");
  367. addNDKBuildStep (proj, "debug", "CONFIG=Debug");
  368. addNDKBuildStep (proj, "release", "CONFIG=Release");
  369. proj->createNewChildElement ("import")->setAttribute ("file", "${sdk.dir}/tools/ant/build.xml");
  370. return proj;
  371. }
  372. static void addNDKBuildStep (XmlElement* project, const String& type, const String& arg)
  373. {
  374. XmlElement* target = project->createNewChildElement ("target");
  375. target->setAttribute ("name", type);
  376. XmlElement* executable = target->createNewChildElement ("exec");
  377. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  378. executable->setAttribute ("dir", "${basedir}");
  379. executable->setAttribute ("failonerror", "true");
  380. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  381. executable->createNewChildElement ("arg")->setAttribute ("value", arg);
  382. }
  383. void writeProjectPropertiesFile (const File& file)
  384. {
  385. MemoryOutputStream mo;
  386. mo << "# This file is used to override default values used by the Ant build system." << newLine
  387. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  388. << newLine
  389. << "target=Google Inc.:Google APIs:7" << newLine
  390. << newLine;
  391. overwriteFileIfDifferentOrThrow (file, mo);
  392. }
  393. void writeLocalPropertiesFile (const File& file)
  394. {
  395. MemoryOutputStream mo;
  396. mo << "# This file is used to override default values used by the Ant build system." << newLine
  397. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  398. << newLine
  399. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPath().toString())) << newLine
  400. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPath().toString())) << newLine
  401. << newLine;
  402. overwriteFileIfDifferentOrThrow (file, mo);
  403. }
  404. void writeIcon (const File& file, const Image& im)
  405. {
  406. if (im.isValid())
  407. {
  408. createDirectoryOrThrow (file.getParentDirectory());
  409. PNGImageFormat png;
  410. MemoryOutputStream mo;
  411. if (! png.writeImageToStream (im, mo))
  412. throw SaveError ("Can't generate Android icon file");
  413. overwriteFileIfDifferentOrThrow (file, mo);
  414. }
  415. }
  416. void writeStringsFile (const File& file)
  417. {
  418. XmlElement strings ("resources");
  419. XmlElement* name = strings.createNewChildElement ("string");
  420. name->setAttribute ("name", "app_name");
  421. name->addTextElement (projectName);
  422. writeXmlOrThrow (strings, file, "utf-8", 100);
  423. }
  424. //==============================================================================
  425. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter);
  426. };
  427. #endif // __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__