32

I'm trying to import some javax.* classes in my android app, but I keep getting compiler errors inside Android Studio. I'm using Ubuntu Linux 13.04.

Here are my imports:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
// Here are the imports I am having trouble with:
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;

And when I try to run the app or even compile the activity java file, I get these errors:

Gradle: error: package javax.sound.sampled does not exist
Gradle: error: package javax.sound.sampled does not exist
Gradle: error: package javax.sound.sampled does not exist

I just can't figure out this problem. Any help would be awesome!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tux
  • 1,896
  • 3
  • 19
  • 27
  • Have you checked your classpath? make sure you have added your System Library. – Smit May 28 '13 at 23:55
  • How would I add in the System Library? Thanks for your help, by the way. – Tux May 29 '13 at 00:04
  • I am not sure for Android Development. But here is path from Eclipse perspectives.... Rightclick on Project Explorer --> Build Path --> Configure Build Path --> Library tab --> Add Library button --> Select JRE System Library --> Next --> Finish – Smit May 29 '13 at 00:10
  • What are you trying to use the audio classes for? – SimonC May 29 '13 at 00:19
  • @SimonC I'm generating a sine wave tone. – Tux May 29 '13 at 00:21
  • 1
    Looks like what you want to do can be achieved using the Android `AudioTrack` class. See: http://stackoverflow.com/questions/10158409/android-audio-streaming-sine-tone-generator-odd-behaviour and http://stackoverflow.com/questions/11436472/android-sine-wave-generation – SimonC May 29 '13 at 00:45

5 Answers5

40

Be aware that when you run an Android app: you don't run/compile it on a standard JVM/JDK, you don't even execute java bytecode. Google choose the java language (or at least a subset of it) as the language to do Android development, but it's only the language.

At the end the compiled java code is not java bytecode, but this is dalvik bytecode. (there is no .class files, but .dex files)

So, when doing Android development: you cannot use the full JavaSE API: you are limited to the API supported by the dalvik VM (available here).

(Note that when you browse this API beware of the version in the top right corner of the page : Added in API level X. It informs you about the Android-API version supporting that class or method)

ben75
  • 29,217
  • 10
  • 88
  • 134
  • Just an aside, you mentioned that "ou cannot use the full JavaSE API". However, I believe the dependency in question here is specifically a JEE, not Java SE. – Chris Maggiulli Sep 04 '19 at 17:27
  • 3
    I wish this answer stated how to get javax to work and not simply stating why it doesn't. We already know typing import javax won't work, so there is no new information here. It seems to me that there must be a workaround or alternative solution. – NL23codes Dec 07 '19 at 01:42
8

Those classes are not included in the Android library. You have to use the Android specific sounds APIs. See: http://developer.android.com/reference/android/media/package-summary.html

SimonC
  • 6,590
  • 1
  • 23
  • 40
5

I had the same problem. I've resolved it by downloading sources of OpenJDK (for example, from here: http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/javax/sound ) and exporting the required package into *.jar file to include that file into android app. It worked just fine for me.

1

If you download OpenJDK from the Red Hat Developer Program (free membership), you can find the .java source files for the entire OpenJDK including javax.sound.sampled. I was able to debug my Android app using OpenJDK8.

Please note, not all OpenJDK8 features are available in Android yet so only include the classes you need. For more information, see https://developer.android.com/studio/write/java8-support.html

-3

I have been working on a javax.sound problem in Android for two days. I took the easy way out because using javax.sound would mean creating a jar file of the libs I needed and if there was C code in JNI under them, I would have to port that to arm. Or I could take an arm Linux dist and bring what I needed for mp3 to wav conversion from there into the JNI. You can use regular jar files in Android. I use an off-the-shelf FTP library from a third party for FTP Juju. My mp3s were not very big. So I just converted them into wavs in res/raw. But the above solutions are available to you.

R Earle Harris
  • 985
  • 9
  • 17