问题描述
我创造我自己的Android JAR文件的工作,这个JAR将包含我需要打电话,但是我不断收到一个错误NoClassDefFound活动。只有包含在JAR与活动的问题,任何其他对象从外部JAR做工精细调用。
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.
下面是创建这个JAR时候我已经采取的步骤
Below are the steps I have taken when creating this JAR
我创建了一个新的库项目名为LibraryTest,并放置一个活动在它所谓LibraryActivity,其中有一个名为librarymain.xml用一些随机文本的基本布局。
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.
我标志着这个项目作为一个库,bin文件夹中检索到的JAR,然后我创建了一个名为TestAddLibrary新项目。我添加了JAR文件到lib文件夹,然后我从LibraryTest项目的TestAddLibrary项目的布局文件夹中添加的librarymain.xml。
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.
然后我加入了LibraryActivity到新项目的清单。清单看起来像这样
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);
}
});
}
}
当我点击按钮启动存储在JAR文件的LibraryActivity,我得到这个错误
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)
我已阅读StackOverflow上对这个问题,但没有我看过帮助了几个问题。我有ADT Eclipse插件的最新版本,我已经确定了订单与出口下,我已经打勾Android的依赖和私人图书馆。
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.
任何人有任何的想法是什么原因造成这个问题?任何帮助将是非常美联社preciated !!
Anyone have any idea what is causing this issue? Any help would be much appreciated!!
编辑:忘了提我做的这是我需要我的code被隐藏,所以我可以分享这一点给其他人,没有他们看到我的code
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
推荐答案
您不能打包资源转化为jar文件。
You cannot package resources into jar files.
您可以打包不参考任何资源罐子纯java文件。
You can package pure java files that do not refer to any resources as jars.
在你的情况,你需要引用库项目在你的Android项目。
In your case you need to reference the library project in your android project.
库项目
这些项目包含可共享的Android源$ C $ c和资源,你可以在Android的项目中引用。当你有要重用公共code这是非常有用的。库项目不能被安装到一个设备,但是,他们在构建时被拉进.apk文件。
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.
要在注释的问题最接近的资源可以找到
To the question in the comment the closest resource could find
您不能将库项目导出到一个JAR文件
You cannot export a library project to a JAR file
一个库不能分发为二进制文件(如JAR文件)。这将在SDK工具的未来版本中加入。
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要为Android库项目创建JAR
这篇关于Android的使用活动在罐 - 的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!