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.
Related
I spent the last three days trying to read an .aar in Android Studio and I just can't figure it out.
My project view looks like this:
--- AnApplication
----------------------.gradle
-----------------------.idea
----------------------- app
-------------------------------- build
-------------------------------- libs
---------------------------------------- cube5-debug.aar
(...)
I want to access a class inside cube5-debug.aar from my main activity.
I know the class is there in the .aar file.
I don't know the precise import line I must write at the start of my main.
I have tried import com.company_cube.cube5 (the package of the project where I generated my .aar) and many other variants.
I don't know if Android Studio is recognizing the file as a library. However, the files sync with the gradle successfully.
(For context, the project where I generated the .aar was made by Unity, by exporting a Unity project into an Android one, and then generating the .aar from that one; the goal was to use this .aar file as a library in a second Android Studio project - the one I am talking about in this post)
I am patiently waiting for any help you can give me. Thanks
EDIT: To import the .aar, I followed the images of this small post:
Importing .aar in android Studio
And solved the error the post reports by putting the .aar in the libs folder like Daniel Nugent said. However, when I try to write, in the Main:
Intent intent = new Intent(getApplicationContext(),UnityPlayerActivity.class);
AS doesn't recognize UnitPlayerActivity. This class is inside the .aar file supposedly. Before making the aar in my first AS project, it was here:
cube5->libs->unity-classes.jar->com.unity3d.player->UnityPlayerActivity
When I open the .aar with Winrar, and open unity-classes.jar with a text editor, I see a almost everything as encoded symbols.
I'm not sure it is normal. Any idea?
EDIT 2: I think I know where the error is: when I import my .aar, I get an IDE fatal error. I don't know how to solve it, so if you can, please check the post I made about it:
IDE fatal error on importing .aar
For android Studio 2.3.3 you can use
repositories {
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'cube5-debug', ext: 'aar')
}
in the app level build.gradle. I did't check it for AS 3.0.
Add .aar file to your app by doing:
in project build.gradle
allprojects {
repositories {
flatDir {
dirs 'libs'
}
}
}
in app build.gradle
dependencies {
compile(name:'cube5-debug', ext:'aar')
}
So I managed to solve it. The .aar file I talked about was exported with AS, after I imported an Unity project into AS. I did this in order to use the Unity program inside AS. However, this gives error for some versions of Unity and AS. I installed Unity 5.5.0f3 and an older version of AS, and everything worked fine.
Note1: if you aim to install that Unity version, you will be prompted with a requirement of some windows 10 tool, which you must get (I cannot remember the name). I did it by downloading windows 10 install and installing only that tool, which comes with the package.
Note2: I made the program from Unity work inside AS but only because it was a simple program. I later wanted to do an Augmented Reality App in Unity, and use it in AS, but it cannot be done apparently. Evertyting works fine until I import the .aar into AS, and a class in that .aar library gets unresolved ("Video player”). This should have to do with the AR camera class, which AS does not recognize. I ended up creating a AR app with Unity, another app in AS, and from the AS app, calling the Unity app with two lines of code, whenever necessary. To return to the AS app, now in background, I simple click the back button, which is recognized by the Unity app (if you make code in Unity for it) and makes it destroy itself, returning to the AS app.
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.
I am not familiar with using Android Studio. Earlier I used Eclipse. Could you explain me what is gradle in Android studio. I have problem with Android Studio. What confused me is that it uses the same android sdk as eclipse but when I create a new project weird things happens:
There is required :com.android.support:appcompat-v7:+ (what it is ? eclipse does not require it)
My MainActivity extends ActionBarActivity instead of Activity why is that ?
Its a support library called appcompat. You need to reference that if you need actionbar below api level 11 (Whether its android studio or eclipse).
If you need actionbar below api level 11 your Activity needs to extend ActionBarActivity.
If you are targetting api level 11 and above then you don't need to extend ActionBarActivity and reference AppCompat. You can simply extend Activity and you will have actionabr by default.
Check Adding the Action Bar in the below link.
http://developer.android.com/guide/topics/ui/actionbar.html
Check for Adding libraries with resources using Android Studio #
https://developer.android.com/tools/support-library/setup.html
Actiobbar is introduced in api level 11. com.android.support:appcompat-v7:+ is a support library which allows you to have an ActionBar in your app for devices running on Android 3.0 or below.
Android Studio default project includes it automatically in dependencies and extends ActionbarActivity instead of Activity in order to use it.
You can check the same in your module's build.gradle file, you will find something like this under your dependencies block.
compile 'com.android.support:appcompat-v7:+'
There are two possible solutions to get rid of this
Remove above line from dependencies block and extend Activity instead of ActionBarActivity in your Activity classes.
Download support repository from Android SDK Manager.
Support Repository and Google Repository shown in SDK manager are not required for Eclipse but in order use Google Play Services and Support libraries in Android Stodio you have to download them.
EDIT(android plugin not found error, asked in comments below ) :
Make sure you have this either in your root level build.gradle file or at the top of module's build.gradle file to let build system know where your gradle pguin is and what is the version :
(before apply plugin: 'android')
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
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.
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.