create_android_project.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 ##############################################################################
00004 # Imports
00005 ##############################################################################
00006 
00007 from __future__ import print_function
00008 
00009 import os
00010 import sys
00011 import argparse
00012 import subprocess
00013 import shutil
00014 
00015 # local imports
00016 import utils
00017 import console
00018 
00019 ##############################################################################
00020 # Methods
00021 ##############################################################################
00022 
00023 
00024 def parse_arguments():
00025     argv = sys.argv[1:]
00026     parser = argparse.ArgumentParser(
00027         description='Creates a new android package based on catkin and gradle. \n')
00028     parser.add_argument('name',
00029                         nargs=1,
00030                         help='The name for the package')
00031     parser.add_argument('-t', '--target-version',
00032                         action='store',
00033                         default='15',
00034                         help='Target android version [15]')
00035     parser.add_argument('-p', '--android-package-name',
00036                         action='store',
00037                         default='com.github.rosjava.android.pkg_name',
00038                         help='Android package name (e.g. com.github.rosjava.android.pkg_name)')
00039     parser.add_argument('-a', '--author',
00040                         action='store',
00041                         default=utils.author_name(),
00042                         help='A single author, may be used multiple times')
00043     args = parser.parse_args(argv)
00044     if args.android_package_name == "com.github.rosjava.android.pkg_name":
00045         args.android_package_name = "com.github.rosjava.android.%s" % args.name[0].lower()
00046     return args
00047 
00048 
00049 def actually_create_android_project(package_name, target_version, java_package_name, is_library):
00050     path = os.path.join(os.getcwd(), package_name.lower())
00051     console.pretty_println("\nCreating android project ", console.bold)
00052     console.pretty_print("  Name      : ", console.cyan)
00053     console.pretty_println("%s" % package_name, console.yellow)
00054     console.pretty_print("  Target Ver: ", console.cyan)
00055     console.pretty_println("%s" % target_version, console.yellow)
00056     console.pretty_print("  Java Name : ", console.cyan)
00057     console.pretty_println("%s" % java_package_name, console.yellow)
00058     if is_library:
00059         console.pretty_print("  Library   : ", console.cyan)
00060         console.pretty_println("yes\n", console.yellow)
00061         cmd = ['android', 'create', 'lib-project', '-n', package_name, '-p', path, '-k', java_package_name, '-t', 'android-' + target_version, ]
00062     else:
00063         activity_name = utils.camel_case(package_name)
00064         console.pretty_print("  Activity  : ", console.cyan)
00065         console.pretty_println("%s\n" % activity_name, console.yellow)
00066         cmd = ['android', 'create', 'project', '-n', package_name, '-p', path, '-k', java_package_name, '-t', 'android-' + target_version, '-a', activity_name]
00067         print("Command: %s" % cmd)
00068     try:
00069         subprocess.check_call(cmd)
00070         print("Command: %s" % cmd)
00071     except subprocess.CalledProcessError:
00072         print("Error")
00073         raise subprocess.CalledProcessError("failed to create android project.")
00074     # This is in the old form, let's shovel the shit around to the new form
00075     utils.mkdir_p(os.path.join(path, 'src', 'main', 'java'))
00076     os.remove(os.path.join(path, 'local.properties'))
00077     os.remove(os.path.join(path, 'project.properties'))
00078     os.remove(os.path.join(path, 'ant.properties'))
00079     os.remove(os.path.join(path, 'proguard-project.txt'))
00080     os.remove(os.path.join(path, 'build.xml'))
00081     os.rmdir(os.path.join(path, 'bin'))
00082     os.rmdir(os.path.join(path, 'libs'))
00083     shutil.move(os.path.join(path, 'AndroidManifest.xml'), os.path.join(path, 'src', 'main'))
00084     shutil.move(os.path.join(path, 'res'), os.path.join(path, 'src', 'main'))
00085     if not is_library:
00086         shutil.move(os.path.join(path, 'src', java_package_name.split('.')[0]), os.path.join(path, 'src', 'main', 'java'))
00087 
00088 ##############################################################################
00089 # Methods acting on classes
00090 ##############################################################################
00091 
00092 
00093 # This inserts the labelled variables into the template wherever the corresponding
00094 # %package, %brief, %description and %depends is found.
00095 def instantiate_template(template, package_name, author, plugin_name, sdk_version):
00096     return template % locals()
00097 
00098 
00099 def create_gradle_package_files(args, author, is_library, sdk_version):
00100     '''
00101       This is almost a direct copy from catkin_create_pkg.
00102     '''
00103     plugin_name = "com.android.library" if is_library else "com.android.application"
00104     try:
00105         package_name = args.name[0].lower()
00106         package_path = os.path.abspath(os.path.join(os.getcwd(), package_name))
00107         console.pretty_println("\nCreating gradle files", console.bold)
00108         for template_name in ['build.gradle']:  # 'CMakeLists.txt']:
00109             filename = os.path.join(package_path, template_name)
00110             template = read_template_file(template_name)
00111             contents = instantiate_template(template, package_name, author, plugin_name, sdk_version)
00112             #if is_library:
00113             #    contents += extra_gradle_library_text()
00114             try:
00115                 f = open(filename, 'w')
00116                 f.write(contents)
00117                 console.pretty_print('  File: ', console.cyan)
00118                 console.pretty_println(template_name, console.yellow)
00119             finally:
00120                 f.close()
00121     except Exception:
00122         raise
00123 
00124 
00125 def add_to_root_gradle_settings(name):
00126     '''
00127       Adds project name to the root level settings.gradle file.
00128     '''
00129     for rel_path in ['.', '..']:
00130         settings_gradle_path = os.path.join(os.getcwd(), rel_path, 'settings.gradle')
00131         if os.path.isfile(settings_gradle_path):
00132             break
00133         else:
00134             settings_gradle_path = None
00135     if settings_gradle_path is None:
00136         console.pretty_println("\nCouldn't find the root level settings.gradle file - not adding to the superproject.")
00137         return
00138     with open(settings_gradle_path, 'a') as settings_gradle:
00139         console.pretty_println("\nIncluding '%s' in the root gradle project configuration (settings.gradle).\n" % name, console.bold)
00140         settings_gradle.write("include '%s'\n" % name)
00141 
00142 
00143 def extra_gradle_library_text():
00144     '''
00145       Not actually necessary until we start using maven-publish plugin. It doesn't handle dependencies right now.
00146     '''
00147     text = "\n"
00148     text += "/* http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds */\n"
00149     text += "android.libraryVariants\n"
00150     text += "publishing {\n"
00151     text += "    publications {\n"
00152     text += "        maven(MavenPublication) {\n"
00153     text += "            /* artifact bundleDebug */\n"
00154     text += "            artifact bundleRelease\n"
00155     text += "        }\n"
00156     text += "    }\n"
00157     text += "}\n"
00158     return text
00159 
00160 
00161 def create_android_project(is_library=False):
00162     args = parse_arguments()
00163     actually_create_android_project(args.name[0], args.target_version, args.android_package_name, is_library)
00164     create_gradle_package_files(args, args.author, is_library, args.target_version)
00165     add_to_root_gradle_settings(args.name[0])
00166 
00167 ##############################################################################
00168 # Borrowed from catkin_pkg.package_templates
00169 ##############################################################################
00170 
00171 
00172 def read_template_file(filename):
00173     template_dir = os.path.join(os.path.dirname(__file__), 'templates', 'android_project')
00174     template = os.path.join(template_dir, '%s.in' % filename)
00175     if not os.path.isfile(template):
00176         raise IOError(
00177             "Could not read template [%s]" % template
00178         )
00179     with open(template, 'r') as fhand:
00180         template_contents = fhand.read()
00181     return template_contents


rosjava_build_tools
Author(s): Daniel Stonier
autogenerated on Sat Jun 8 2019 19:54:56