Skip to content

Instantly share code, notes, and snippets.

@marshall
Created August 16, 2010 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshall/527107 to your computer and use it in GitHub Desktop.
Save marshall/527107 to your computer and use it in GitHub Desktop.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := v8hi
LOCAL_CFLAGS := -I/Users/marshall/Source/v8/include -g
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -ldl -llog -L$(TARGET_OUT)
LOCAL_SRC_FILES := v8hi.cpp
PRIVATE_WHOLE_STATIC_LIBRARIES := $(TARGET_OUT)/libv8.a
$(TARGET_OUT)/libv8.a:
cp $(LOCAL_PATH)/libv8.a $(TARGET_OUT)
include $(BUILD_SHARED_LIBRARY)
#!/bin/sh
# NDK r4
ANDROID_NDK=/home/marshall/Apps/android-ndk-r4
# android checkout dir on the android-sdk-2.1_r1 tag
TOP=/home/marshall/Source/android
ABI_BIN=$ANDROID_NDK/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin
CC=$ABI_BIN/arm-eabi-gcc
CXX=$ABI_BIN/arm-eabi-g++
RANLIB=$ABI_BIN/arm-eabi-ranlib
AR=$ABI_BIN/arm-eabi-ar
TOP=$TOP CC=$CC CXX=$CXX RANLIB=$RANLIB AR=$AR scons os=android arch=arm $@
#include <v8.h>
#include <string.h>
#include <jni.h>
#include <android/log.h>
#define LOG_TAG "V8Hi"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
extern "C" {
JNIEXPORT jstring JNICALL
Java_com_example_v8hi_V8Hi_evalJS( JNIEnv* env, jobject thiz, jstring str );
}
JNIEXPORT jstring JNICALL
Java_com_example_v8hi_V8Hi_evalJS( JNIEnv* env, jobject thiz, jstring str )
{
LOGD("in execJS");
v8::HandleScope handle_scope;
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
v8::Handle<v8::Context> context = v8::Context::New(NULL, global);
v8::Context::Scope context_scope(context);
const char* sourceStr = env->GetStringUTFChars(str, NULL);
LOGD("got source str: %s", sourceStr);
v8::Handle<v8::String> source = v8::String::New(sourceStr);
v8::Handle<v8::Script> script = v8::Script::Compile(source);
v8::Handle<v8::Value> result = script->Run();
env->ReleaseStringUTFChars(str, sourceStr);
v8::String::Utf8Value resultStr(result);
const char* cstr = ToCString(resultStr);
return env->NewStringUTF(cstr);
}
package com.example.v8hi;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import android.os.Bundle;
public class V8Hi extends Activity {
public static final String TAG = "V8HiActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Create a TextView and set its content. the text is retrieved by
* calling a native function.
*/
TextView tv = new TextView(this);
Log.d(TAG, "evalJS!");
tv.setText(evalJS("(function() { return 'hello from JS!'})();"));
setContentView(tv);
}
/*
* A native method that is implemented by the 'v8hi' native library,
* which is packaged with this application.
*/
public native String evalJS(String source);
static {
Log.d(TAG, "Loading v8 library..");
System.loadLibrary("v8hi");
Log.d(TAG, "Loaded.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment