Is it possible to run Android apps in JVM? - java

I am trying to run symbolic testing on Android apps to collect some information, for example, the execution tree. Thus I want to run it in JVM instead of the emulator because there are a lot of existing symbolic testing tools for Java applications.
I tried to run HelloAndroid which is a sample app outputting "Hello Android" on TextView by
java -cp ./ -cp $ANDROID_LIB/android.jar HelloAndroid.class
where HelloAndroid.class is compiled Java class before converting into .dex. But JVM is keeping complaining that
Exception in thread "main" java.lang.NoClassDefFoundError: HelloAndroid/class
Caused by: java.lang.ClassNotFoundException: HelloAndroid.class
I am confused because I've already specify the HelloAndroid class. And there is no complex statements or calls into Android library in the source code:
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
I am new to Android and am struggling to make this small app to execute in JVM. So would you please give me some suggestion? I am wondering if I am on the right way, I mean try to execute simple apps in JVM? Thanks!

Android apps aren't compiled in the same that Java apps are. Android apps are complied down to .dex files, where as regular Java apps are complied to a jar file; neither are anywhere close to being compatible with each other.
The closest currently available thing to running Android apps on a PC is the Android emulator.
So to answer your question, no it is not possible to run Android apps in Oracle's JVM.

Related

Android how to run one small app in another

I have recently developed an app in which I wanna use a small app I have developed before, let's say in one view like below:
public class MyMain extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showmysmallapp(); //how?
}
}
I have saved the small app as aar file and imported it to my new app and added in my build.gradle dependency also:
dependencies {
compile project(":mysmallapp")
...
}
How can I run the entire small app when a view of my main app starts pls?
or should I have added the library in another way?
Much thanks ahead.
I guess you want to run an app as a plugin.
You can use one of these libraries:
VirtualApp
VirtualApp is an open platform for Android that allows you to create a
Virtual Space, you can install and run apk inside. Beyond that,
VirtualApp is also a Plugin Framework, the plugins running on
VirtualApp does not require any constraints. VirtualApp does not
require root, it is running on the local process
DL: Apk (Readme in chinese).
Android PluginManager
PluginManager is used to manage android applications like eclipse
plugins. you can start an activity from an uninstalled apk placed in
sdcard,just like it has installed or registed in the application's
AndroidManifest.xml.

How to use the generated .so library in another Android Project?

I’ve followed http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/ and it works well! However, I want to use the generated .so library in a new Android application, and I simply don’t know how to do this... I’ve been struggling for days and if any step-by-step tutorial can be shared that would be helpful!
This is the Android Project MyApp which I used to generate the .so files:
MainActivity :
Java Class : MyNDK
header file: com_demo_ble_myapp_MyNDK.h
Cpp file: MyLibrary
And this is the structure of my new Android project useSOLib, I simply copy all the so files from MyApp\app\src\main\libs to useSoLib\app\src\main\jniLibs
And this is MainActivity in useSoLib:
I can Build-> Make Project successfully, but when I run it on the device, the app shows "Unfortunately, useSoLib has stopped." and crushed. I know I miss some steps here, but I'm new to Android Studio so I have no clue where I should start with... thank you in advance for any suggestions! :)
You should import MyNDK and use its instance. JNI library is related to Java class. Change the code:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MyTag", new MyNDK().getMyString());
}
}
You can either export the MyNDK class as a jar package or create an aar bundle. Then import the Java class into your new project with JNI library.
The post How to Build *.so Library Files into AAR Bundle in Android Studio may help you.

FATAL EXCEPTION: main java.lang.NoClassDefFoundError: external library.jar.class Android studio

