Run apk on java

Can Android Studio be used to run standard Java projects?

For those times when you want to isolate the Java and give it a quick test.. Can you run non-Android Java projects in Android studio as in Eclipse?

Android studio is based upon intelliJ. So sure, it surely able to run «standard» java code. But why do you need it ?

@vmironov That’s essentially what they’re doing. The Android features are an IntelliJ plugin, they just package it together for Android Studio. I haven’t used Android Studio in particular, but I’ve been using IntelliJ for a while, and you should have no issue running standard Java projects within it.

@kcoppock, yep, but this plugin cann’t be added to a «usual» IntelliJ IDEA (I have a purchased Ultimate version and I don’t really want to switch back to Community edition). The Good news is that JetBrains has announced that it will be possible to have Android Studio’s features in Intellij IDEA 13

Is there a simple way to get this to work with Gradle yet? I keep crashing because it cannot see my gradle libraries at runtime.

13 Answers 13

Tested on Android Studio 0.8.6 — 3.5

Using this method you can have Java modules and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects.

  1. Open your Android project in Android Studio. If you do not have one, create one.
  2. Click File > New Module. Select Java Library and click Next.
  3. Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project.
  4. Add your code to the Java module you’ve just created.
  5. Click on the drop down to the left of the run button. Click Edit Configurations.
  6. In the new window, click on the plus sign at the top left of the window and select Application
  7. A new application configuration should appear, enter in the details such as your main class and classpath of your module.
  8. Click OK.
Читайте также:  Java enum for example

Now if you click run, this should compile and run your Java module.

If you get the error Error: Could not find or load main class. , just enter your main class (as you’ve done in step 7) again even if the field is already filled in. Click Apply and then click Ok.

My usage case: My Android app relies on some precomputed files to function. These precomputed files are generated by some Java code. Since these two things go hand in hand, it makes the most sense to have both of these modules in the same project.

NEW — How to enable Kotlin in your standalone project

If you want to enable Kotlin inside your standalone project, do the following.

    Continuing from the last step above, add the following code to your project level build.gradle (lines to add are denoted by >>>):

buildscript < >>> ext.kotlin_version = '1.2.51' repositories < google() jcenter() >dependencies < classpath 'com.android.tools.build:gradle:3.1.3' >>> classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files > > . 
apply plugin: 'java-library' >>> apply plugin: 'kotlin' dependencies < implementation fileTree(dir: 'libs', include: ['*.jar']) >>> implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" >>> runtimeClasspath files(compileKotlin.destinationDir) > . 

I can confirm this works in Android Studio 0.8.14. I used a basic Hello World project, and added a Custom class containing a main method. I deleted the MainActivity.java file. At step 2, I needed to disclose the More Modules pane (bottom left of the Create New Module window) and scroll down to find the Java Library item. At step 6, I named my application, selected Custom as my main class, and app for the classpath of the module. NOTE: If you delete the app module, as per @simonp’s solution, your application will run, but with errors.

There should be another step after step 2 : add code to the class, or at least make it a Hello-World code: docs.oracle.com/javase/tutorial/getStarted/application

EDIT: many moon after this question was asked, yes, now apparently you can.

No, but it’s based on Intellij IDEA.

The community version of that is free for download but it doesn’t support most things requiring an external database or application server. The line for Java is pretty much that JavaSE code can happily use Community.

If you want that (or are using JavaEE) then you either need the Ultimate version, which isn’t free, or the EAP of the next version which is usually good for a month until they release another.

Basically it works like this

Android Studio is just Android the Android Stuff from IDEA 13 Community.

. which will be free, and is anything from IDEA 13 Ultimate.

. that doesn’t require a database or app server.

IDEA 12 Community doesn’t have the ability to import the gradilized project and it won’t, so while you can do Android development in it now (I do), don’t expect it to have the same features as Android Studio. There’s a lot of good new Android stuff in it, that’s going into 13.

Happy to help! I personally love IDEA and loathe Eclipse. This is the first time I’ve gone to playing with the EAP because I’m clearly going to need compatibility with everyone migrating to Android Studio.

As of AS 0.5.8, this is no longer correct. You can definitely run standard Java SE code in AS. At least I have success importing a Swing project, developing, and running it. Unfortunately, you cannot create a «regular» Java project as of this writing, at least not that I know of.

Please delete/edit this answer since it is no longer correct. Android Studio can now build Java modules and run them in the local JRE.

So it’s better to use Android-Studio for android stuff, and IntelliJ for Java and the rest? What about SWT support on IntelliJ? Do you know of a nice UI designer for it? I need it mostly for preview of the UI .

Easy way to run a java program in Android Studio would be,

  1. Create a java Class says «Test.java» in Android Studio.
  2. Write your code eg, a Hello World program to test.
  3. Right-click on the Java class and:
  • select the option Run ‘Test.main()’

There you have your Java code running below.

This worked — thanks! In my case I was having issues with the gradle build crashing, when first trying to run this. So I did a Android Studio > File > Invalidate Caches / Restart and then the issue was resolved

@Shruti It takes a while to compile the first time after switching from running the app to running the test.java. Do you know if the executable it’s creating is added to the APK size at all?

