2

So I've been doing some research on how to run java code on an android device with dalvikvm. I can successfully run a java program compiled to a .dex file using the command dalvikvm -cp RunTest.dex RunTest, where I have a RunTest class which has my main function. I've been pushing RunTest.dex and libeit_test.so (my native library) to /data/local/tmp, where I call the above command. My question is: Is there a way to package RunTest.dex and libeit_test.so into an apk, and run my program with the apk with something like dalvikvm -cp RunTest.apk RunTest? I have been packaging my apk with ./dx --dex output=RunTest.apk RunTest.dex libeit_test.so, but the program fails to run when I am calling dalvikvm -cp RunTest.apk RunTest.

java.lang.NoClassDefFoundError: RunTest
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "RunTest" on path: DexPathList[[zip file "RunTest.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

The whole point of all of this is to push just one file to a device and run my test program. I'm trying to keep things as simple and minimalistic as possible.

Otto45
  • 555
  • 2
  • 6
  • 20
  • 1
    There probably is a way to get the vm to read classes out of a zipfile/apk, yes (though there might be a problem with the odex?) but the runtime linker won't take a .so file contained there - you will have to use your java code to copy it out to the file system and mark it executable before you can load that into your process and make jni calls to it. – Chris Stratton Aug 06 '14 at 16:26
  • Ok, that's what I needed to know about the .so file. I'll just have to use a .dex file then like I have been. Thanks for the quick response – Otto45 Aug 06 '14 at 17:32
  • 1
    That's not exactly what I was implying. If you can get dalvikvm to use a dex file contained in a zip or apk, then you can have your java code copy a .so which is also contained there out into the filesystem and mark it executable, then System.load() [not loadLibrary()] it and call into it. But yes, this could be more trouble than host-side automation to push two files instead of one. Actually the `adb sync` command will push/update an entire directory from host to device. – Chris Stratton Aug 06 '14 at 17:34
  • Thanks. I was implying that it would be easier to push the two files rather than messing with the .so inside the apk, I should've been more clear – Otto45 Aug 06 '14 at 17:45
  • Try `export LD_LIBRARY_PATH=/data/local/tmp` with the .so file there, and see if that helps. (I can't remember offhand how Dalvik sets up the native library path, and I'm away from a dev device setup.) – fadden Aug 07 '14 at 14:01
  • I know that works, my question involved it being packaged within the apk – Otto45 Aug 07 '14 at 15:58

2 Answers2

0

The command line you described is used to build the DEX file (including all .JAR and .SO files in it), once you have your code and dependencies properly DEXed, you need to build the APK using the Android SDK's AAPT tool:

aapt \
  package \
  -f \
  -M AndroidManifest.xml \
  -S res \
  -I %ANDROID_HOME%/platforms/android-19/android.jar \
  -F my-app.ap_ \
  --debug-mode  
Roberto Andrade
  • 1,793
  • 1
  • 21
  • 27
-1

If you have any unused jars remove that & clean and run

ViJay
  • 189
  • 7