4

I am trying to write a simple cordova-plugin for android, but I am stuck into a crash that I don't know how to resolve.

I basically followed these steps to create simple plugin and it worked fine.

http://www.mat-d.com/site/tutorial-creating-a-cordova-phonegap-plugin-for-android-app/

Yet, my plugin must call an activity that is packaged as an .aar file. Basically I have a full blown application with java/layouts that needs to be triggered by the plugin.

I followed these steps to add my .aar file to the android build of cordova.

Cordova plugin development - adding aar

It also worked. Now, I have so far in my plugin structure these files (admittedly is pretty hacked right now since I kept most of original names):

enter image description here

in my yourFile.gradle I have this:

repositories{    
  jcenter()
  flatDir{
      dirs 'libs'
   }
}

dependencies {
   compile(name:'mylittlelibrary-release', ext:'aar')
   compile 'com.android.volley:volley:1.0.0'
}

android {
  packagingOptions {
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
  }
}

and the main part of my "CoolPlugin.java" looks like this:

public class CoolPlugin extends CordovaPlugin {
    public CoolPlugin() {}
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
    }
    public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                final Context context = cordova.getActivity().getApplicationContext();
                try {
                    Intent intent = new Intent(context, testv.com.mylittlelibrary.MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    cordova.getActivity().startActivity(intent);
                } catch (Exception e) {
                    String errorText = e.toString();
                    Toast toast = Toast.makeText(context, errorText, duration);
                    toast.show();
                }

My "mylittlelibrary-release.aar" has its main activity under the namespace "testv.com.mylittlelibrary" so the MainActivity.java looks like this:

package testv.com.mylittlelibrary;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;

import com.android.volley.*;
import com.android.volley.toolbox.*;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        etc...

I was reading the thread below, and looks like I need in somehow register the main activity of my aar file.

open an activity from a CordovaPlugin

I am not totally sure how to do this, so I went inside the auto-generated AndroidManifest.xml under

C:\cordova\hello\platforms\android\AndroidManifest.xml

and hacked the activity with the namespace inside as such:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.example.hello" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:label="@string/activity_name"
        android:launchMode="singleTop"
        android:name="testv.com.mylittlelibrary.MainActivity">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="23" />
</manifest>

I installed the plugin and ran the app using:

cordova plugin add c:/CoolPlugin/
cordova build android
cordova run android

there is button in the html file that triggers the plugin, which then triggers my activity as you see on the "MainActivity" of the "mylittlelibrary-release.aar" file.

Yet, I keep getting this crash. I believe the android plugin cannot resolve the namespace properly and throws this exception.

com.example.hello E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hello, PID: 13119
    java.lang.NoClassDefFoundError: Failed resolution of: Ltestv/com/mylittlelibrary/MainActivity;
    at CoolPlugin$1.run(CoolPlugin.java:59)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5835)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)

I also believe is has something to do with the proper registering the MainActivity under the testv.com.mylittlelibrary. But I can't figure out how to do in in cordova. I need to resolve this, so I am glad to give you points of my reputation for a solid answer that unblocks me.

thank you.

Community
  • 1
  • 1
gmmo
  • 2,577
  • 3
  • 30
  • 56

1 Answers1

3

nevermind, it was simple mistake. The library missed some imports and a theme. This fixed the issue.

Manifest:

android:theme="@style/Theme.AppCompat.Light"

Gradle:

dependencies {
   compile(name:'mylittlelibrary-release', ext:'aar')
   compile 'com.android.support:support-v13:23.1.0'
   compile 'com.android.support:support-v4:23.1.0'
   compile 'com.android.support:gridlayout-v7:23.1.1'
   compile 'com.android.support:appcompat-v7:23.1.0'
   compile 'com.android.support:design:23.1.0'
gmmo
  • 2,577
  • 3
  • 30
  • 56
  • Hi , Glad that you worked out a resolution. I need your suggestion for the below one. I have an aar file which is included similar way which has classes and methods to be referred in Activity. But I need to create an Activity which will reside outside of aar file and inside my Cordova Plugin. How can this be achieved? Any suggestion? – Sreehari Dec 20 '16 at 06:44
  • I tried your way as well. Creating a Library. Tested the library with Native android application and works well. But when included in hybrid application as an pluginname.aar file with modifications including manifest in hello\platforms\android\AndroidManifest.xml , I am getting error . Caused by: java.lang.ClassNotFoundException: Didn't find class "nativeplugin.cclib.MainActivity" on path: DexPathList. Any idea why its not able to refer? – Sreehari Dec 20 '16 at 14:06
  • Can you look into http://stackoverflow.com/questions/41245394/start-activity-inside-cordova-plugins-aars-file-in-ionic2 link which has similar issue ? – Sreehari Dec 20 '16 at 15:13
  • @Stallion the .aar file can contain as many activities and you want and you can fire any of them at any time. I believe you may have issues on your gradle files. This is when I had path problems with the cordova plugin. I posted a snipped of the code I use that work for me. Hope it helps. – gmmo Dec 20 '16 at 19:20
  • thanks for your support .. the flow is working successfully now – Sreehari Dec 22 '16 at 07:27