1
  1. Main project which depends on a library named 'sf' i made.
  2. In library 'sf',using Intent to start new activeity

    import com.sf.proxy.MainActivity;
    public void login(Activity context) {
        Intent intent = new Intent(context,MainActivity.class);
        context.startActivity(intent);
    }
    

I got an exception saying:

    02-06 10:07:58.198: D/AndroidRuntime(9770): Shutting down VM
    02-06 10:07:58.198: W/dalvikvm(9770): threadid=1: thread exiting with uncaught exception (group=0xa6251288)
    02-06 10:07:58.198: E/AndroidRuntime(9770): FATAL EXCEPTION: main
    02-06 10:07:58.198: E/AndroidRuntime(9770): java.lang.IllegalStateException: Could not execute method of the activity
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.view.View$1.onClick(View.java:3591)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.view.View.performClick(View.java:4084)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.view.View$PerformClick.run(View.java:16966)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.os.Handler.handleCallback(Handler.java:615)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.os.Handler.dispatchMessage(Handler.java:92)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.os.Looper.loop(Looper.java:137)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at java.lang.reflect.Method.invoke(Method.java:511)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at dalvik.system.NativeStart.main(Native Method)
    02-06 10:07:58.198: E/AndroidRuntime(9770): Caused by: java.lang.reflect.InvocationTargetException
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at java.lang.reflect.Method.invoke(Method.java:511)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     at android.view.View$1.onClick(View.java:3586)
    02-06 10:07:58.198: E/AndroidRuntime(9770):     ... 11 more
    02-06 10:07:58.198: E/AndroidRuntime(9770): Caused by: java.lang.NoClassDefFoundError: com.sf.proxy.MainActivity

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample">

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true" />
    <application
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.sf.proxy.MainActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:screenOrientation="sensor"
            android:theme="@android:style/Theme.Translucent.NoTitleBar">
        </activity>
   ......

I know what this exception means so first I looked inside jar file, and the "missing" class with exactly exist.Anyone else experienced this? Thanks in advance.

timestee
  • 1,086
  • 12
  • 36

2 Answers2

0

You cannot package resources into jar files.

You can package pure java files that do not refer to any resources as jars.

In your case you need to reference the library project in your android project.

Library Projects

These projects contain shareable Android source code and resources that you can reference in Android projects. This is useful when you have common code that you want to reuse. Library projects cannot be installed onto a device, however, they are pulled into the .apk file at build time.

http://developer.android.com/tools/projects/index.html

To the question in the comment the closest resource could find

You cannot export a library project to a JAR file

A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools.

How to create jar for Android Library Project

Also, have a look into below url.

http://blog.sofisoftware.com/post/2011/10/05/Android-Library-projects-and-Jars

Community
  • 1
  • 1
Rikin Prajapati
  • 1,913
  • 2
  • 16
  • 23
  • i did not package any resource into jar, just source code, and the exception saying 'java.lang.NoClassDefFoundError', not about resource. – timestee Feb 06 '17 at 10:23
0

My sf jar depends on android-support-v4.jar. Main project depends on sf, Add android-support-v4.jar to Order and Export.

Solved.

timestee
  • 1,086
  • 12
  • 36