• No results found

Setting up the Java part

In document 50 Android Hacks (Page 141-146)

Interacting with other languages

33.3 Setting up the Java part

The Java part will hold an Activity class and a TextFormatter class with the native method. The Activity is the following:

public class MainActivity extends Activity { private TextView mTextView;

@Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mTextView = (TextView) findViewById(R.id.text);

Set a text to TextView using TextFormatter’s formatString method

String text = TextFormatter.formatString("Text from Java"); mTextView.setText(text);

} }

The following is the TextFormatter Java code: public class TextFormatter {

public static native String formatString(String text);

Native call declaration

static {

113

Using Scala inside Android

System.loadLibrary("objc"); System.loadLibrary("cf"); System.loadLibrary("foundation"); System.loadLibrary("textformatter"); System.loadLibrary("main"); } }

The most important part of this piece of code is understanding what libraries will get loaded inside the static block

B

. They include the following:

 macemu: Contains emulation of some APIs used by objc4 and CoreFoundation libraries

 objc: objc4 runtime  cf: CoreFoundation classes

 foundation: The Foundation library  textformatter: Our TextFormatter library  main: Our main library

When you run the application, you’ll see a TextView populated with a mixture of texts from the Java and Objective-C worlds.

33.4

The bottom line

Using Itoa to port Objective-C applications to Android might be a good idea, depend- ing on the type of code you need to port. I’ve used it to port business logic from iOS to Android and also to port cocos2d-iphone games to Android. My recommendation is that you give it a try and decide if it would work for you.

33.5

External links

www.nasatrainedmonkeys.com/portfolio/shaman-doctor/ www.cocos2d-iphone.org/ www.cocos2d-x.org/ www.itoaproject.com/ https://github.com/DmitrySkiba/itoa-ndk/wiki/Variables

Hack 34

Using Scala inside Android

Android v1.6+

If you’ve never heard of Scala, it’s a multiparadigm programming language designed to integrate features of object-oriented programming and functional programming. Let’s look at some of the benefits of using Scala, instead of Java, in Android to create a project:

 Less verbose than Java.  It can use existing Java code.  Closures.

 Dealing with threads is easier than in Java.

Discussing the benefits of Scala over Java is beyond the scope of this book, but let’s look at what’s possible with Scala. In this hack, we’ll create a two-Activity application. One will be coded in Java and the other in Scala. This is a basic example we’ll use to understand how to compile an Android application with Scala code.

As you might know, Android builds code by compiling your Java classes to byte- code, and afterward that bytecode is converted to dex. To make Scala code work inside Android, we need a tool that does all of this:

 Converts Scala code to bytecode

 Processes the Scala standard library to minimize the app size  Processes Java code

 Creates an APK

Believe it or not, there are a lot of ways of getting this done. From my personal point of view, the best tool is SBT with its Android plugin.

What is SBT? SBT stands for Simple Build Tool. It’s an open source build tool for Scala. Among its benefits:

 The project structure is similar to Maven.

 It manages dependencies using existing Maven and/or Ivy package repositories.

 It allows you to mix Scala and Java code.

What does the SBT Android plugin pro- vide? The Android plugin is a script for creating a new Android project that SBT

can compile. It also has several handy SBT

targets for doing things such as packaging your app for the market and deploying to your device.

If we create a new Android applica- tion using the SBT Android plugin, we’ll get a project directory structure similar to figure 34.1.

Since SBT allows Java code as well, we’ll add our Java code inside src/main/ java. Remember that, though Scala doesn’t need to place files on a certain folder depending of the defined pack-

115

Using Scala inside Android

com.manning .androidhacks.hack034 as our package, so we need to create a directory structure that respects that. The correct project structure for adding a second Java Activity can be seen in figure 34.2.

Figure 34.2 Project structure with Java code

Let’s look at the Activity done in Java and how it connects to the Scala Activity. Here’s the code:

public class MainActivityJava extends Activity { @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.main); }

public void buttonClick(View v) {

startActivity(new Intent(this, ScalaActivity.class));

B

Start Activity coded in Scala

} }

Do we need to do anything different to call the Activity done in Scala? No, there isn’t anything special. We start the Scala Activity as any ordinary Activity

B

.

class ScalaActivity extends Activity {

override def onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState)

setContentView(new TextView(this) {

B

Anonymous subclass of TextView is set as content view

setText("Activity coded in Scala ") })

} }

You can see that the Scala Activity’s code is 100% Scala. The Scala coded there comes from the demo application created by the SBT Android plugin. Take a closer look at how the content view is set

B

. That line creates an anonymous subclass of the TextView, and with the help of an initializer block it calls the setText() method.

To run the application, we can launch SBT and execute the following:  android:package-debug

 android:start-device

Unfortunately, creating an APK takes a while. This two-Activity application takes me about one full minute to compile. You should know that this isn’t Scala’s fault. What takes so long is the ProGuard pass that goes through the Scala library and removes any unused part of it. To solve this issue, some developers add the Scala libraries to their developing device. There’s even an Android application that installs Scala on your device if it’s rooted.

34.1

The bottom line

Scala is gaining a lot of momentum in the Java world, and it’s also attracting interest in the community of Android developers. Learning a new language might feel time- consuming, but Scala is something that every Java developer should try.

34.2

External links

http://www.scala-lang.org/ http://en.wikipedia.org/wiki/Simple_Build_Tool https://github.com/jberkel/android-plugin http://nevercertain.com/2011/02/03/scala-android-intellij-win-part-1-prerequisites.html https://github.com/scala-android-libs/scala-android-libs

117

In document 50 Android Hacks (Page 141-146)