Using Eclipse, how can I build & compile an Android project targeted for 1.5 that uses 1.6+ features?
Probably a simple question but I've developed an app that has always been using a build target of 1.5 without issue. However, now I'm adding TextToSpeech which was introduced in 1.6. I've created a TextToSpeech wrapper that encapsulates the TextToSpeech code and I have checks in the activity so that when it does run, it should only work for 1.6 devices and above. But the trick is to get the code to run in Eclipse with a build target of 1.5. When I try to do that, of course, I get the following errors tied to my TextToSpeech wrapper class:
"TextToSpeech cannot be resolved."
I've played around with exporting as jar, creating a library project, etc. but I cannot seem to get those to work. For a library project, it says that dependent projects must have the same or higher API level so it won't work. Some relevant code excerpts:
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="8"
/>
activity class
static {
try {
TextToSpeechWrapper.checkAvailable();
androidTextToSpeechAvailable = true;
} catch (Throwable t) {
androidTextToSpeechAvailable = false;
}
}
wrapper class
import java.util.HashMap;
import java.util.Locale;
import android.content.Context;
import android.speech.tts.TextToSpeech;
/*
* A wrapper class for the newer Android text-to-speech library that is only found in
* Android OS 1.6 and above (Donut and above). This is useful so that the app can
* be loaded on pre-Donut devices without breaking the app.
*/
public class TextToSpeechWrapper {
private TextToSpeech mTextToSpeech;
// class initialization fails when this throws an exception
static {
try {
Class.forName("android.speech.tts.TextToSpeech");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// Some static vars from 开发者_开发百科the text-to-speech class
public static int SUCCESS = TextToSpeech.SUCCESS;
public static int QUEUE_FLUSH = TextToSpeech.QUEUE_FLUSH; <--- Eclipse errors point here cause it does not exist in 1.5.
So my question is how do I make the project use a build target of 1.5 again w/ the emulator so I can test and feel confident my app still works for 1.5 and above?
If your purpose is to test your app on a 1.5 device/AVD, the you could try to build it against Android 1.6 (API level 4) and set minSdkVersion=3 in your manifest. I'm not sure, but I think you should be able to run it on an Android 1.5 (API level 3) device this way. If this doesn't work, I have another solution, but it's a bit more complicated so let me know if you need it and I'll explain it to you.
But the trick is to get the code to run in Eclipse with a build target of 1.5.
That is not possible. You need to build to at least the minimum API level you are calling.
So my question is how do I make the project use a build target of 1.5 again w/ the emulator so I can test and feel confident my app still works for 1.5 and above?
You don't. You use a build target of 1.6 or higher, then test the app on an Android 1.5 device/emulator.
What Mark said. Use a build target of 4 (Android 1.6) not 8, and a minSdk of 3 (Android 1.5). You'll still have to do the kind of checking that you're doing so you gracefully handle the 1.5 devices that don't have TTS. You can't simply include a jar file in 1.5 because TTS needs so many other components that are not in the jar file you tried.
精彩评论