启动活动驻留在Cordonic插件的Ionic2项目中的aar文件中 [英] Start Activity residing inside Cordova Plugin's aar's file in Ionic2 Project

查看:149
本文介绍了启动活动驻留在Cordonic插件的Ionic2项目中的aar文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的目标:



我创建了 Android插件项目 cclib-release.aar )具有目标活动。



我想使用 Cordova Plugin ionic2 中调用此活动c> cclib-release.aar Cordova插件中添加为依赖项。



我的 Inject.java 就像这样

  public class Inject extends CordovaPlugin {

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

if(action.equals( coolMethod)){
String message = args.getString(0);
this.coolMethod(message,callbackContext);
返回true;
}
返回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(预期的一个非空字符串参数。);
}
}
}

在ionic2中添加此插件

  cordova插件添加PATH_TO_PLUGIN 

按照SO的上述回答,我手动修改了Ionic2项目的 AndroidManifest.xml .Path = FragmentWithIonic\platforms \ android 引用活动。

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

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

  this.platform.ready()。then(()=> {
window.plugins.Inject.coolMethod(message,short ,位置);
});

我试图手动导入compute.cclib.MainActivity Inject.java 中。但是调用Activity仍然会给出运行时错误!


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




 <?xml version ='1.0'coding ='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 = 真 >
< 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 =16android:targetSdkVersion =24/>
< / manifest>

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






编辑1



@gmmo建议后,我参考了我的插件的gradle文件中的库( Inject.gradle

  repositories { 
jcenter()
maven {urlhttps://jitpack.io}
flatDir {
dirs'aar'
}
}
dependencies {
compile'c​​om.android.support:appcompat-v7:25.1.0'
compile(name:'cclib-release',ext:'aar')
}

android {
packagingOptions {
不包括'META-INF / NOTICE'
排除'META-INF / LICENSE'
}
}

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

 配置根项目'android'时出现问题。 
>您尚未接受以下SDK组件的许可协议:
[Android支持存储库]。
在构建项目之前,您需要接受许可协议并使用Android Studio SDK Manager完成缺少组件的安装。
或者,要了解如何将许可协议从一个工作站转移到另一个工作站,请访问http://d.android.com/r/studio-ui/export-licenses.html



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

  compile'c​​om.android.support:appcompat-v7: 25.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插件项目中已经涵盖所有这些依赖项后,为什么需要这样做。


What I am trying to achieve:

I have created Android Plugin Project (cclib-release.aar) which has a targeted Activity.

I want to invoke this Activity in ionic2 using Cordova Plugin which has cclib-release.aar added as dependency inside Cordova Plugin. aar file added in cordova plugin as per this link

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. 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)

My Inject.java goes like this

    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

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.

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

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

I tried to manually import compute.cclib.MainActivity in Inject.java. But invoking Activity is still giving run time error!

Below is the modified 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?


EDIT 1

After @gmmo suggested, I included reference libraries in my Plugin's gradle File (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.

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. I am pretty sure this is due to this lines in Inject.gradle

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

Plugin might not able to find the corresponding dependencies

解决方案

Okey finally I figure out the issue.

Ionic2 project need to update SDK tools by the following command

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

Got this input from 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 .

As I was in an impression that why would this be required after all these dependencies was already covered in Android Plugin Project.

这篇关于启动活动驻留在Cordonic插件的Ionic2项目中的aar文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