# HG changeset patch # Parent 238cd0f1c0f7686631e5e5d87e98bd54b76ebadb Basic android project creator. diff --git a/android-tools/build.gradle b/android-tools/build.gradle new file mode 100644 --- /dev/null +++ b/android-tools/build.gradle @@ -0,0 +1,385 @@ +/* + * Creates Android project for deploying JavaFX application. + * Requires: Android sdk installed, JavaFX sdk for Android, VM image for Android + * + * Parameters: + * ANDROID_SDK - path to Android sdk + * TARGET - android platform (optional). + * NAME - name of the Android application. + * PACKAGE - package of the Android application. + * DIR - path where to a project wll be created. + * DEBUG - generate vm with debug and profiling support + * JFX_SDK - path to Android JavaFX sdk. + * JFX_APP - path to JavaFX application dist folder containg all jars. + * JFX_MAIN - a main class which to start. + * + * Usage: gradle createProject + * -PANDROID_SDK= + * -PTARGET= /optional/ + * -PNAME= + * -PPACKAGE= + * -PDIR= + * -PDEBUG + * -PJFX_MAIN= + * -PJFX_SDK= + * -PJFX_APP= + */ + +def manifest = +"\n" + +"\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\n" + +"\t\n" + +"" + +def custom_rules = +"\n" + +"\n" + +"\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\n" + +"\t\n" + +"\t\n" + +"" + +def build_extras = +"\n" + +"\n" + +"\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\t\n"+ +"\t\t\t\n" + + "\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\t\n" + +"\t\t\t\t\t\n" + +"\t\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\t\t\n" + +"\t\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\t\n" + +"\t\t\n" + +"\t\t\n" + +"\t\n" + +"" + +def stringsXml = +"\n" + +"\n" + +"\t%s\n" + +"" + +def usageMessage = + "\nUsage: gradle createProject\n" + + "\t-PANDROID_SDK=\n" + + "\t-PTARGET= /optional/\n" + + "\t-PNAME=\n" + + "\t-PPACKAGE=\n" + + "\t-PDIR=\n" + + "\t-DEBUG \n" + + "\t-PJFX_MAIN=\n" + + "\t-PJFX_SDK=\n" + + "\t-PJFX_APP=\n" + +String getLatestPlatform(String path) { + def max = 0 + file("$path/platforms").eachDir() {file-> + def pname = file.getName() - "android-" + def ptf = pname?.isInteger() ? pname.toInteger() : 0 + if (max < ptf) { + max = ptf + } + } + if (max == 0) { + return null; + } + return "android-" + max +} + +boolean assertDir(File dir) { + return dir != null && dir.exists() && dir.isDirectory() && dir.canRead(); +} + +boolean assertDir(String dir) { + return dir != null && assertDir(file(dir)) +} + +boolean assertDir(String dir, Closure c) { + return assertDir(dir) ? c(dir) : false +} + +defaultTasks 'usage' + +task conf { + ext.sdk = project.hasProperty("ANDROID_SDK") ? "${ANDROID_SDK}" : null; + if (ext.sdk == null) { + ant.fail("Error: Can't build Android. Path to Android SDK was not set or is invalid!\n" + + usageMessage) + } + + if (!assertDir(ext.sdk)) { + if (!assertDir(ext.sdk)) { + ant.fail("Error: Path to Android SDK is invalid! [$ext.sdk]\n" + + usageMessage) + } + } + + ext.sdk_target = project.hasProperty("TARGET") ? "${TARGET}" : getLatestPlatform(sdk); + if (!assertDir(file("$sdk/platforms/$ext.sdk_target"))) { + def new_sdk_target = getLatestPlatform(sdk); + if (!new_sdk_target.equals(ext.sdk_target)) { + logger.warn("Warning: Redefining Android target to: [$ext.sdk_target]") + ext.sdk_target = new_sdk_target; + } else { + ant.fail("Error: Failed to resolve Android target!\n" + + usageMessage) + } + } + + ext.isDalvik = true + ext.launcher = isDalvik ? "com.oracle.dalvik.DalvikLauncher" : "com.oracle.dalvik.JavaSELauncher" + + ext.project_name = project.hasProperty("NAME") ? "${NAME}" : null + if (ext.project_name == null) { + ant.fail("Error: Project name must be specfied!\n" + + usageMessage) + } + + ext.project_package = project.hasProperty("PACKAGE") ? "${PACKAGE}" : null + if (ext.project_package == null) { + ant.fail("Error: Project package must be specified!\n" + + usageMessage) + } + + ext.jfxapp = project.hasProperty("JFX_APP") ? "${JFX_APP}" : null + if (ext.jfxapp == null) { + logger.warn("Warning: JavaFX application dist forlder must be specified!") + } + + ext.jfxsdk = project.hasProperty("JFX_SDK") ? "${JFX_SDK}" : null + if (ext.jfxsdk == null) { + logger.warn("Warning: JavaFX SDK must be specified!") + } + + ext.main_class = project.hasProperty("JFX_MAIN") ? "${JFX_MAIN}" : null + if (ext.main_class == null) { + logger.warn("Warning: Main class must be specified!") + } + + ext.workdir = project.hasProperty("DIR") ? "${DIR}" : '.' + if (new File("$ext.workdir/$ext.project_name").exists()) { + ant.fail("Error: Project file already exists!\n" + + usageMessage) + } + if (!new File("$ext.workdir").exists()) { + ant.mkdir(dir: "$ext.workdir") + } + ext.debug = project.hasProperty("DEBUG") ? true : false +} + +conf << { + println "\n====================================================" + println "Android SDK:\t\t[$conf.ext.sdk]" + println "Target:\t\t\t[$conf.ext.sdk_target]" + println "Project name:\t\t[$conf.ext.project_name]" + println "Package:\t\t[$conf.ext.project_package]" + println "JavaFX application:\t[$conf.ext.jfxapp]" + println "JavaFX sdk:\t\t[$conf.ext.jfxsdk]" + println "JavaFX main.class:\t[$conf.ext.main_class]" + println "Workdir:\t\t[$conf.ext.workdir]" + println "debug:\t\t\t[$conf.ext.debug]" + println "===================================================\n" +} + +task usage << { + println usageMessage +} + +task androidCreateProject(type:Exec, ddependsOn: conf) { + workingDir conf.ext.workdir + args ('create', 'project', '-n', conf.ext.project_name, '-p', conf.ext.project_name) + args ('-t', conf.ext.sdk_target, '-k', conf.ext.project_package, '-a', 'Activity') + executable 'android' +} + +task deleteSrcAndLayout(dependsOn: androidCreateProject) << { + ant.delete (includeEmptyDirs: 'true') { + fileset(dir: file("$conf.ext.workdir/$conf.ext.project_name/src"), includes: '*/**') + fileset(dir: file("$conf.ext.workdir/$conf.ext.project_name/res/layout"), includes: '*/**') + } +} + +task writeBaseProperties(dependsOn: deleteSrcAndLayout) << { + new File("$conf.ext.workdir/$conf.ext.project_name/base.properties").withPrintWriter{ pw-> + pw.println("jfx.sdk=$conf.ext.jfxsdk") + pw.println("jfx.app.dist.dir=$conf.ext.jfxapp") + if (conf.ext.isDalvik) { + pw.println("isDalvik=true") + } + } +} + +task updateManifest(dependsOn: writeBaseProperties) << { + def mf = new File("$conf.ext.workdir/$conf.ext.project_name/AndroidManifest.xml") + if (mf.exists()) { + mf.delete() + } + mf.withPrintWriter{ pw -> + pw.printf(manifest, "${conf.ext.project_package}", "${conf.ext.launcher}", "${conf.ext.main_class}") + } +} + +task updateStringsXml(dependsOn: updateManifest) << { + def f = new File("$conf.ext.workdir/$conf.ext.project_name/res/values/strings.xml") + if (f.exists()) { + f.delete() + } + f.withPrintWriter{ pw -> + pw.printf(stringsXml, conf.ext.project_name) + } +} + +task writeCustomRules(dependsOn: updateStringsXml) << { + new File("$conf.ext.workdir/$conf.ext.project_name/custom_rules.xml").withPrintWriter{ pw-> + pw.print("$custom_rules") + } +} + +task writeBuildExtras(dependsOn: writeCustomRules) << { + new File("$conf.ext.workdir/$conf.ext.project_name/build-extras.xml").withPrintWriter{ pw-> + pw.print("$build_extras") + } +} + + +task createProject(dependsOn: writeBuildExtras) << { + println 'Creates and set up android project for JavaFX.' +} diff --git a/android-tools/createHelloWorld.sh b/android-tools/createHelloWorld.sh new file mode 100755 --- /dev/null +++ b/android-tools/createHelloWorld.sh @@ -0,0 +1,1 @@ +gradle -PDEBUG -PDIR=/home/user/work -PNAME=HelloWorld -PPACKAGE=com.oracle.helloworld -PJFX_SDK=/home/user/work/jfx78narya/build/android-sdk -PJFX_APP=/home/user/NetBeansProjects/HelloWorld/dist -PJFX_MAIN=com.helloworld.HelloWorld createProject