Let me start with some context. I have a Java file and a .jar library which contains an Android project with an Activity, its layout, and another jar library. I also have the javascript interface for my plugin, of course. In other words, this is what I have:
plugins/com.phonegap.example.activity.plugin/www/myplugin.js
plugins/com.phonegap.example.activity.plugin/src/android/MyPlugin.java
plugins/com.phonegap.example.activity.plugin/src/android/libs/myJar.jar
plugins/plugin.xml
My plugin is supposed to execute a method within my MyPlugin.java file which is supposed to then launch the Activity that is within the myJar.jar file. The plugin.xml for this plugin reads:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="com.phonegap.example.activity.plugin"
version="0.1.0">
<name>MyPlugin</name>
<description>Launch an Android Activity from PhoneGap 3</description>
<author>Author</author>
<license>MIT</license>
<keywords>phonegap,activity</keywords>
<js-module src="www/myplugin.js" name="MyPlugin">
<clobbers target="window.MyPlugin" />
</js-module>
<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="MyPlugin">
<param name="android-package" value="com.phonegap.example.activity.plugin"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<activity
android:name="com.phonegap.example.activity.ExampleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.phonegap.example.action.EXAMPLE" />
</intent-filter>
</activity>
</config-file>
<source-file src="src/android/MyPlugin.java" target-dir="src/com/phonegap/example/activity" />
<source-file src="src/android/libs/myJar.jar" target-dir="libs" />
</platform>
</plugin>
I have installed Phonegap 3.3 and Cordova 3.4 using npm install, and I have Ruby 2.0.0 installed as well. I'm running all this on a Mac OS X.
Onto the problem:
phonegap create TestApp)cordova platform add android)phonegap local plugin add /path/to/plugin/folder from within my newly created appI run phonegap build android and the compile fails with the following error:
[error] An error occurred while building the android project. Error executing "ant debug -f "/path/to/TestApp/platforms/android/build.xml"": BUILD FAIL /path/to/ant/build.xml:720: The following error occurred while executing this line: /path/to/ant/build.xml:734: Compile failed; see the compiler error output for details.
Total time: 4 seconds
I don't understand why this is happening at all. When I install the plugin it seems like it's been added successfully and it appears within the plugins/android.json file.
At one point, the project automagically decided to compile properly (I don't know what made it work). I tried to access the plugin I had added by adding a line that reads var p = window.MyPlugin within the www/js/index.js, right after the app.receivedEvent('deviceready'); in the onDeviceReady function. I used console.log(p); and it turned out to be undefined, as if my plugin had never been loaded/initialized in the first place.
I suppose this is a problem within my plugin definition, since the Java part has been tested on an Android native app and it was working properly. Where did I go wrong with my plugin? What can I do to troubleshoot it? Why wasn't it initialized? Any help/guidance is very well-appreciated.
======UPDATE=====
I followed Dawson's suggestion to use cordova build android -d and here's the output I received:
[javac] /path/to/TestApp/platforms/android/src/com/phonegap/example/MyPlugin.java:10:
error: package com.phonegap.example.activity does not exist
[javac] import com.phonegap.example.activity.ExampleActivity;
This com.phonegap.example.activity package is defined within the myJar.jar file, so it seems that MyPlugin.java can't find the myJar.jar file.
I think the plugin.xml should be correctly taking care of the including the jar file during plugin installation. This is how it's being added in the plugin.xml:
<source-file src="src/android/libs/myJar.jar" target-dir="libs" />
Any ideas of what may be wrong here?
It's solved thanks to Dawson.
I ran cordova build android -d and viewed its output. Noticed misplaced libraries (myPlugin.java couldn't find myJar.jar) so I just moved them to the proper place as Dawson suggested.
Thanks a lot!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With