使用AndroidX将自定义.aar库添加到Cordova插件

时间:2019-03-21 13:18:35

标签: android cordova gradle plugins aar

客观

我需要为Cordova开发一个插件,该插件为Android应用程序实现一些我已经开发的代码(AAR文件)。

系统

  • 操作系统:Ubuntu 18.04.2 LTS
  • IDE:VSCode和Android Studio
  • 电话:Nexus 4-Android 5.1.1
  • 科尔多瓦版本:8.1.2(cordova-lib@8.1.1)
  • Cordova应用程序的SDK信息(来自build.gradle):
defaultBuildToolsVersion="27.0.1" //String
defaultMinSdkVersion=19 //Integer - Minimum requirement is Android 4.4
defaultTargetSdkVersion=27 //Integer - We ALWAYS target the latest by default
defaultCompileSdkVersion=27 //Integer - We ALWAYS compile with the latest by default

我尝试过的

  1. 我已经从该项目中创建了一个.aar文件,并且正在尝试将其添加到我的Cordova插件中。我可以从该项目中导入方法,但是一旦打开使用这些方法的活动,应用程序就会崩溃(该应用程序仍可正确构建)。
03-21 12:35:59.728 12235-12235/io.cordova.hellocordova E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.cordova.hellocordova, PID: 12235
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/content/ContextCompat;
  1. 我认为这可能是由于androidx所致。因此,我将该行添加到了Failed resolution of: Landroidx/core/content/ContextCompat;文件中:
plugin.xml
  1. 进行此更改后,应用程序拒绝构建,但出现了这些错误(我用<!-- I have the same version of androidx.appcompat in my aar library --> <framework src="androidx.appcompat:appcompat:1.0.0-beta01"/> 替换了绝对项目路径):
<projectpath>
  1. 由于这些错误,我开始认为这可能是AGPBI: {"kind":"error","text":"error: resource android:attr/dialogCornerRadius not found.","sources":[{"file":"/home/benjamin/.gradle/caches/transforms-1/files-1.1/appcompat-1.0.0-beta01.aar/ad380179fb375e61241b11fa4df558eb/res/values-v28/values-v28.xml","position":{"startLine":8,"startColumn":4,"startOffset":447,"endLine":11,"endColumn":12,"endOffset":684}}],"original":"","tool":"AAPT"} AGPBI: {"kind":"error","text":"error: resource android:attr/dialogCornerRadius not found.","sources":[{"file":"/home/benjamin/Documents/Projects/Git/CordovaPluginProcessSdk/MobileApp/platforms/android/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values-v28/values-v28.xml","position":{"startLine":10}}],"original":"","tool":"AAPT"} AGPBI: {"kind":"error","text":"error: resource android:attr/fontVariationSettings not found.","sources":[{"file":"/home/benjamin/.gradle/caches/transforms-1/files-1.1/appcompat-1.0.0-beta01.aar/ad380179fb375e61241b11fa4df558eb/res/values/values.xml","position":{"startLine":1303,"startColumn":4,"startOffset":70911,"endColumn":68,"endOffset":70975}}],"original":"","tool":"AAPT"} AGPBI: {"kind":"error","text":"error: resource android:attr/ttcIndex not found.","sources":[{"file":"/home/benjamin/.gradle/caches/transforms-1/files-1.1/appcompat-1.0.0-beta01.aar/ad380179fb375e61241b11fa4df558eb/res/values/values.xml","position":{"startLine":1303,"startColumn":4,"startOffset":70911,"endColumn":68,"endOffset":70975}}],"original":"","tool":"AAPT"} <projectpath>/platforms/android/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values-v28/values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found. <projectpath>/platforms/android/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values-v28/values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found. <projectpath>/platforms/android/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:228: error: resource android:attr/fontVariationSettings not found. <projectpath>/platforms/android/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:228: error: resource android:attr/ttcIndex not found. error: failed linking references. Failed to execute aapt com.android.ide.common.process.ProcessException: Failed to execute aapt at com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:796) at com.android.build.gradle.tasks.ProcessAndroidResources.invokeAaptForSplit(ProcessAndroidResources.java:551) at com.android.build.gradle.tasks.ProcessAndroidResources.doFullTaskAction(ProcessAndroidResources.java:285) at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:109) at sun.reflect.GeneratedMethodAccessor274.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73) at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTaskAction.doExecute(DefaultTaskClassInfoStore.java:173) at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:134) at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:121) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:122) at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:111) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70) at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:63) at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54) at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88) at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52) at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52) at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54) at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43) at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248) at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:124) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:80) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:105) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:99) at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:625) at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:580) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:99) at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) at java.lang.Thread.run(Thread.java:745) Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details cordova库之间的错误。要检查我是否尝试在该行中添加其他库(凌空,它不是我的aar库工作所必需的):
androidx
  1. 该应用程序成功构建。但是,当我运行它时,一旦我们开始使用AAR库中的元素,它就会崩溃。 (请参阅第1步)

Cordova是否与androidx兼容?如果是,是什么导致我无法使用它?

似乎与我的测试和错误不兼容,但是我没有在Cordova文档网站或Google搜索中找到有关该主题的相关信息。所以我无法确认。

PS:我试图尽可能详尽,请询问您是否缺少信息。


编辑#1:

  1. 我试图在我的.aar库中将<framework src="com.android.volley:volley:1.1.1"/> 改回androidx。我生成了一个新的.aar并使用
appcompat

代替androidx导入。

结果:与(1.)问题相同。

结论:可能不是因为androidx

  1. 我记得在Cordova的官方文档中,他们使用的是appcompat:Cordova - Plugin.xml - Framework。所以我用了这个:
<framework src="com.android.support:appcompat-v7:28.0.0"/>

它正确构建。但是仍然崩溃,因为此版本的库不存在.aar文件中使用的函数。

<!-- Depend on v21 of appcompat-v7 support library -->
<framework src="com.android.support:appcompat-v7:21+" />

如果可能的话,我想继续使用androidx而不是appcompat,因为我还有其他依赖它的模块。

2 个答案:

答案 0 :(得分:1)

缺少的资源(ttcIndex等)仅在API 28中添加-这就是为什么它们位于名为values-v28的文件夹中的原因。

您需要将compileSdkVersion更新为28,以使其成功编译。

答案 1 :(得分:0)

由于本文档Flutter Doc,我迁移了项目以使其与Androidx兼容。 (就像Android Studio可以使用Refactor > Migrate to AndroidX选项一样)。是的,它来自Flutter,而不是Cordova,但是由于它是任何Android项目的常规信息,因此仍然适合。

我要做的主要事情是:

  • 将文件gradle.properties添加到platforms/android
android.enableJetifier=true
android.useAndroidX=true
  • 将minSdkVersion从15升级到19
  • 将目标升级并编译sdkVersion从2728
  • 将gradle从3.0.0更新到3.3.2

自从我使用Cordova CLI创建项目以来,就这些功能而言,我认为它不会像普通的Android项目那样工作,因此我忽略了它。

我已经进行了一些测试,并且该应用程序现在似乎可以正常运行。

编辑#1

我不得不再次解决该问题,因此我之前完成了解决问题的步骤。看来,解决该问题的一个方法是将platform/android/project.properties文件中的版本从target=android-27更改为target=android-28