SL4A的全称为Scripting Layer for Android, 顾名思义就是Android的脚本架构层,它的目的就是可以用熟知的脚本开发语言来开发Android应用程序。其工作原理基于RPC远程调用,通过本地的脚本解析器和远端的原生态Android Server层的APK进行信息交互,即实现一个远程代理,把本地脚本的函数调用通过json格式的封装,传递给远程原生态Server APK进行实际的android系统函数呼叫,最后将操作的执行结果反馈给本地脚本解析器,然后再在终端显示出运行结果。

SL4A适用场景

  • RAD programming,即 rapid application development,快速原型开发,进行工程项目的可行性分析
  • 编写测试脚本,通常代码量都比较小,可方便实现
  • 开发工具软件包,来实现一些快捷的功能

http://www.pythoncentral.io/python-for-android-the-scripting-layer-sl4a/

The Scripting Layer for Android, SL4A, is an open source application that allows programs written in a range of interpreted languages to run on Android. It also provides a high level API that allows these programs to interact with the Android device, making it easy to do stuff like accessing sensor data, sending an SMS, rendering user interfaces and so on.

It's is really easy to install and it works on any stock Android device, so you don't need to be root or anything like that.

Currently, the Scripting Layer supports Python, Perl, Ruby, Lua, BeanShell, JavaScript and Tcl. It also provides access to the Android system shell, which is actually just a minimal Linux shell.

You can find out more about the SL4A project from their website.

Why Android's SL4A is Different

There are a couple of other options for running Python on Android, and some are very good, but none offer the flexibility and features of the Scripting Layer. The alternatives really focus on enabling you to create and package a native app using some otherwise unsupported language, and some do that really well. For example, with Kivy, you can create an app in Python that'll run across many popular operating systems, desktop and smartphone, including Android. However because it's multi-platform, you don't have any way of directly accessing the Android API, so you can't use many of the features that make smartphones so interesting.

SL4A was designed around the Android OS: It requires Android to be useful, but allows much closer integration with the operating system.

With SL4A, you can still package your apps, and you can publish them to app stores like Play if you like, but it's just an option. One advantage to having packaging as an option, rather than as a purpose, is that most Python scripts, in practice, are not actually published through app stores anyway. They're meant to be used as regular Python programs. You'll normally just want to write a bit of code, save it, then run it, and keep iterating. Having to keep building the app is just tedious.

With the Scripting Layer, you can start hacking away like you would on any other system, just editing and executing files.

This series focuses on Python, and Python is the most popular and well supported language on SL4A, but the other languages have features which can be really useful. For example, BeanShell, which is a very high level language that compiles to Java, has the ability to side-step the Scripting Layer API and access the Android Java API directly. The Scripting Layer's Ruby and JavaScript interpreters, JRuby and Rhino, also run on the JVM, so these languages can do this too. It's nice to have an environment with these kinds of features available.

The Plunge: Getting Things Set Up

SL4A is really easy to install. The application is distributed as an APK, the standard format for Android apps, so it can be installed in the same way. However, before installing apps from 'unknown sources', you need to allow that on your device. If you haven't already, open your device's main Settings menu, open the Security menu, then "allow installation of non-Market apps" by checking the Unknown Sources option. Now you're ready to install the Scripting Layer.

The Scripting Layer (SL4A)

If you go to the SL4A project's home page, you can download a copy of the Scripting Layer APK to your device by just scanning the barcode on that page and confirming the download when prompted. If you don't have a barcode scanner on your device, there's always a bunch of gratis scanners available from any app store.

Once you've downloaded the APK, you should be able to install it from directly within your device's notifications panel, where the download will appear. You device may differ slightly, but it will be obvious how you install the APK, as long as you've enabled installation from unknown sources.

Once you've installed the Scripting Layer, you'll be able to open it, create and edit little shell scripts with the built-in editor and run them. It very easy to get started.

Whenever you first open SL4A, you'll see the contents of your scripts directory, which lives at/sdcard/sl4a/scripts. This is where you'll normally put your own scripts for easy access. You can create directories in here to help keep things sane if you want to build more complex applications, or just to organise simple scripts if you have a lot of them.

The SL4A Interpreters

SL4A only includes the shell as standard, but installing other interpreters is easy to do from within the app. If you open SL4A, then hit your devices main menu button, and the SL4A menu will pop up. If you press View, you'll see a menu with three options, Interpreters, Triggers and Logcat. Selecting Interpreters will move your view from the scripts directory to the interpreters list. Hitting the device's main menu button in this view will open a new menu with an option to add interpreters. From here you can select which interpreter you want to install, which will open your browser and download the APK for that interpreter. Install this APK in the same way that you installed SL4A.

Each interpreter exists as a separate Android app and will appear as an app in the device's menus. The Python Interpreter's app is called Python for Android, or just PY4A. Each interpreter's app has, at least, the ability to install or uninstall the interpreter. PY4A can also manage .egg files, which gives you an easy way to install Python C modules on the Scripting Layer, where you can otherwise only use pure Python. Any Python C modules must first be cross-compiled for ARM cores, which is an involved process, but some useful packages are available pre-compiled from the Python for Android project pages, along with instructions on how to compile others.

If you haven't already, open the Python for Android app and hit the Install button. Your Scripting Layer now supports Python.

The Python Library on SL4A

If you'd like to add new modules to extend the device's Python library, you have to do things a bit differently to what you're probably used to. Without root, you can't directly modify the system files, so the Scripting Layer has its own directory for packages that lives on your sdcard at/sdcard/com.googlecode.pythonforandroid/extras/python. You can always import from this directory and similar directories exist for each language you have installed.