It does not work for me. I get this: A problem occurred configuring project ‘:app’. > Could not create task ‘:app:MyClass.main()’. > SourceSet with name ‘main’ not found.

With Android Studio 0.6.1+ (and possibly earlier) you can easily develop standard Java (non-Android) apps.

This method has been tested on 0.8.2:

Start by creating a vanilla Android Phone app, using File > New Project. Then add a Java Library module to hold your Java Application code. (Choose ‘Java Library’ even if you’re building an application). You’ll find you can build and run Java apps with main() methods, Swing apps etc.

You’ll want to delete the auto-generated Android «app» module, which you’re not using. Go to File -> Project Structure, and delete it (select the «app» module in the box on the left, and click the ‘minus’ icon above the box). Now when you reopen File -> Project Structure -> Project, you’ll see options for selecting the project SDK and language level, plus a bunch of other options that were previously hidden. You can go ahead and delete the «app» module from the disk.

In 0.6.1 you could avoid creating the android module in the first place:

Go to File > New Project. Fill in your application name. On the «form factors» selection page, where you state your minimum Android SDK, deselect the Mobile checkbox, and proceed with creating your project.

Once the project is created, go to File -> Project Structure -> Project, and set your JDK as the «Project SDK». Add a Java Library module to hold your application code as above.

Where can I find the build then? If I click Build->Make Project or Build->Compile ‘PureJava.java’ android studio says «Compilation completed successfully». But I can’t find any build files. I deleted the app module as well as the app folder.

@MLProgrammer-CiM yes it is possible. It has happened to me that when debugging, the IDE stalls at some point in the «updating indices» phase, I don’t know what’s causing it, I’ve tried cleaning caches and restarting.

Here’s exactly what the setup looks like.

enter image description here

enter image description here

Edit Configurations > ‘+’ > Application:

HI Adam, can I know if it still works? And why would we need gradle for this application? Could the project be further cleaned up so just the bare minimum files are present?

Hi @Harsha, I implemented this as an experiment in 2016. I have not implemented this recently as I use standalone IntelliJ for backend Kotlin/Java applications. Feel free to update the comments/post with your updated findings.

I have done it in the same way. It is still working. I would like to start it from the command line but I can not find how to instruct Gradle to use the application configuration.

I found a somewhat hacky, annoying and not-completely-sure-it-always-works solution to this. I wanted to share in case someone else finds it useful.

In Android Studio, you can right-click a class with a main method and select «Run .main()». This will create a new Run configuration for YourClass, although it won’t quite work: it will be missing some classpath entries.

In order to fix the missing classpath entries, go into the Project Structure and manually add the output folder location for your module and any other module dependencies that you need, like so:

  • File -> Project Structure .
  • Select «Modules» in the Project Settings panel on the left-column panel
  • Select your module on the list of modules in the middle-column panel
  • Select the «Dependencies» tab on the right-column panel

And then for the module where you have your Java application as well as for each of the module dependencies you need: — Click «+» -> «Jars or directories» on the far right of the right-column panel — Navigate to the output folder of the module (e.g.: my_module/build/classes/main/java) and click «OK» — On the new entry to the Dependencies list, on the far right, change the select box from «Compile» to «Runtime»

After this, you should be able to execute the Run configuration you just created to run the simple Java application.

One thing to note is that, for my particular [quite involved] Android Studio project set-up, I have to manually build the project with gradle, from outside Android Studio in order to get my simple Java Application classes to build, before I run the application — I think this is because the Run configuration of type «Application» is not triggering the corresponding Gradle build.

Finally, this was done on Android Studio 0.4.0.

I hope others find it useful. I also hope Google comes around to supporting this functionality soon.

Источник

How to run Java app in Android

Is it possible to run java app (jar) in my android application? Because I need to create PDF, the problem is if I generate PDF in android, only can show with small image, if it contains large image in many pages, it will be error. So I think, I can generate PDF in java and then included to android app.

is it possible to run java app (jar) in my android application , no Android does not support Java. Sorry.

Java programs run everywhere where the JVM is installed. However android uses different VM — Dalvik. My guess would be you can do it only through emulator.

6 Answers 6

Concernig the mentiones app JBED: Well honestly, I could not find any credible source for this tools JBED, so I would really be very cautious (e.g. who is the developer?)

In the manifestfile (in Androidmanifest.xml, where every app has to state what rights it needs to run, see How to view AndroidManifest.xml from APK file?) there are many rights mentioned (what could be necessary, as the app wants to run as an emulator), so a java application might want to send an SMS, record audio, take pictures and place calls — so the emulator would need those rights as well.

But then the app also registers the «android.intent.action.BOOT_COMPLETED» event (i.e. autostart after boot) and this would go against every description of the tool.

Ah yeah and giveaway: The apk has a folder «certs» that has some (root-)certificates. But those are not the real certificates of the authorities, e.g. Versign. If one installs the app and by that those certificates the trust you might have in https-connections is lost because those who made the fake certificates can create own, false certificates that your phone would trust.

I assume (or am pretty sure) this is a spy tool, but I could be wrong. The (rare) testimonials that claim the tool ran perfectly will probably be the same person that posted the tool under a different name.

Источник

Оцените статью