简体   繁体   中英

Cordova CLI project ignores build-extras after toolchain upgrade

Up until recently I have been using the following

  • Cordova Android 6.3.0
  • Gradle 4.9
    • Node 8.9.2

to build my hybrid Android app which uses one custom - ie written by me - plugin. The plugin in turn has a number of external dependencies which I specify via the myapp/platforms/android/build-extras.gradle file which is listed below

ext.postBuildExtras = 
{
 android
 {
  dependencies
  {
   compile 'com.squareup.duktape:duktape-android:1.3.0'    
   compile 'net.zetetic:android-database-sqlcipher:3.5.9@aar'
   compile 'co.realtime:messaging-android:2.1.+'
   compile 'com.google.android.gms:play-services-location:15.0.1'
   compile 'com.android.installreferrer:installreferrer:1.0'
  } 
  defaultConfig
  {
   jackOptions {enabled true}
  }
  compileOptions 
  {
   sourceCompatibility JavaVersion.VERSION_1_8
   targetCompatibility JavaVersion.VERSION_1_8
  }
  allprojects
  {
   compileOptions
   {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
   }
  }
 }
}

I target Android SDK 26 with the minimum SDK level set at 23. My Cordova config.xml file is shown below

<?xml version='1.0' encoding='utf-8'?>
<widget android-versionCode="1190" android-versionName="Etoile-2" 
 id="my.app.id" version="1.1.9" xmlns="http://www.w3.org/ns/widgets" 
 xmlns:cdv="http://cordova.apache.org/ns/1.0">
 <name>My App</name>
 <description>My App</description>
 <author email="someone@gmail.com" href="https://example.org">My Name. 
 </author>
 <content src="index.html" />
 <access origin="*" />
 <icon platform="android" qualifier="mdpi" src="res/mdpi.png" />
 <icon platform="android" qualifier="hdpi" src="res/hdpi.png" />
 <icon platform="android" qualifier="xhdpi" src="res/xhdpi.png" />
 <icon platform="android" qualifier="xxdpi" src="res/xxdpi.png" />
 <icon platform="android" qualifier="xxxdpi" src="res/xxxdpi.png" />
 <allow-intent href="http://*/*" />
 <allow-intent href="https://*/*" />
 <allow-intent href="tel:*" />
 <allow-intent href="sms:*" />
 <allow-intent href="mailto:*" />
 <allow-intent href="geo:*" />
 <platform name="android">
     <allow-intent href="market:*" />
     <FrameLayout android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      xmlns:android="http://schemas.android.com/apk/res/android">
    </FrameLayout>
    <preference name="android-minSdkVersion" value="23" />
    <preference name="android-targetSdkVersion" value="26" />
 </platform>
 <preference name="Orientation" value="portrait" />
 <plugin name="ext.org.plugin" spec="path:\to\my\plugin" />
 <engine name="android" spec="^7.0.0" />
</widget>

I am in the process of provisioning a more modern Windows PC to do my Android builds. In the process I have upgraded to

  • Cordova Android 7.0.0
  • Gradle 4.10.0 (installed via Scoop)
  • Node 10.10.0

I went through the process of reconstructing the entire project step-by-step

  1. Create a new Cordova CLI project cordova create myapp ext.example.myproj MyApp
  2. Add the Android platform cordova platform add android which adds Cordova Android 7.0.0
  3. Build this dummy app cordova build android --debug : working
  4. Replace the default Cordova config.xml with my version (shown above) minus the reference to my custom plugin
  5. Build again - still working
  6. Copy across my original build-extras.gradle file to myapp/platforms/android
  7. Build again - still working
  8. Add my custom plugin cordova plugin add 'path:\\to\\my\\plugin
  9. Issue a cordova clean followed by cordova build android which results in errors along the lines of

    :app:compileDebugJavaWithJavac path:\\to\\my\\app\\platforms\\android\\app\\src\\main\\java\\ext\\example\\plugin\\BCTrailer.java:4: error: package net.sqlcipher does not exist import net.sqlcipher.Cursor;

which appears to imply that the contents of my build-extras.gradle file were not used during the build. I deliberately corrupted that file by leaving out a brace to make the XML invalid. If the file were being read I had expected that Gradle would complain. Instead it just goes ahead and issues the same errors such as package net.sqlcipher does not exist etc.

I have noted talk of compile being deprecated in dependencies in favor of a whole new clutch of instructions such as implementation and api . I tried replacing compile with implementation in my own build-extras.gradle file but to no avail.

Clearly, I am doing something wrong here and it has to do with the changes in Gradle.

Update: fixed in cordova-android 7.1.3

In cordova-android 7.1.3 my pull request was included so the issue is fixed see the release notes

For cordova-android >= 7.0.0 but < 7.1.3

Move (or copy) the build-extras.gradle to platforms/android/app/

Old Answer:

I had a similar issue. Starting with cordova-android 7.0 cordova changed the directory structure and missed some changes in there code.

To get the build-extras.gradle working again just move it or better copy it (when the cordova-android is following their documentation again) to platforms/android/app/ .

Here is the issue and here is related pull request

Whilst I still do not understand the root cause of the problems I ran into during my attempt at upgrading the Android APK build toolchain I now have a solution which I am sharing here for the benefit of others who find this thread.

  • With Cordova Android 7.0.0 (as opposed to 6.3.1 which I was using previously) build-extras.gradle in the myapp/platforms/android folder is being ignored.
  • It was in any case incorrect to start declaring plugin dependencies via a build-extras file in the APK project itself.
  • Although this had always worked for me prior to this toolchain upgrade I had found it rather strange that dependencies declared in a build-extras.gradle **ext.postBuildExtras** were working to fork in external dependencies in a plugin which one would assume had been built in to the app by that point.
  • I tried switching my dependencies declaration to a build.gradle file in the actual plugin . However, this did not change the outcome - the dependencies did not make it into the build with the result that various import path.to.external.dependency declarations in Java units in the plugin threw errors.
  • I removed the build.gradle file and declared the dependencies via a series of <framework src='path/to/dependency' /> dependency statements in the plugin.xml file and Bingo! - it all worked.

As a side note - with Cordova Android 7 and anything higher than Node 9.0 you no longer need to use the, now deprecated, Jack compiler or do anything else to persuade Gradle/Android/Cordova to use Java 8. I am now no longer using a build-extras.gradle file in my app or a build.gradle file in my plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM