今回は、Cordova のプラグイン「phonegap-plugin-csdk-image-editor」を導入しようとした際に遭遇したエラーです。
なお、現在も違うエラーに苦戦中…。
…既存のコードを修正したほうが早いかもしれません。
エラーメッセージは「java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/os/AsyncTaskCompat;」です。
ちなみに、Android Studio で実行した際表示されたエラーメッセージ冒頭に書かれているエラーは「java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/os/BuildCompat;」でしたが、そちらではなく「java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/os/AsyncTaskCompat;」で検索したところ有効な記事にたどり着けました。
今回参考にさせていただいた記事はこちら。
android – java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/os/BuildCompat – Stack Overflow
https://stackoverflow.com/questions/39601370/java-lang-noclassdeffounderror-failed-resolution-of-landroid-support-v4-os-bui
で、対処法ですが、「support/v4/os/AsyncTaskCompat」関連のファイルが参照できないことが問題なので、手動で追加すればOKです。
下記の画像のように、java ディレクトリの直下に android.support.v4.os というディレクトリを作成します。

あとは、作成したディレクトリ内に、AsyncTaskCompat.java と AsyncTaskCompatHoneycomb.java のファイルを下記のコードで作成します。
AsyncTaskCompat.java
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.support.v4.os;
import android.os.AsyncTask;
import android.os.Build;
/**
* Helper for accessing features in {@link android.os.AsyncTask}
* introduced after API level 4 in a backwards compatible fashion.
*/
public final class AsyncTaskCompat {
/**
* Executes the task with the specified parameters, allowing multiple tasks to run in parallel
* on a pool of threads managed by {@link android.os.AsyncTask}.
*
* @param task The {@link android.os.AsyncTask} to execute.
* @param params The parameters of the task.
* @return the instance of AsyncTask.
*/
public static <Params, Progress, Result> AsyncTask<Params, Progress, Result> executeParallel(
AsyncTask<Params, Progress, Result> task,
Params... params) {
if (task == null) {
throw new IllegalArgumentException("task can not be null");
}
if (Build.VERSION.SDK_INT >= 11) {
// From API 11 onwards, we need to manually select the THREAD_POOL_EXECUTOR
AsyncTaskCompatHoneycomb.executeParallel(task, params);
} else {
// Before API 11, all tasks were run in parallel
task.execute(params);
}
return task;
}
private AsyncTaskCompat() {}
}
AsyncTaskCompatHoneycomb.java
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.support.v4.os;
import android.os.AsyncTask;
/**
* Implementation of AsyncTask compatibility that can call Honeycomb APIs.
*/
class AsyncTaskCompatHoneycomb {
static <Params, Progress, Result> void executeParallel(
AsyncTask<Params, Progress, Result> task,
Params... params) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}
}
上記 2つのファイルが作成出来たら、アプリを一度 Clean してから Build → 実行してください。
私の環境では、問題なく動作しました!
以上、「phonegap-plugin-csdk-image-editor」プラグインを導入時に遭遇したエラー「java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/os/AsyncTaskCompat;」の対処法でした。
「phonegap-plugin-csdk-image-editor」プラグインの導入方法そのものについては、後日ご紹介する予定です。