无法使用.aar文件从Cordova插件启动活动 [英] Can't start an activity from a cordova plugin using .aar files

查看:80
本文介绍了无法使用.aar文件从Cordova插件启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Android编写一个简单的cordova-plugin,但是我陷入了崩溃,不知道如何解决.

我基本上按照以下步骤创建了简单的插件,并且效果很好.

http://www.mat-d.com/site/tutorial-creating-a-cordova-phonegap-plugin-for-android-app/

但是,我的插件必须调用打包为.aar文件的活动.基本上,我有一个功能齐全的Java/布局应用程序,需要由插件触发.

我按照以下步骤将.aar文件添加到cordova的android版本中.

Cordova插件开发-添加aar

它也起作用.现在,到目前为止,我已经在插件结构中包含了以下文件(自从我保留了大多数原始名称以来,现在这些文件已经被黑了):

在我的yourFile.gradle中,我有这个:

repositories{    
  jcenter()
  flatDir{
      dirs 'libs'
   }
}

dependencies {
   compile(name:'mylittlelibrary-release', ext:'aar')
   compile 'com.android.volley:volley:1.0.0'
}

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

"CoolPlugin.java"的主要部分如下所示:

public class CoolPlugin extends CordovaPlugin {
    public CoolPlugin() {}
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
    }
    public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                final Context context = cordova.getActivity().getApplicationContext();
                try {
                    Intent intent = new Intent(context, testv.com.mylittlelibrary.MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    cordova.getActivity().startActivity(intent);
                } catch (Exception e) {
                    String errorText = e.toString();
                    Toast toast = Toast.makeText(context, errorText, duration);
                    toast.show();
                }

我的"mylittlelibrary-release.aar"的主要活动在名称空间"testv.com.mylittlelibrary"下,因此MainActivity.java如下所示:

package testv.com.mylittlelibrary;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;

import com.android.volley.*;
import com.android.volley.toolbox.*;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        etc...

我正在阅读下面的线程,看起来我需要以某种方式注册我的aar文件的主要活动.

从CordovaPlugin打开活动

我不太确定该怎么做,所以我进入了自动生成的AndroidManifest.xml,

C:\ cordova \ hello \ 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.example.hello" 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" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/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:name="testv.com.mylittlelibrary.MainActivity">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="23" />
</manifest>

我安装了插件,并使用以下命令运行了该应用程序:

cordova plugin add c:/CoolPlugin/
cordova build android
cordova run android

html文件中有一个按钮触发插件,然后触发我的活动",就像您在"mylittlelibrary-release.aar"文件的"MainActivity"上看到的一样.

但是,我一直在崩溃.我相信android插件无法正确解析名称空间,并引发此异常.

com.example.hello E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hello, PID: 13119
    java.lang.NoClassDefFoundError: Failed resolution of: Ltestv/com/mylittlelibrary/MainActivity;
    at CoolPlugin$1.run(CoolPlugin.java:59)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5835)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)

我也相信与在testv.com.mylittlelibrary下正确注册MainActivity有关.但我不知道如何在科尔多瓦做.我需要解决此问题,因此很高兴为您提供我的声誉,因为它的可靠回答可以阻止我.

谢谢.

解决方案

没关系,这是简单的错误.图书馆错过了一些进口商品和一个主题.这样就解决了这个问题.

清单:

android:theme="@style/Theme.AppCompat.Light"

等级:

dependencies {
   compile(name:'mylittlelibrary-release', ext:'aar')
   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'

I am trying to write a simple cordova-plugin for android, but I am stuck into a crash that I don't know how to resolve.

I basically followed these steps to create simple plugin and it worked fine.

http://www.mat-d.com/site/tutorial-creating-a-cordova-phonegap-plugin-for-android-app/

Yet, my plugin must call an activity that is packaged as an .aar file. Basically I have a full blown application with java/layouts that needs to be triggered by the plugin.

I followed these steps to add my .aar file to the android build of cordova.

Cordova plugin development - adding aar

It also worked. Now, I have so far in my plugin structure these files (admittedly is pretty hacked right now since I kept most of original names):

in my yourFile.gradle I have this:

repositories{    
  jcenter()
  flatDir{
      dirs 'libs'
   }
}

dependencies {
   compile(name:'mylittlelibrary-release', ext:'aar')
   compile 'com.android.volley:volley:1.0.0'
}

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

and the main part of my "CoolPlugin.java" looks like this:

public class CoolPlugin extends CordovaPlugin {
    public CoolPlugin() {}
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
    }
    public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                final Context context = cordova.getActivity().getApplicationContext();
                try {
                    Intent intent = new Intent(context, testv.com.mylittlelibrary.MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    cordova.getActivity().startActivity(intent);
                } catch (Exception e) {
                    String errorText = e.toString();
                    Toast toast = Toast.makeText(context, errorText, duration);
                    toast.show();
                }

My "mylittlelibrary-release.aar" has its main activity under the namespace "testv.com.mylittlelibrary" so the MainActivity.java looks like this:

package testv.com.mylittlelibrary;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;

import com.android.volley.*;
import com.android.volley.toolbox.*;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        etc...

I was reading the thread below, and looks like I need in somehow register the main activity of my aar file.

open an activity from a CordovaPlugin

I am not totally sure how to do this, so I went inside the auto-generated AndroidManifest.xml under

C:\cordova\hello\platforms\android\AndroidManifest.xml

and hacked the activity with the namespace inside as such:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.example.hello" 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" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/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:name="testv.com.mylittlelibrary.MainActivity">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="23" />
</manifest>

I installed the plugin and ran the app using:

cordova plugin add c:/CoolPlugin/
cordova build android
cordova run android

there is button in the html file that triggers the plugin, which then triggers my activity as you see on the "MainActivity" of the "mylittlelibrary-release.aar" file.

Yet, I keep getting this crash. I believe the android plugin cannot resolve the namespace properly and throws this exception.

com.example.hello E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hello, PID: 13119
    java.lang.NoClassDefFoundError: Failed resolution of: Ltestv/com/mylittlelibrary/MainActivity;
    at CoolPlugin$1.run(CoolPlugin.java:59)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5835)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)

I also believe is has something to do with the proper registering the MainActivity under the testv.com.mylittlelibrary. But I can't figure out how to do in in cordova. I need to resolve this, so I am glad to give you points of my reputation for a solid answer that unblocks me.

thank you.

解决方案

nevermind, it was simple mistake. The library missed some imports and a theme. This fixed the issue.

Manifest:

android:theme="@style/Theme.AppCompat.Light"

Gradle:

dependencies {
   compile(name:'mylittlelibrary-release', ext:'aar')
   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'

这篇关于无法使用.aar文件从Cordova插件启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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