The Python extras directory comes preloaded with a ton of useful modules, and you can chuck any pure Python modules you like in here.

If you add a module to the extras directory, make sure it's the importable Python module itself. When you grab a library from a repository, it'll normally be structured so that the app's root directory contains a bunch of stuff like READMEs, docs, tests and setup files, as well as the actual module you need, which may be a file or a directory. Remember, it must be pure Python and you will have to resolve any dependencies.

Note: If you don't have a decent file browser on your device, grab one immediately. I normally use ES File Explorer which is free on Play. If you want to get seriously into hacking Androids, it's a good idea to install the Android SDK on a dev box sooner rather than later too. It includes tools that make light work of some common tasks. It's a fairly complex bit of kit, but guides for each OS are readily available online, and you'll need it to package apps.

Android's SL4A Hello World: Your First Script and the API

You have a bunch of example scripts installed automatically in the scripts directory whenever you add a new interpreter. These are a good place to start looking for code examples, and you can safely delete them once you're done.

Using SL4A in Python is really simple. Most scripts start with the following two lines:

......

Android 脚本设计之 SL4A的更多相关文章

  1. 详解 “Android UI”设计官方教程

    我们曾经给大家一个<MeeGo移动终端设备开发UI设计基础教程>,同时很多朋友都在寻找Android UI开发的教程,我们从Android的官方开发者博客找了一份幻灯片,介绍了一些Andr ...

  2. Robot Framework测试框架用例脚本设计方法

    Robot Framework介绍 Robot Framework是一个通用的关键字驱动自动化测试框架.测试用例以HTML,纯文本或TSV(制表符分隔的一系列值)文件存储.通过测试库中实现的关键字驱动 ...

  3. (转载)Android UI设计之AlertDialog弹窗控件

    Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...

  4. android 界面设计基本知识Ⅲ

    本章继续讲述在android界面设计中相关的知识点.介绍内容包括BroadcastReceiver(广播),Service(服务),Widget(小部件),WebView(网页加载控件). 1.Bro ...

  5. 安卓app设计规范整理和Android APP设计篇(转)

    随着安卓智能手机不停的更新换代.安卓手机系统越来越完美,屏幕尺寸也越来越大啦!比如最近小米的miui 6的发布和魅族手机系统的更新等等. 以小米MIUI6的安卓手机来说,MIUI6进行了全新设计,坚持 ...

  6. Android动画设计源码地址

    Android动画设计源码地址 http://blog.csdn.net/shanghaibao123/article/details/45223825

  7. LoadRunner脚本设计、场景设计和结果分析

    本次笔记主要记录LoadRunner脚本设计.场景设计和结果分析   1. 脚本设计       录制模式            手工模式:插入步骤.手动编写       1.1  脚本增强:     ...

  8. 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面

    [Android UI设计与开发]第05期:引导界面(五)实现应用程序只启动一次引导界面 jingqing 发表于 2013-7-11 14:42:02 浏览(229501) 这篇文章算是对整个引导界 ...

  9. Eclipse+Selenium自动化测试脚本设计V1.0

    Eclipse+Selenium自动化测试脚本设计V1.0 http://www.docin.com/p-803032251.html

随机推荐

  1. tensorflow中的boolean_mask

    将mask中所有为true的抽取出来,放到一起,这里从n维降到1维度 tensor = [[1, 2], [3, 4], [5, 6]] import numpy as np mask=np.arra ...

  2. Linux入门(二)Shell基本命令

    上一篇讲了普通用户切换到root用户,今天补充一点,对于Debian和Ubuntu用户,安装时候只有一个普通用户注册,在需要root权限时,我们可以在普通用户模式下输入sudo这个命令运行某些相关特权 ...

  3. [java笔记]常用的设计模式

    1.单例设计模式 单例设计模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 1)构造方法私有化 2)声明一个本类对象 3)给外部提供一个静态方法获取对象实例 例如: class Singl ...

  4. AdvStringGrid 垂直居中 、水平居中

    官网faq,解答: 结果:

  5. opencv(4)实现数据增加小工具

    数据增加(data augmentation),作为一种深度学习中的常用手段,数据增加对模型的泛化性和准确性都有帮助.数据增加的具体使用方式一般有两种,一种是实时增加,比如在Caffe中加入数据扰动层 ...

  6. sqlserver中的循环遍历(普通循环和游标循环)(转载)

    sql 经常用到循环,下面介绍一下普通循环和游标循环 1.首先需要一个测试表数据Student

  7. Django配置https协议

    本博客来自https://blog.csdn.net/huplion/article/details/52892901 1.首先我们需要得到一张证书文件 参考:WINDOWS系统下创建自签名SSL证书 ...

  8. javascript输入验证数字方法,适合充值时输入正整数验证

    说明:用于验证正整数的输入,不允许输入其他字符. html: <input type="text" id="sell_jobNum" name=" ...

  9. 【51nod】1251 Fox序列的数量

    题解 容斥题 我们枚举出现次数最多的数出现了K次 然后我们需要计算的序列是所有数字出现个数都不超过K - 1次 我们枚举不合法的数字的数目j,说明这个排列里除了我们固定出现K次的数至少有j个数是不合法 ...

  10. git status中文文件名编码问题解决

    在默认设置下,中文文件名在工作区状态输出,中文名不能正确显示,而是显示为八进制的字符编码. 通过将git配置变量 core.quotepath 设置为false,就可以解决中文文件名称在这些Git命令 ...