java 如何正确包含cordova插件的外部jar文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25438160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:02:18  来源:igfitidea点击:

How do I properly include an external jar file for a cordova plugin?

javaandroidcordova

提问by konsumer

I am trying to make a simple cordova android plugin that requires classes defined in a jar file. I have a test project here, that includes the example usage & a simplified version of my plugin.

我正在尝试制作一个简单的cordova android插件,它需要在jar文件中定义的类。我在这里有一个测试项目,其中包括示例用法和我的插件的简化版本。

In my plugin.xml, I have this:

在我的 plugin.xml 中,我有这个:

<platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Pebble">
                <param name="android-package" value="com.jetboystudio.pebble.PebblePGPlugin"/>
            </feature>
        </config-file>
        <source-file src="src/android/PebblePGPlugin.java" target-dir="src/com/jetboystudio/pebble" />
        <source-file src="src/android/libs/pebble_kit.jar" target-dir="libs" />
    </platform>

In my test project, I have pebblekit jar in-place where it needs to be (I think): plugins/com.jetboystudio.pebble.PebblePGPlugin/src/android/libs/pebble_kit.jar

在我的测试项目中,我将 pebblekit jar 放在需要的位置(我认为):plugins/com.jetboystudio.pebble.PebblePGPlugin/src/android/libs/pebble_kit.jar

When I "cordova build" I get no errors, but when I install the apk-file I get "Class not found" in Chrome device inspect. I am assuming this class it can't find is one of the classes defined in pebble_kit.jar. Also, it doesn't seem to be copying this file into platforms/android.

当我“cordova build”时,我没有收到任何错误,但是当我安装 apk 文件时,我在 Chrome 设备检查中得到“Class not found”。我假设它找不到的这个类是在 pebble_kit.jar 中定义的类之一。此外,它似乎没有将此文件复制到平台/android 中。

If I could just debug better (which class is not found?) that might be a good start, if no one has the actual answer of why this doesn't work.

如果我可以更好地调试(找不到哪个类?),这可能是一个好的开始,如果没有人知道为什么这不起作用的实际答案。

采纳答案by csantanapr

Your plugin.xmlis correct.

plugin.xml是对的。

Do not edit the plugin.xmlafter you have added/install the plugin into the project.
When you run cordova build or prepare it will not process the native parts of plugin.xml, it only get's process on cordova plugin add

plugin.xml在将插件添加/安装到项目中后,请勿编辑。
当您运行 cordova build 或 prepare 时,它​​不会处理 plugin.xml 的本机部分,它只会获取对cordova plugin add 的处理

Update your plugin repo/folder with plugin.xmlcontaining the jarfile and the line that you have is correct.

使用plugin.xml包含jar文件和正确的行更新您的插件存储库/文件夹。

Then do

然后做

cordova plugin rm

cordova plugin add

cordova build

The end result verify that /platforms/android/libs/pebble_kit.jaris present after cordova build.

最终结果验证/platforms/android/libs/pebble_kit.jar在cordova构建之后存在。

回答by Lindsay-Needs-Sleep

You can also add a jar file with:

您还可以添加一个 jar 文件:

<lib-file src="src/android/libs/pebble_kit.jar" />

This will add the jar to platforms/android/app/libs/

这会将罐子添加到 platforms/android/app/libs/