简体   繁体   English

驻留在Ionic2项目中Cordova插件的aar文件中的Start Activity

[英]Start Activity residing inside Cordova Plugin's aar's file in Ionic2 Project

What I am trying to achieve: 我想要达到的目标:

I have created Android Plugin Project ( cclib-release.aar ) which has a targeted Activity. 我创建了具有目标活动的Android Plugin Projectcclib-release.aar )。

I want to invoke this Activity in ionic2 using Cordova Plugin which has cclib-release.aar added as dependency inside Cordova Plugin . 我想援引这一活动ionic2使用Cordova Plugin具有cclib-release.aar添加为内部依赖Cordova Plugin aar file added in cordova plugin as per this link 按照此链接在cordova插件中添加了aar文件

Problem facing: 面临的问题:

While invoking Activity, I am getting the following error 调用活动时,出现以下错误

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]]

The issue is similar to SO Link in which the issue was resolved. 该问题类似于解决该问题的SO Link But even after following similar step's I am getting the above mentioned error! 但是即使执行了类似的步骤后,我仍然遇到上述错误!

My Plugin Structure : ( cclib-release.aar is my targeted library with Activity) 我的插件结构 :( cclib-release.aar是我的带有Activity的目标库)

在此处输入图片说明

My Inject.java goes like this 我的Inject.java像这样

    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.");
            }
      }
    }

Added this plugin in ionic2 using 使用ionic2添加了此插件

cordova plugin add PATH_TO_PLUGIN

As per above mentioned answer by SO, I have modified Ionic2 project's AndroidManifest.xml manually .Path = FragmentWithIonic\\platforms\\android to refer the Activity. 按照上面提到的答案,我手动修改了Ionic2项目的AndroidManifest.xml = FragmentWithIonic\\platforms\\android以引用该Activity。

<activity android:label="@string/activity_name"  
android:launchMode="singleTop" android:theme="@android:style/Theme.DeviceDefault.NoActionBar"
android:name="compute.cclib.MainActivity">
</activity>

I am invoking this in Ionic2 using below which works fine in other cases. 我在Ionic2中使用下面的方法调用它,在其他情况下也可以使用。

this.platform.ready().then(() => {
            window.plugins.Inject.coolMethod(message, "short", position);
        });

I tried to manually import compute.cclib.MainActivity in Inject.java . 我试图在Inject.java手动import compute.cclib.MainActivity Inject.java But invoking Activity is still giving run time error! 但是调用Activity仍然会导致运行时错误!

Below is the modified FragmentWithIonic\\platforms\\android\\AndroidManifest.xml 下面是修改后的FragmentWithIonic \\ platforms \\ android \\ AndroidManifest.xml

<?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>

What modifications I need to do to invoke Activity in my plugin? 我需要做什么修改才能在插件中调用Activity?


EDIT 1 编辑1

After @gmmo suggested, I included reference libraries in my Plugin's gradle File ( Inject.gradle ) 在@gmmo建议之后,我在我的插件的gradle文件( Inject.gradle )中包含了参考库。

   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'
    }
 }

Now I am getting the following error while building my ionic2 project. 现在,在构建ionic2项目时出现以下错误。

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

在此处输入图片说明

I have already updated with all license agreements in SDK. 我已经更新了SDK中的所有许可协议。 I am pretty sure this is due to this lines in Inject.gradle 我很确定这是由于Inject.gradle中的这一行

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

Plugin might not able to find the corresponding dependencies 插件可能找不到对应的依赖项

You can definitely invoke full activities inside the aar file. 您绝对可以在aar文件中调用完整的活动。 My aar file contained several activities. 我的aar文件包含一些活动。 I have not seen the issue you mentioned, but I have seen path issues when the aar gradle file had problems. 我没有看到您提到的问题,但是当aar gradle文件出现问题时,我已经看到了路径问题。 You may want to revise your gradle file. 您可能要修改gradle文件。 This is the one my plugin uses: 这是我的插件使用的一个:

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'
  }
}

and this is how I invoke activities from the cordova plugin: 这就是我从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);
}

and snipped of my plugin .xml 并删除了我的插件.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>

Okey finally I figure out the issue. 最终,Okey我弄清楚了这个问题。

Ionic2 project need to update SDK tools by the following command Ionic2项目需要通过以下命令更新SDK工具

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

Got this input from cordova-plugin-issues cordova-plugin-issues获得了此输入

One more command was mentioned which I am not sure about which was the below one. 提到了另一个命令,我不确定哪个是下面的命令。 If someone come across similar issue can try this out as well 如果有人遇到类似问题,也可以尝试一下

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

A big Thanks to @gmmo , as he gave me the input to add all dependencies also along with plugin's Inject.gradle . 非常感谢@gmmo,他为我提供了添加所有依赖项以及插件的Inject.gradleInject.gradle

As I was in an impression that why would this be required after all these dependencies was already covered in Android Plugin Project. 就像我给人的印象那样,为什么在Android插件项目中已经涵盖了所有这些依赖项之后,为什么需要这样做呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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