3つのアプリの共有コードとリソースを使用して2つのAndroidライブラリプロジェクトを作成しました。ライブラリの1つ(ライブラリAと呼びます)には、3つのアプリすべてで共有されるコードとリソースがあり、もう1つには、3つのうち2つだけで共有されるコードとリソースがあります(ライブラリBと呼びます)。
そこで、ライブラリBをAに依存させました。私が抱えている問題は、ライブラリBに依存する2つのアプリにあります。最初のアクティビティを開始すると、ライブラリBで定義されたxmlレイアウトの要素にアクセスしようとするとNoSuchFieldErrorsまたはNullPointerExceptionsが発生します。 。ライブラリBのスーパークラスのリソースが見つからないようです。問題を再現する小さな例を作成しました。
ライブラリ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