Cordova插件开发——添加aar [英] Cordova plugin development - adding aar
问题描述
我是cordova插件开发的新手.我想写一个插件,可以打开一个新的android活动并显示一些广告.
I am new to the cordova plugin development stuff. I want to write a plugin which is able to open a new android activty and showing some advertisement.
所以我遵循了一个简单的教程这里.效果很好,符合预期.
So I followed a simple tutorial here. That works very well and as expected.
下一步是将此 Android Studio Gradle 项目包含到我的插件中.
Next step is to include this Android Studio Gradle project to my plugin.
我的第一次尝试:将 gradle 项目添加到我的cordova 插件的子文件夹中,并将以下行添加到plugin.xml 文件中:
My first try: Adding the gradle project to a subfolder of my cordova plugin and adding the following line to the plugin.xml file:
<framework src="libs/Broper/build.gradle" custom="true" type="gradleReference" />
我也试过:
<framework src="libs/Broper/app/build.gradle" custom="true" type="gradleReference" />
不起作用.所以我无法将那个 android studio 项目的类导入到我的插件 java 文件中.
doesn't work. So I can't import the classes of that android studio project to my plugin java files.
然后一个更好的解决方案(我是这么认为的)是添加一个 AAR.但是我什至不知道如何在我的cordova插件中添加该AAR.
Then a better solution (I thought so) is to add an AAR instead. But there I don't even have a clue what to do to add that AAR in my cordova plugin.
那么,问题是:如何以正确的方式将 android studio 项目(或库)添加到我的cordova 插件中?
So, the question is: How do I add an android studio project (or library) to my cordova plugin the right way?
推荐答案
这是我在 Cordova 插件中使用 gradle 引用所做的工作,我认为这可能对您有所帮助.
Here's what I've done to use a gradle reference with a Cordova plugin, I think this might help you.
全局结构:
pluginFolder/
build-extras.gradle
plugin.xml
yourDirContainingYourAAR/
src/
android/
yourFile.gradle
myPlugin.java
将您的库,例如 foo.aar
,放在 yourDirContainingYourAAR
目录中(如果需要,请创建它)
Put your library, say foo.aar
, in the yourDirContainingYourAAR
directory (create it if needed)
在
plugin.xml
文件中:
<platform name="android">
<!-- your configuration elements, references, source files, etc... -->
<framework src="src/android/yourFile.gradle" custom="true" type="gradleReference" />
<resource-file src="yourDirContainingYourAAR/foo.aar" target="libs/foo.aar" />
</platform>
在gradle文件yourFile.gradle
中:
repositories{
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'foo', ext:'aar')
}
android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
在您的插件的根文件夹(与 plugin.xml
相同级别)创建一个 build-extras.gradle
.如果需要,根据您的项目需要添加或删除 minSdkVersion
和 targetSdkVersion
:
In the root folder of your plugin (same level as plugin.xml
) create a build-extras.gradle
.
If needed, add or remove minSdkVersion
and targetSdkVersion
according to your project needs :
android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
}
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
这篇关于Cordova插件开发——添加aar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!