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.

579 lines
24KB

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