Created
March 6, 2012 19:56
-
-
Save charroch/1988640 to your computer and use it in GitHub Desktop.
HelloWorld on dalvik
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hello | |
import java.lang.String | |
import android.net.Uri | |
import com.android.dx.dex.DexOptions | |
import com.android.dx.dex.file.DexFile | |
import dalvik.system.DexClassLoader | |
import java.io.{PrintWriter, FileInputStream, FileOutputStream, File} | |
import com.android.dx.dex.cf.{CfOptions, CfTranslator} | |
import java.io._ | |
object HelloWorld { | |
def main(args: Array[String]) { | |
println("Hello, world!") | |
val u = Uri.parse("content://test") | |
val f = new File("/tmp/dexfile.dex"); | |
println(" l " + f.exists()); | |
val a = new DexFile(new DexOptions); | |
val i = new FileInputStream("/tmp/remote/net/novoda/HelloWorldJava.class") | |
val array = Stream.continually(i.read).takeWhile(-1 !=).map(_.toByte).toArray | |
i.close() | |
val clazz = CfTranslator.translate("net/novoda/HelloWorldJava.class", array, new CfOptions, new DexOptions) | |
a.add(clazz) | |
val out = new FileOutputStream("/tmp/dexfile.dex") | |
val humanOutRaw = new FileOutputStream(f); | |
val humanOut = new OutputStreamWriter(scala.Console.out); | |
//val outArray = a.toDex(humanOut, true) | |
a.writeTo(humanOutRaw, humanOut, true); | |
out.close | |
val l = new DexClassLoader("/tmp/dexfile.dex", "/tmp/and/android-data", "/media/server/FOSS/android/head/out/host/linux-x86/lib", ClassLoader.getSystemClassLoader) | |
import scala.collection.JavaConversions._ | |
new dalvik.system.DexFile(f).entries().foreach(println) | |
val classs = new dalvik.system.DexFile(f).loadClass("net.novoda.HelloWorldJava", l) | |
val s = classs.newInstance //.asInstanceOf[{ def hello() }].hello() | |
println(s + " < ========== ") | |
s.getClass.getDeclaredMethods.foreach(println) | |
val aaaa = l.loadClass("net.novoda.HelloWorldJava") | |
//aaaa.getClass.getDeclaredMethods.foreach(println) | |
println(aaaa) | |
println(aaaa.getClass) | |
val foo = aaaa.newInstance.asInstanceOf[{ def hello() }] | |
foo.hello() | |
println(aaaa) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rm /tmp/and/android-data/dalvik-cache/*
rm hello.dex.jar
rm -r out/
mkdir out
/usr/local/scala-2.9.1/bin/scalac -cp /opt/android-sdk-linux_x86/platforms/android-15/android.jar:/opt/android-sdk-linux_x86/platform-tools/lib/dx.jar hello.scala
mv com out/
dx --dex --output=hello.dex.jar out
ANDROID_PRINTF_LOG=tag ANDROID_LOG_TAGS=*:i ANDROID_DATA=/tmp/and/android-data ANDROID_ROOT=/media/server/FOSS/android/head/out/host/linux-x86 LD_LIBRARY_PATH=/media/server/FOSS/android/head/out/host/linux-x86/lib DYLD_LIBRARY_PATH=/media/server/FOSS/android/head/out/host/linux-x86/lib /media/server/FOSS/android/head/out/host/linux-x86/bin/dalvikvm -classpath /media/server/FOSS/android/head/out/target/product/generic_x86/dex_bootjars/system/framework/framework.jar:/tmp/remote/scala-actors.jar:/tmp/remote/scala-collection.jar:/tmp/remote/scala-immutable.jar:/tmp/remote/scala-library.jar:/tmp/remote/scala-mutable.jar:/tmp/remote/hello.dex.jar -Duser.dir=/tmp/and/ -Djava.io.tmpdir=/tmp/and/ -Xbootclasspath::/tmp/remote/dx.dex.jar:/media/server/FOSS/android/head/out/host/linux-x86/framework/core-hostdex.jar:/media/server/FOSS/android/head/out/host/linux-x86/framework/bouncycastle-hostdex.jar:/media/server/FOSS/android/head/out/host/linux-x86/framework/apache-xml-hostdex.jar -Duser.language=en -Duser.region=US -Xverify:none -Xdexopt:none -Xcheck:jni -Xjnigreflimit:2000 com.hello.HelloWorld