Thursday, 21 January 2021

How to use an aidl file from Android source in local project/package?

As an experiment, and as a way to force myself to understand the ins and outs of how tts works on Android, I started by copying the source for the TextToSpeech class into a new class in my project and renaming it (and it's package name) to that of my project.

This, of course resulted in many classes getting highlighted in red and needed to be re-attached.

So I began the process of repeating the same procedure -- copying the source of the missing class into a new class in my project -- which leads to more missing classes within that new class.

I was expecting this to happen, but in theory I was hoping it would be possible to graft the whole android.tts.* "tree" (or whatever parts were necessary in order to make the TextToSpeech class function) into my project.

I think I have almost reached the end, but I've run into a problem:

MainActivity.java: (no errors here, just illustrating the small test class so you understand what I'm doing)

public class MainActivity extends AppCompatActivity {

    // notice that this not android.speech.tts.TextToSpeech class, but a local copy of it.
    MyTextToSpeech tts; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new MyTextToSpeech(getApplicationContext(), new MyTextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i == TextToSpeech.SUCCESS) {
                    begin();
                }
                else {
                    Log.i("XXX", "init failed");
                }
            }
        }, "com.google.android.tts");
    }

    private void begin() {
        Log.i("XXX", "It seems to have initialized!");
    }
}

I've gotten rid of most of the errors (plz just assume this is the only one remaining), but there is this one class that I can't seem to pull into the project: ITextToSpeechService:

enter image description here

Apparently this is a hidden/internal "aidl" file the source of which is here.

I tried pasting it in a new folder named "aidl" (following this answer) and it "worked" in the sense that it existed in the aidl folder without errors, but when trying to compile it using gradle\tasks\other\compileDebugAidl, I got this error:

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:compileDebugAidl'.

1 exception was raised by workers: java.lang.RuntimeException: java.lang.RuntimeException: java.io.IOException: com.android.ide.common.process.ProcessException: Error while executing process /Users/boober/Library/Android/sdk/build-tools/28.0.3/aidl with arguments {-p/Users/boober/Library/Android/sdk/platforms/android-29/framework.aidl -o/Users/boober/Desktop/SCRAP/TTSGraft/app/build/generated/aidl_source_output_dir/debug/out -I/Users/boober/Desktop/SCRAP/TTSGraft/app/src/main/aidl -I/Users/boober/Desktop/SCRAP/TTSGraft/app/src/debug/aidl -I/Users/boober/.gradle/caches/transforms-2/files-2.1/1cd7eff88f5e86d0249af2958bf465f0/core-1.1.0/aidl -I/Users/boober/.gradle/caches/transforms-2/files-2.1/929ec10604c047a76453df3294ada6ae/versionedparcelable-1.1.0/aidl -d/var/folders/vz/d68y33c14pjc9m2vrkqp2kvc0000gn/T/aidl4956045951058849495.d /Users/boober/Desktop/SCRAP/TTSGraft/app/src/main/aidl/com/booberbunz/ttsgraft/ITextToSpeechService.aidl}

... and it still would not resolve within MyTextToSpeech.java.

How to get this file into my project locally so that I no longer have the "can't resolve error" in red as seen in image?

And as an aside question: is this actually not possible to do -- to hardcode tts into a project?

Thanks.



from How to use an aidl file from Android source in local project/package?

No comments:

Post a Comment