Cordova plugin development - adding aar

半城伤御伤魂 提交于 2019-11-26 08:06:23

问题


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.

Next step is to include this Android Studio Gradle project to my plugin.

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\" />

also I tried:

<framework src=\"libs/Broper/app/build.gradle\" custom=\"true\" type=\"gradleReference\" />

The graddle files are recognised by cordova. But something don\'t work. So i can\'t import the classes of that android studio project to my plugin java files.

Then a better solution (I thought so) is to add a AAR instead. But there I don\'t even have a clue what to do to add that AAR in my cordova plugin.

So, the question is: How do I add a android atudio aroject (or library) to my cordova plugin the right way?


回答1:


Here's what I've done to use a gradle reference with a Cordova plugin, I think this might help you.

Global structure :

pluginFolder/
  build-extras.gradle
  plugin.xml
  yourDirContainingYourAAR/
  src/
    android/
      yourFile.gradle
      myPlugin.java

Put your library, say foo.aar, in the yourDirContainingYourAAR directory (create it if needed)

  • In the plugin.xml file :

    <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>
    
  • In the gradle file yourFile.gradle :

    repositories{    
      jcenter()
      flatDir {
          dirs 'libs'
       }
    }
    
    dependencies {
       compile(name:'foo', ext:'aar')
    }
    
    android {
      packagingOptions {
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
      }
    }
    
  • 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'
       }
    }
    



回答2:


There has to be a slight modification to Niko's answer

When gradle runs the compilation using Niko's answer, it searches for the libs in app/libs which is not present in the project (it does in the standard Android Studio project, but in Cordova Android project structure the libs folder is different). However the plugin copies the aar library to app/src/main/libs which can make us think it is copying the aar to app/libs

<resource-file src="yourDirContainingYourAAR/foo.aar" target="libs/foo.aar" />

Hence the gradle file should be

repositories{    
 jcenter()
   flatDir {
     dirs 'src/main/libs'
   }
}

dependencies {
  compile(name:'foo', ext:'aar')
}

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

This will give you a successful compilation of the app

Cheers!!




回答3:


After cordova project creation, adding platform, adding plugin is done.

Lets say the aar file are copied in libs folder. ( assume file name is cards.aar )

then in app build.gradle specify following and click sync project with Gradle files.

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile(name:'cards', ext:'aar')
}

It works for me!! but one drawback of this solution is .. you need to open the project from platform folder on Android Studio and add the above lines to your build.gradle



来源:https://stackoverflow.com/questions/30757208/cordova-plugin-development-adding-aar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!