I am developing an android app which process speech and I have speech basic project (dependency for android project) ready on JAVA so I compiled JAVA project in eclipse in JAVA 7 compiler and exported that java project as a runnable jar.
I put this jar into my android studio project's libs folder and by right clicking selected AS A library, I got build successful message. But when I try run the android project it gives me error saying,
FATAL EXCEPTION: main Process: in.automator.automator, PID: 4242
java.lang.NoClassDefFoundError: jar_filename.Storage.class_in_jar_file
but the said class is there in the jar file, the only doubtful thing is the mentioned class file looks something like this
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import marf.util.InvalidSampleFormatException;
public class MARFAudioFileFormat extends AudioFileFormat {
....
...
....
}
It refers javax.sound.sampled, so possibly that might causing the problem.
I tried searching on the google for the problem but didn't got the solution which can resolve the issue. I tried everything.
I am using JRE 7 in android studio and exporting java project in Compiler & itself.
How to resolve this error?
Thanks in advance.
You are correct, the problem lies in the fact that the library you're trying to depend on, in turn depends on the javax package hierarchy. Android does not provide javax, so those classes don't exist at runtime (even though they do exist in your computer's JDK, so your compiler doesn't complain - it's basically the same as not having a particular DLL installed that a program on your PC needs).
The NoClassDefFoundError is being thrown on the class that first references the invalid class dependencies, which is probably what's confusing. That class may indeed actually exist in your jar, but it requires classes that don't exist in order to complete its definition - so in effect, your class is not fully defined.
The only way around this is to figure out a way to do whatever you're after, without the javax namespace. I have heard of a few attempts to try to port javax.* into Android, but they never end well - many of the javax classes end up boiling down to native calls, which also won't exist on Android.

Bizarre NoClassDefFoundError when running in Android

I've tried looking at all the other NoClassDefFoundError threads on here and elsewhere, but they all seem to be a different problem.
My activity refers to a custom FragmentPagerAdapter - let's call it MyFragmentPagerAdapter.
The Activity refers to it with:
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getFragmentManager());
ViewPager viewPager = (ViewPager) findViewById(R.id.form_pager);
viewPager.setAdapter(pagerAdapter);
This all seems to compile and build perfectly well in Eclipse. Eclipse can certainly find all the classes - it auto filled the import statement for me.
However, when I try and run the class in Android (emulator or device) I get a NoClassDefFoundError for MyFragmentPagerAdapter and (of course) the app crashes.
I've tried deleting the APK on the device and re-installing in case it was a deployment issue, but it didn't help.
This isn't to do with some 3rd party library - it's my own class that it fails to find!
I have also tried "cleaning" the Project.
I have extracted the apk and inspected the output of
dexdump classes.dex | grep PagerAdapter
and there are references to my class in there, so I can only assume the compiled class is getting itnot the apk - which makes me even more bemused.
NoClassDef refers to classes that are referred by your class (in this case "MyFragmentPagerAdapter") not to the class itself (that's the ClassNotFoundException).
Check the import you have in the class "MyFragmentPagerAdapter" and be sure that they are all in the classpath of your android application i.e. by checking the libs folder and (eventually) other android libraries you are referring to in your project.
Hope this helps.

How to run the main method of an Android Activity class?

I'd would like to examine a private method of an Android Activity by executing it within the Activity's public static void main(String[] args) method which I created.
I use Eclipse ADT and my naive approach of running the Activity as a Java application resulted in:
A fatal error has been detected by the Java Runtime Environment:
Internal Error (classFileParser.cpp:3174), pid=2936, tid=2980
Error: ShouldNotReachHere()
So I looked at the Run Configuration and found out that Android 3.1 is the sole entry in the Bootstrap Entries section of Classpath. I managed to configure the Build Path of the project so that the JRE is in the Bootstrap Entries too. Then I removed the Android 3.1 entry and added android.jar to User Entries.
The result of executing the Run Configuration is a RuntimeException:
Exception in thread "main" java.lang.RuntimeException: Stub!
at android.content.Context.(Context.java:4)
An alternative of executing some tests would be to fire up a JUnit test. But in the case of a private method this is cumbersome.
Is there a way to successfully run the main method of an Android Activity class?
There is another option for the problem at hand, if the private method - which should be examined through the execution of a main method - can be extracted to another class. This of course means that the method suddenly became at least protected.
But if the method is definded within a class that does not derive from android.app.Activity a main method can be defined. You only have to adjust the Run Configuration in Eclipse.
Run the class with Run as Java Application. This generates a new Run Configuration named like the class
Edit the newly creaded Run Configuration
Delete the Android library from Bootstrap Entries in the Classpath tab
Add JRE 6 or so to the build path of the project
Add this JRE to the Bootstrap Entries in the Classpath tab
Add android.jar, which resides in the platforms directory of the Android SDK in User Entries in the Classpath tab
Activity is very important from Android's point of view, Activity's lifecycle are collections of few methods which are handled by OS through out the activities' life.
public void onCreate(Bundle savedBunldeInstance)
is called as soon as the App is launched creating the activity. This is the entry point of an application in android. You must have the emulator or physical device for running Android app, Here the file is compiled 2 times, once by java compiler then by dalvik compiler.
DVM (Dalvik Virtual Machine) is located on the Android device (EMULATOR OR PHYSICAL), this is the reason why we need emulator or physical device to run it..and onCreate is the entry point for it.
On your request I am editing this post with an additional piece of information. The lines below are abstracted from http://developer.android.com/
Android applications don't have a single entry point.
(there's no main() function, for example).
for further details click this :
http://developer.android.com/guide/topics/fundamentals.html
The main method isn't the entry point in Android like in Java. You wanna override the
public void onCreate(Bundle savedBunldeInstance) method.

Categories

Resources