This won't work. The activities that are specified in the AndroidManifest.xml of an apk must be defined in the main classes.dex of that apk.
What context should I use when sending an Intent to an activity in a separate DEX file?
-
21-04-2022 - |
Question
I have an android activity located in a separate dex file that I want to launch from my android app. I get an exception after starting the intent.
Didn't find class "com.example.testproject.SampleActivity" on path: /data/app/com.example.testproject.apk
I built an activity class and let Eclipse put it in a dex file
public class SampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_sample);
setContentView(getIntent().getIntExtra("LAYOUT", -1));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sample, menu);
return true;
}
}
I added that activity to my main apps manifest file
<activity
android:name="com.example.testproject.SampleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I also added the activity_sample.xml layout file to my main apps project so I could pass it via the Intent to the other activity
then from the main app I build an intent to call the other activity as follows
String p = Environment.getExternalStorageDirectory().getPath() + "/classes.dex";
ClassLoader dexLoader = new DexClassLoader(p, getCacheDir().getAbsolutePath(), null, getClassLoader());
try {
Class<?> activityClass = dexLoader.loadClass("com.example.testproject.SampleActivity");
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(getApplicationContext(), activityClass);
intent.putExtra("LAYOUT", R.layout.activity_sample);
startActivity(intent);
finish();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
the class is loaded okay but after calling startActivity() I get the exception noted above. This seems to the a problem with the context of the intent since the exception shows the class is being looked for in /data/app/com.example.testproject.apk which it is not in the apk (the dex file is on the sd card).
Any ideas how to resolve this issue?
Solution