0

I am working on creating my own Android JAR file, this JAR will contain Activities which I need to call however I keep getting a NoClassDefFound error. It is only an issue with Activities contained in the JAR, any other objects called from the external JAR work fine.

Below are the steps I have taken when creating this JAR

I created a new library project called LibraryTest, and placed one Activity in it called LibraryActivity, which has a basic layout called librarymain.xml with some random text.

I have marked this project as a library, retrieved the JAR from the bin folder, I then created new project called TestAddLibrary. I added the JAR file to the lib folder, I then added the librarymain.xml from the LibraryTest project to the layout folder of the TestAddLibrary project.

I then added the the LibraryActivity to the manifest of the new project. Manifest looks like this

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testaddlibrary.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
             android:name="com.example.librarytest.LibraryActivity"
             >
         </activity>
    </application>

</manifest>

Then in the main activity of the new project, I have this

package com.example.testaddlibrary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.librarytest.LibraryActivity;

public class MainActivity extends Activity
{
    Button but;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        but = (Button) findViewById(R.id.butNext);

        but.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(MainActivity.this, LibraryActivity.class);
                startActivity(i);
            }

        });
    }
}

When I click the button to launch the LibraryActivity which is stored in the JAR file, I get this error

07-10 09:20:24.798: E/AndroidRuntime(28195): FATAL EXCEPTION: main 
07-10 09:20:24.798: E/AndroidRuntime(28195): java.lang.NoClassDefFoundError: com.example.librarytest.R$layout 07-10
09:20:24.798: E/AndroidRuntime(28195): at com.example.librarytest.LibraryActivity.onCreate(LibraryActivity.java:20)
07-10 09:20:24.798: E/AndroidRuntime(28195): at android.app.Activity.performCreate(Activity.java:5104) 07-10
09:20:24.798: E/AndroidRuntime(28195): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-10 09:20:24.798: E/AndroidRuntime(28195): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-10 09:20:24.798: E/AndroidRuntime(28195): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-10 09:20:24.798: E/AndroidRuntime(28195): at android.app.ActivityThread.access$600(ActivityThread.java:141) 07-10
09:20:24.798: E/AndroidRuntime(28195): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-10 09:20:24.798: E/AndroidRuntime(28195): at android.os.Handler.dispatchMessage(Handler.java:99) 07-10
09:20:24.798: E/AndroidRuntime(28195): at android.os.Looper.loop(Looper.java:137) 07-10 09:20:24.798:
E/AndroidRuntime(28195): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-10
09:20:24.798: E/AndroidRuntime(28195): at java.lang.reflect.Method.invokeNative(Native Method) 07-10
09:20:24.798: E/AndroidRuntime(28195): at java.lang.reflect.Method.invoke(Method.java:511) 07-10 09:20:24.798:
E/AndroidRuntime(28195): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 09:20:24.798: E/AndroidRuntime(28195): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-10
09:20:24.798: E/AndroidRuntime(28195): at dalvik.system.NativeStart.main(Native Method)

I have read several questions on StackOverflow regarding this issue but nothing I have read has helped. I have the latest version of ADT Eclipse plugin, I have made sure that under Order and Export, I have ticked Android dependencies and Private Libraries.

Anyone have any idea what is causing this issue? Any help would be much appreciated!!

Edit: Forgot to mention other reason I am doing this is that I require my code to be hidden so I can share this out to other people without them seeing my code

AdamM
  • 4,400
  • 5
  • 49
  • 95
  • you cannot package resource files into jar. since your activity refer's to resources in resources file its not possible to package into jar. you need to refer the library project in your android project just like the google play services library project – Raghunandan Jul 10 '13 at 08:35
  • How do I prevent others from seeing my code then? I got it working, by adding the LibraryTest the same way as the Google Play services pack, but then all my code is visible, and I need to ensure my code is hidden when sharing this project out to other people – AdamM Jul 10 '13 at 08:49
  • 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.** – Raghunandan Jul 10 '13 at 08:57
  • The reason I am doing this is that I have a large framework I have developed, and I wish to convert this into a JAR so I can quickly put it into other projects and that I can also give it out to other developers who will be using the framework. The framework uses several resources such as layouts as images. I need to be able to turn this into a JAR file or a Library in which the code I have created is hidden. Using a Library project resolves the NoClassDefFound error issue, but my code is visible. Is there anyway I can share this project out with resources and ensure my code is protected? – AdamM Jul 10 '13 at 09:03
  • i am not aware of it. i read some one on so which is close to your requirement. it was answered by commonsware. i will see if i can post the link here. – Raghunandan Jul 10 '13 at 09:05
  • As you answered the noClassDefFoundError issue I was having, if you put that in the answer section, I will mark as accepted, my issue regarding ensuring code is hidden will have to be asked in a separate question as I am beginning to realize it will probably be a lot more complicated. Thanks for your time! – AdamM Jul 10 '13 at 09:40
  • adam you want me to answer it so that you can accept? – Raghunandan Jul 10 '13 at 09:52
  • Yeah, the initial question I asked here was to do with the NoClassDefFOundError issue which you solved. My issue with sharing the Library Project and ensuring the code is obfuscation will have to be asked in a different question as that is a separate issue – AdamM Jul 10 '13 at 09:53
  • adam check this probably close to your requirement http://stackoverflow.com/questions/17063826/how-to-create-jar-for-android-library-project/17063848#17063848 – Raghunandan Jul 10 '13 at 09:59

1 Answers1

2

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    Cheers. Btw figured out how to ship a jar file, library and resources with hidden code. Read here http://blog.sofisoftware.com/post/2011/10/05/Android-Library-projects-and-Jars. – AdamM Jul 10 '13 at 11:22
  • Just a heads up the links in that article don't work, just skip down to the bottom of the article where he lays out how to do it in 2 simple steps. Tested it myself and it works perfectly – AdamM Jul 10 '13 at 11:25