Issues with importing an Ionic app into Android Studio

I was able to successfully import an Ionic app into the latest build of Android Studio.

The trick is to take an existing Android Studio project and then copy its gradle directory over to the platforms/android directory.

Then just import the existing project. It will prompt you to fix plugins and re-import.

After that you have to edit both “build.gradle” files and specify android version 19.1.0 and comment out the compile options for the JAVA version.

It will then build successfully.

However, when I try and run it on an emulator or a genymotion vm, I get the following error.

Any suggestions on what might be causing the error?

10-01 19:06:48.645    1322-1322/com.ionicframework.chyldweather207963 I/System.out﹕ Error adding plugin com.ionic.keyboard.IonicKeyboard.
10-01 19:06:48.645    1322-1322/com.ionicframework.chyldweather207963 D/AndroidRuntime﹕ Shutting down VM
10-01 19:06:48.645    1322-1322/com.ionicframework.chyldweather207963 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa6272288)
10-01 19:06:48.645    1322-1322/com.ionicframework.chyldweather207963 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ionicframework.chyldweather207963/com.ionicframework.chyldweather207963.chyldweather}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:191)
            at org.apache.cordova.PluginManager.startupPlugins(PluginManager.java:114)
            at org.apache.cordova.PluginManager.init(PluginManager.java:92)
            at org.apache.cordova.CordovaWebView.loadUrlIntoView(CordovaWebView.java:383)
            at org.apache.cordova.CordovaWebView.loadUrlIntoView(CordovaWebView.java:368)
            at org.apache.cordova.CordovaWebView.loadUrl(CordovaWebView.java:344)
            at org.apache.cordova.CordovaActivity.loadUrl(CordovaActivity.java:376)
            at com.ionicframework.chyldweather207963.chyldweather.onCreate(chyldweather.java:32)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
1 Like

Why open android studio at all? You can do all the things you need to do for an android app inside your terminal.

I had that issue forever ago and it has to do with the build system. Cordova creates a project that gets built by ant. Android studio uses gradle. While you can get everything to work, it’s much easier to do builds from the command line.

So, I can debug the java portion of the app if I need to.

I use Xcode with ios publishing - for deploying to the app store and also with debugging/figuring out what the objective-c code is actually doing. I would like to have the same capabilities in Android Studio, if possible.

Android Studio is a pretty nice IDE (way better that eclipse) and is useful to have if you need it.

Yeah you can.

$ adb logcat

http://developer.android.com/tools/help/adb.html#logcat

If you need to do any deep debugging of any java, check out this

For iOS, Xcode is going to be need since apple keeps everything pretty closed off. But with android, you can do just about everything from debugging, building debug release, to full signed release apps with out an IDE.

Totally agree that android studio is way better than eclipse, just about anything is to be honest

Just trying to point out that you may not need to have to use android studio.

I noticed now that ‘ionic platform add android’ generates an android directory in the platforms directory that contains gradle files. They are dated though (because android gradle has a rapid pace). Moreover there’s a number of things to tweak to get it to work. First of all your JAVA_HOME and ANDROID_HOME should be set correctly (see e.g. http://therockncoder.blogspot.nl/2014/12/installing-ionic-framework-from-scratch.html). I made a github repo so you can see from the commit chain what I changed to get it to work properly in Android studio at:
https://github.com/hansvdam/sample-ionic-Android-Studio/commits/master
And yeah, then you open the ‘android’ directory in Android studio.
And also ‘Why would you want to run stuff from android studio?’: Let’s say there’s stuff in your app that has to be native. Maybe ionic is even only a small portion of your app. Then you want to have a proper Android IDE to work with the native part.

3 Likes

Hi hdam, I’ve tried to follow your git solution without success, have you got some notes or tutorial of the process?

for ionic staff: As Hdam said there is stuff that needs to be native, also in my case I’m a developer focused in applications for industrial equipment and I’m used to work with SBCs with hardware features that aren’t found on mobiles or tablets.

Thanks.

Hi hdam,

Can you explain a little bit more on how to open the android directory in Android Studio?
I downloaded the app but there is nothing that I can run with:

Hi taquionbcn,

Do you successfully import the app into Android Studio?

Hi mhartington,

Could you please share us some tips/tutorials on how to debug the native code (java/objective c)?
I think that would be very helpful for us.

Your help is much appreciated!
Thanks very much!

Hi chyld:

Do you successfully import the app in the end?
If so, could you please share your steps with us?

Thanks very much!

Nope, I started a new project using IntelliJ and learned the hard way how to do a cordova-ionic project.

I created the ionic project and then create a cordova plugin for each native java element that I need then I “imported” the native java code to the cordova plugin.

Hi taquionbcn,

Finally, I have managed to debug the Android Code in Eclipse, not in Android Studio. Since Android Studio tried to download some plugin/config from Maven, and it took like forever to finish, I had no choice but to give up.

Here is some steps that you can reference:

  1. Add Android Development Tools in Eclipse
  2. Import Your platforms–>android
  3. Add your project(MainActivity) and cordovaLib
  4. Debug your applications

Then here you goes. I know it seems quite silly, but it really took me days to figure out how to debug the java code.

I have tried adb logcat, but the log was like went crazy, and it would kill me to find out my logs in it. Then I found out the android monitor.bat, which can add filters to your applications , and it looked quite promising, but it can not load any logs that were written in the java code(and i don’t know why). Then here is the eclipse debug.

If anyone has any other ways to debug, it would be quite appreciated to share with us.