启动活动驻留在Ionic2项目中的Cordova插件的aar文件中

时间:2016-12-20 15:06:10

标签: android android-studio ionic2 cordova-plugins

我想要实现的目标:

我创建了Android Plugin Projectcclib-release.aar),其中包含有针对性的活动。

我想使用ionic2 Cordova Plugincclib-release.aar中调用此活动,Cordova Plugin已将java.lang.NoClassDefFoundError: Failed resolution of: Lcompute/cclib/MainActivity; Caused by: java.lang.ClassNotFoundException: Didn't find class "compute.cclib.MainActivity" on path: DexPathList[[zip file "/data/app/com.ionicframework.fragmentwithionic759798-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ionicframework.fragmentwithionic759798-2/lib/arm, /vendor/lib, /system/lib]] 添加为cclib-release.aar内的依赖项。 aar file added in cordova plugin as per this link

面临问题:

在调用Activity时,我收到以下错误

    public class Inject extends CordovaPlugin {

    @Override
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

       if (action.equals("coolMethod")) {
                String message = args.getString(0);
                this.coolMethod(message, callbackContext);
                return true;
            }
           return false;
      }

    private void coolMethod(String message, CallbackContext callbackContext) {

         if (message != null && message.length() > 0) {
                cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    Intent intent = new Intent(cordova.getActivity().getApplicationContext(), 
                    compute.cclib.MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    cordova.getActivity().startActivity(intent);
                }
                });
                callbackContext.success(message);
            } else {
                callbackContext.error("Expected one non-empty string argument.");
            }
      }
    }

此问题类似于解决问题的SO Link。 但即使遵循类似的步骤,我也会得到上述错误!

我的插件结构 :( cordova plugin add PATH_TO_PLUGIN 是我的目标库,包含Activity)

enter image description here

我的 Inject.java 就像这样

AndroidManifest.xml

使用

在ionic2中添加此插件
FragmentWithIonic\platforms\android

根据SO的上述答案,我手动修改了Ionic2项目<activity android:label="@string/activity_name" android:launchMode="singleTop" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:name="compute.cclib.MainActivity"> </activity> .Path = this.platform.ready().then(() => { window.plugins.Inject.coolMethod(message, "short", position); }); 以引用活动。

import compute.cclib.MainActivity

我在下面的Ionic2中调用它,在其他情况下可以正常工作。

Inject.java

我尝试在<?xml version='1.0' encoding='utf-8'?> <manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.ionicframework.fragmentwithionic759798" xmlns:android="http://schemas.android.com/apk/res/android"> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true"> <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize"> <intent-filter android:label="@string/launcher_name"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="@string/activity_name" android:launchMode="singleTop" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:name="compute.cclib.MainActivity"> </activity> </application> <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="24" /> </manifest> 中手动Inject.gradle。但是调用Activity仍然会给出运行时错误!

  

下面是修改过的FragmentWithIonic \ platforms \ android \ AndroidManifest.xml

   repositories{
   jcenter()
   maven { url "https://jitpack.io" }
   flatDir{
    dirs 'aar'
   }
   }
  dependencies {
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile(name:'cclib-release', ext:'aar')
   }

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

在我的插件中调用Activity需要做些哪些修改?

编辑1

在@gmmo建议之后,我在插件的gradle文件中包含了参考库(A problem occurred configuring root project 'android'. > You have not accepted the license agreements of the following SDK components: [Android Support Repository]. Before building your project, you need to accept the license agreements and complete the installation of the missing components using the Android Studio SDK Manager. Alternatively, to learn how to transfer the license agreements from one workstation to another, go to http://d.android.com/r/studio-ui/export-licenses.html

compile 'com.android.support:appcompat-v7:25.1.0'

现在我在构建ionic2项目时遇到以下错误。

XCreateSimpleWindow

enter image description here

我已经更新了SDK中的所有许可协议。我很确定这是由于Inject.gradle中的这一行

CreateWindow("edit"..)

插件可能无法找到相应的依赖项

2 个答案:

答案 0 :(得分:1)

你绝对可以在aar文件中调用完整的活动。我的aar文件包含几个活动。我没有看到你提到的问题,但是当aar gradle文件出现问题时,我看到了路径问题。您可能想要修改gradle文件。这是我的插件使用的那个:

repositories{
  jcenter()
  maven { url "https://jitpack.io" }
  flatDir{
      dirs 'libs'
   }
}
dependencies {
    compile 'com.android.support:support-v13:23.1.0'
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:gridlayout-v7:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.google.android.gms:play-services-base:8.1.0'
    compile 'com.google.android.gms:play-services-location:8.1.0'
    compile 'com.google.android.gms:play-services-maps:8.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:recyclerview-v7:23.1.0'
    compile 'com.android.support:cardview-v7:23.1.0'
    compile 'org.altbeacon:android-beacon-library:2.+'
    compile 'com.cloudinary:cloudinary-android:1.2.2'

    compile(name:'mysdk', ext:'aar')
}

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

这就是我从cordova插件调用活动的方法:

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    Logger.v(Constants.Engine.LOGGER_TAG_APP, "Plugin Initialized...");
    Context context = cordova.getActivity();

    // This works for me
    Intent intent = new Intent(context, PermissionsActivity.class);
    context.startActivity(intent);
}

并剪掉了我的插件.xml

<!-- Initialization -->
<platform name="android">
    <config-file target="config.xml" parent="/*">
        <feature name="PlatformSDK">
            <param name="android-package" value="com.mycompany.PlatformSdk.cordova.PlatformSDKCordovaPlugin" />
            <param name="onload" value="true" />
        </feature>
    </config-file>
    <framework src="libs/mysdk.gradle" custom="true" type="gradleReference" />
    <resource-file src="libs/mysdk.aar" target="libs/mysdk.aar" />
    <source-file src="PlatformSDKCordovaPlugin.java" target-dir="src/com/mycompany/PlatformSDK/cordova" />
</platform>

答案 1 :(得分:0)

Okey终于找到了问题。

Ionic2项目需要通过以下命令更新SDK工具

android update sdk --no-ui --filter extra

cordova-plugin-issues

获得此输入

还提到了另外一个命令,我不确定下面是哪一个。如果有人遇到类似问题也可以试试这个

android update sdk --no-ui --all --filter
  

非常感谢@gmmo,因为他给了我添加全部的输入   依赖项以及插件Inject.gradle

正如我在一个印象中所说,为什么在Android插件项目中已经涵盖所有这些依赖项之后需要这样做。