Issue
I've created two android library projects with shared code and resources for 3 apps. One of the libraries (I'll call it library A) has code and resources that are shared by all 3 apps and the other has code and resources that are shared only by two of the three (let's call it library B).
So I made library B depend on A. The problem I'm having is with the two apps that depend on library B. When starting the first Activity I get NoSuchFieldErrors or NullPointerExceptions when trying to access the elements in a xml layout defined in library B. It seems that it can't find the resources from the Super class in labrary B. I've created a small example that reproduces the problem.
In library A:
AActivity.java:
public class AActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doStuff();
}
protected void doStuff() {
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlib"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testliba.AActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In library B:
BActivity.java:
public class BActivity extends AActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main_b);
super.onCreate(savedInstanceState);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage(getResources().getString(R.string.buttonText));
}
@Override
protected void doStuff() {
Button b = (Button) findViewById(R.id.button1);
b.setText("I'm a button");
}
}
*res/layout/activity_main_b.xml:*
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="56dp"
android:text="@string/buttonText" />
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlib"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testlibb.BActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In app that depends on library B:
MainActivity.java:
public class MainActivity extends BActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Contents specific for this Activity
}
}
In the gen directory of the application I find three R.java generated:
- com.example.testapp
- com.example.testlibb
- com.example.testliba
and id.button1 is present both in the R.java in testapp and testlibb and they have the same value in both. Still at runtime it's not found.
Thanks in advance
Solution
The problem turned out to be that the AndroidManifest.xml in both library projects had the same default package
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlib"
this caused problems when generating R.java. So to fix the issue I just had to change the default packages of the library projects so that they differed from eachother:
In library A
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testliba"
And in library B
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlibb"
Answered By - Simon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.