Share
Embed
Ian Darwin - Everybody knows that you have to write Android apps in Java, right? Wrong!
This session will cover a variety of technologies that let you build and package Android apps without writing a line of Java code. Technologies covered will include App Inventor - a simple visual building-blocks approach - and focus on well-known other languages (including Python, Perl, and a cast of other scripting languages mentioned in passing), and of course that old favorite C and its overgrown offspring C++. Not to mention OpenGL and some physics and gaming frameworks like Cocos2D.
AndroidTO 2013
October 17, 2013
Transcript
56 slides
-
Android App Dev for Non-Java Devs Ian Darwin University Health
Network Centre for Global eHealth Innovation http:/ /ehealthinnovation.org O’Reilly Android Cookbook http:/ /androidcookbook.com/ 1 Thursday, 17 October, 13 -
Ian Darwin [email protected] • Android Cookbook: androidcookbook.com, O'Reilly, Amazon, ..
• Most of the contributed code samples in that book are in Java(!) and are free on github.com/ AndroidCook/Android-Cookbook-Examples repo •One subdirectory per demo project 2 Thursday, 17 October, 13 -
Android: It’s not just for Java Devs anymore! • App
Inventor • SL4a (gives Python, Perl, and more) • Ruboto (gives Ruby) • HTML5 (phonegap, Cocos2D, etc) • Other Languages (C++, Scheme, Clojure, ...) 4 Thursday, 17 October, 13 -
App Inventor • Invented by Google, using MIT technologies, Open
sourced, trademark handed over to MIT • Uses drag-and-building-blocks metaphor • Does most of what "standard" SDK can do • Encourages non-programmers to get involved in building mobile apps! –See the app on your phone as you build it 6 Thursday, 17 October, 13 -
Starting With App Inventor • http:/ /Explore.AppInventor.MIT.Edu –Tutorials, examples, curricula,
and non-obvious setup instructions • Must have Java 6 enabled in browser • Must run Setup installer on local computer • Then login with your Google/GMail account –Apps updatable from any computer; only testable if Setup run there 7 Thursday, 17 October, 13 -
Inventing An App • First page shows Drag-and-drop GUI Builder
• Open Blocks Editor button to add event handling • Then run on Emulator or real device over USB • Then package as APK and sell on Google Play app store 9 Thursday, 17 October, 13 -
App Inventor Summary • App Inventor looks easy • Takes
a while to find things - read the tutorial before ploughing in! • This can be good for non-technical people •If the navigation is explained well 12 Thursday, 17 October, 13 -
SL4A • SL4a allows an open-ended number of scripting languages
to be used • Already: Python (2.6) Perl JRuby sh/bash lua BeanShell Tcl JavaScript(Rhino) • Not in Play Store, must download • http:/ /code.google.com/p/android-scripting/ • After downloading, then must add interpreter(s) • Hidden: Menu->View->Interpreters->Add • For Perl and Python, there are forked projects to simplify download 14 Thursday, 17 October, 13 -
SL4A API • Get an "Android" object • Everything starts
from there. • Huge API • http:/ /code.google.com/p/android- scripting/wiki/ApiReference 16 Thursday, 17 October, 13 -
hello.py • $ more hello_world.py • import android • droid
= android.Android() • droid.makeToast('Hello, Android!') 17 Thursday, 17 October, 13 -
sms.py droid = android.Android() number = ‘647-555-5555’ message = ‘Hello
via sms’ stat = droid.smsSend(number, message); 18 Thursday, 17 October, 13 -
Managing Scripts • Script editing tedious - it aint vi/emacs
• Intended for on-device experimentation • Try keeping in CVS/SVN/Git and copying •adb push/pull • Better - native version control client? 19 Thursday, 17 October, 13 -
phonepicker.py import android d = android.Android() num = d.pickPhone().result d.phoneCallNumber(num)
20 Thursday, 17 October, 13 -
ifconfig.py • Runs netcfg, extracts external ip address ifs =
{} r=r'^(\w+\d?)\s+(UP|DOWN)\s+([\d.]+).*' proc = os.popen('netcfg','r') for line in proc.readlines(): m=re.match(r, line) if m and m.group(1) != 'lo' and m.group(2)=='UP': ifname=m.group(1) ipaddr=m.group(3) ifs[ifname] = ipaddr # droid.makeToast(str(ifs)) title = 'ifconfig output' message = str(ifs) droid.dialogCreateAlert(title, message) droid.dialogSetPositiveButtonText('Happy now!') droid.dialogShow() response = droid.dialogGetResponse().result 21 Thursday, 17 October, 13 -
Longer Examples.py See test.py - exercises many bits of API
See “Judge Droid”, start at cloning Locale! http:/ /www.morelightmorelight.com/ 2010/04/05/judge-droid-lays-down- the-law/ 23 Thursday, 17 October, 13 -
Perl 5.10 Examples • Basically same API but with Perl
syntax • e.g. Speaking: • my $result = $droid->ttsSpeak(message); • Compare test.py and test.pl in the googlecode downloads 24 Thursday, 17 October, 13 -
APK how-to • Manual method •create project, copy interpreter, etc.
• Python, Perl Projects: • http:/ /code.google.com/p/android- python27 / • http:/ /code.google.com/p/perl-android- apk/ 25 Thursday, 17 October, 13 -
Sharing Your Script • In Qr Code - they’re not
just for URLs!! • Visit http:// zxing.appspot.com/ generator/ • Set mode to text, size to Large • Put default script name as first line • Paste your script, click Generate and it’s done! • As APK https:/ /androidcookbook.com/r/2916 26 Thursday, 17 October, 13 -
What would a Java geek coming to Android want? Standard
Java includes javax.scripting that lets you do all that from within Java app If Android had this, nirvana? A partial implementation actually exists, using V8 JS engine, at https:/ / code.google.com/p/jav8/ 27 Thursday, 17 October, 13 -
Ruboto - Ruby for the Robot? • Based on JRuby
• 1) Ruboto-irb is an Interactive Ruby app (in the Play Store). •Has own script editor • 2) Main Ruboto runs on desktop PC, builds apks –Requires Ruboto Core installed on device 29 Thursday, 17 October, 13 -
Ruboto API More like “standard” Android API Activity class: Define
methods like on_create, on_resume as per Android api 30 Thursday, 17 October, 13 -
Hello Ruboto # # Hello, World (imports omitted!) # def
self.hello_world(context) java_import "android.view.Gravity" ruboto_import_widgets :TextView context.start_ruboto_activity "$hello_world" do setTitle "App/Activity/Hello World" def on_create(bundle) setContentView(text_view :text => "Hello, World!", :gravity => (Gravity::CENTER_HORIZONTAL | Gravity::CENTER_VERTICAL)) end end end 31 Thursday, 17 October, 13 -
$activity.start_ruboto_activity "$glsurface" do setTitle "GLSurfaceView" def on_create(bundle) @surface_view = TouchSurfaceView.new(self)
@surface_view.renderer = RubotoGLSurfaceViewRenderer.new self.content_view = @surface_view end def on_resume @surface_view.on_resume end ... end Fraction of OpenGL demo 32 Thursday, 17 October, 13 -
HTML5: Phonegap/ Cordova Topic has been done to death! Write
a mobile web app Package with PhoneGap a.k.a. Cordova Pro - gets you other platforms Cons - hard to look native Covered in other sessions at most conferences! 34 Thursday, 17 October, 13 -
HTML5 + Cocos2D Cocos2D is two frameworks, one for C++
and one for HTML5 Both multi-platform Both include other scripting langs Lua See http:/ /www.cocos2d-x.org 35 Thursday, 17 October, 13 -
Cocos2Dx Code? No code example since - being C++ -
you need about 10 files to run “Hello World” 5 in your app, 5 in proj.mac, more in proj.android, more in proj.blackberry, etc XCode built hello app for Mac; Android version failed the “5 minute test” - did not build on my test system 36 Thursday, 17 October, 13 -
And the lord divided them, and gave them all different
languages... • C/C++ - NDK • Scheme (Kawa) • Scala • Clojure • You name it... 39 Thursday, 17 October, 13 -
NDK Call C or C++ from Java - same as
SE public native foo(int bar); static {System.loadLibrary(“bar”);} Compile, run javah, implement C code defined in header Build Makefile fragments tell Make to build bar.so, install in apk 40 Thursday, 17 October, 13 -
package foo.ndkdemo; public class Main { static { System.loadLibrary("sqrt-demo"); }
public static native double sqrtInC(double d); $ cat jni/sqrt-demo.c #include <stdio.h> #include <stdlib.h> #include "foo_ndkdemo_SqrtDemo.h" JNIEXPORT jdouble JNICALL Java_foo_ndkdemo_SqrtDemo_sqrtInC( JNIEnv *env, jclass clazz, jdouble d) { jdouble x0 = 10.0, x1 = d, diff; do { x1 = x0 - (((x0 * x0) - d) / (x0 * 2)); diff = x1 - x0; // printf("x0=%12.8f, x1=%12.8f, diff=%12.8f\n", // x0, x1, diff); x0 = x1; } while (labs(diff) > foo_ndkdemo_SqrtDemo_EPSILON); return x1; } 41 Thursday, 17 October, 13 -
C++: All OpenGL Games commonly provide entire UI in OpenGL,
app logic in C/C++ Supported by Android (NDK or NativeActivity), iOS, BB10 (and probably everything else) Good performance, reasonable portability Good for “games-like” UI; not for “standard” interactive app (lists) 42 Thursday, 17 October, 13 -
Android NativeActivity Android lifecycle (onCreate(), onResume() etc.) not smooth fit
for OpenGL lifecycle (init, resize, display) They provide android_native_app_glue and an Android-specific lifecycle, running own thread See http:/ /developer.android.com/tools/ sdk/ndk/; example at http:/ / developer.android.com/reference/ android/app/NativeActivity.html 43 Thursday, 17 October, 13 -
C++ + GUI Toolkit QT Toolkit covers more than just
GUI Widely used in open source (KDE) and is the base for BB10 GUI! But: BB “Cascades” widgets BB10-only Projects adapting QT to platforms e.g., “Necessitas” = QT for Android Like Android: XML layouts and GUI builder (We need FOSS “qml to Android XML”) 44 Thursday, 17 October, 13 -
C++ + Tooling: MoSync MoSync provides Eclipse-based IDE, open source
tools for: C++ app gen, app runs on many platforms (widest range of any?) JavaScript No server dependencies (unlike competing projects) 45 Thursday, 17 October, 13 -
C++ + Tooling: MoSync MoSync provides Eclipse-based IDE, open source
tools for: C++ app gen, app runs on many platforms (widest range of any?) JavaScript No server dependencies (unlike competing projects) 45 Thursday, 17 October, 13 -
Kawa Scheme Web site documents its use It is used
in App Inventor We found it difficult to install http:/ /per.bothner.com/blog/2010/ AndroidHelloScheme/ http:/ /androidscheme.blogspot.ca/ 46 Thursday, 17 October, 13 -
Scala Scala is a JVM language that tries to be
“ a better Java” introducing Functional Programming http:/ /robots.thoughtbot.com/post/ 5836463058/scala-a-better-java-for- android 47 Thursday, 17 October, 13 -
package com.thoughtbot.helloscala import _root_.android.app.Activity import _root_.android.os.Bundle import _root_.android.widget.Toast import _root_.android.view.View
import _root_.android.view.View.OnClickListener import _root_.android.widget.Button class HelloActivity extends Activity { override def onCreate(savedInstanceState : Bundle) { super.onCreate(savedInstanceState) setContentView(R.layout.main) val button = findViewById(R.id.button).asInstanceOf[Button] button.setOnClickListener(new View.OnClickListener() { def onClick(v : View) { Toast.makeText(this, "You have clicked the button", Toast.LENGTH_LONG).show() } }) } } Hello.scala This is Java-like style Scala people prefer traits and implicits (similar to AOP?) See web page for better example!! 48 Thursday, 17 October, 13 -
Clojure A Lisp-1 language for the JVM with full Java
integration Android support at http:/ /clojure- android.blogspot.ca/ 49 Thursday, 17 October, 13 -
Others Are there other languages? Undoubtedly Do I have time
to track them all down? No AndroidCookbook.com is crowdsourced; add your favorite! 50 Thursday, 17 October, 13 -
References- Other Books Android Design Patterns book (Nudelman) APress Android
Games books (OpenGL) Robert Green Infinite # of books on: Android, iOS, x-plat, PhoneGap, etc etc... 52 Thursday, 17 October, 13 -
Code Samples Colin Eberhardt’s PropertyCross project implements a realistic app
(Real Estate Property Search) in several platforms Open-source Tries to measure code sharing too 53 Thursday, 17 October, 13 -
Code Samples Colin Eberhardt’s PropertyCross project implements a realistic app
(Real Estate Property Search) in several platforms Open-source Tries to measure code sharing too 53 Thursday, 17 October, 13