0

As an experiment, and as a way to force myself to understand the ins and outs of how tts works on Android, I thought it might be interesting to copy the source code for the TextToSpeech class into a new class in my project and rename it (and it's package name) to that of my project.

This of course results in many classes getting highlighted in red and needing to be re-attached and brought into my project as well.

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

I was expecting these errors 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.

Here's my code to illustrate:

MainActivity.java:

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 in my "custom" TextToSpeech class (plz just assume this error 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.

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100

1 Answers1

0

I just added those AIDLs to a project and compile them. I've just added both ITextToSpeechService.aidl and ITextToSpeechCallback.aidl and removed imports, modified packages and added ITextToSpeechCallback import to ITextToSpeechService:

import com.android.speech.tts.ITextToSpeechCallback; 

My files were under src/main/aidl/com/android/speech/tts so the package in AIDLs is:

package com.android.speech.tts;
Sina
  • 2,683
  • 1
  • 13
  • 25
  • I don't mean to have started a confusing question... I'll try to edit my question if that is the case. I know it may seem strange/pointless, but the point of my exercise is to *not* import anything from com.android.speech.tts.* , but instead to actually copy and paste the code into my project and rename the package to my own... and hopefully still have it compile/run. – Nerdy Bunz Jan 20 '21 at 22:20
  • I didn't import anything from android itself. I've just added a package named exactly like android packaging. I added AIDLs to my project in a manner they have the exact packaging as in android in case it would be relevant in IPC later. What I've imported is actually the AIDL file I've added myself. You add it with whatever package you like and import ITextToSpeechCallback.aidl into ITextToSpeechService.aidl and remove other imports in both. – Sina Jan 21 '21 at 08:28