Propper java-groovy interoperability: how? - android

I managed to integrate grooid into my plain android project, so it could compile, with all those lint, multidex magic (yes, I fixed 65k limit problem), and successfully run on a device
Then I created a src/main/groovy folder, moved one of my java activities into it and turned it to a groovy class:
package com.mypackage
#CompileStatic
class SplashActivity extends Activity {
void onCreate( Bundle savedInstanceState ) {
super.onCreate savedInstanceState
contentView = R.layout.splash
}
}
upon compiling, I get a compile errors like:
:app:compileDebugJavaWithJavac
AuthHelper.java:25: error: cannot find symbol
import com.mypackage.SplashActivity;
^
symbol: class SplashActivity
So, the groovy files must be somehow compiled before compile*JavaWithJavac.
How can this be done?

If you place the java files into the groovy folder the groovy compiler will facilitate joint compilation.
You can also use the skipJavaC flag to get joint compilation as well see https://github.com/groovy/groovy-android-gradle-plugin#only-use-groovyc for more information.

Related

Hard-coding a subset of Android source code (TextToSpeech) into an app

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:
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.
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;

Why an unresolved reference on attempting to access constant values?

The app defines constants in a Kotlin singleton object:
#file:JvmName("APIConstants")
package com.myapp.api
object APIConstants {
const val HTTP_RESPONSE_CODE_NOT_AUTHORIZED = 401
etc....
}
They are then used in another class:
import com.myapp.api.APIConstants.HTTP_RESPONSE_CODE_NOT_AUTHORIZED
etc ...
class API {
private fun returnBadResponse(response: Response<*>, callback: ApiAuthListener<*>) {
if (response.code() == HTTP_RESPONSE_CODE_NOT_AUTHORIZED) {
callback.onBadAuthToken()
} else {
callback.onFailure(response.message(), getServerError(response))
}
}
In this class Android Studio (3.0 beta) provided a hint to add the import for the constant, and it does not give any indication of a problem (no red underlines etc, and the constant reference in the method is shown in purple italic text indicating it has been resolved) but when I build the project I get this:
Error: Unresolved reference: HTTP_RESPONSE_CODE_NOT_AUTHORIZED
I've tried clearing the IDE cache and restarting it, and doing a clean build, which make no difference. I've tried removing the #JvmName annotation and even placing the const values in the root of the file with no containing object but neither allows a build.
Why is the class failing to reference the constant, especially when the IDE strongly suggests it can resolve it?
And the solution is.... to make very sure all Kotlin source files have a .kt file extension! In this case the APIConstants file was called "APIConstants" and not "APIConstants.kt" which appears to mean the IDE was able to resolve references based on the content of the file, but the build tools could not. Confusingly Android Studio showed a Kotlin K icon on the filename despite the lack of a .kt extension.

React Native Sqlite for android configuration

I am playing with react native for android and i have special interest with sqlite.
I try to use the sqlite lib here react-native-sqlite-storage buy i always have BUILD FAILED when "run-android". I'm a little lost at step 4 (How to use Android)
configuring the MainActivity
The error:
E:\Documents\Visual Studio CODE\MyApp\android\app\src\main\java\com\myapp\MainActivity.java:22: error: cannot find symbol
protected List getPackages() {
^
symbol: class List
location: class MainActivity
...
Have somebody a very simple sample project for android to see the correct conf?
thanks in advance.
You are missing import statement for the List.
import java.util.List;
#Override
protected List getPackages() {
//noinspection RedundantArrayCreation
return Arrays.asList(new ReactPackage[]{
new MainReactPackage(),
});
}
Any java IDE has some kind of fix import function so it would automatically propose you correct solution.
Try android studio it's free and react-native automatically generate gradle configuration. All you need to do is to import your project folder with gradle files (android by default) into IDE and press ctrl+space to turn IntelliSense on.
You should change MainAplication.java and not MainActivity.java

Android Source Code Compilation issue

i am working on one of C++ Application, where i need to interface with Android Source Code, i got this Source Code from below URL
https://github.com/android
Now When i am Compiling my application which uses Camera module, then it has many dependencies like utils,binder,gui,system. by this way i am including every header files which camera module of Android depends depends on.
but i am stuck with following error:
In file included from jni/headers/camera/ICamera.h:22:0,
from jni/headers/MyCamera.h:4,
from jni/headers/VideoWrapper.h:4,
from jni/src/com_example_jnitest_VideoJava.cpp:2
jni/headers/binder/Parcel.h:31:7: error: template argument
required for 'class Flattenable'
class Flattenable;
^
jni/headers/binder/Parcel.h:105:37: error: invalid use of
template-name 'android::Flattenable' without an argument list
status_t write(const Flattenable& val);
^
jni/headers/binder/Parcel.h:160:30: error: 'Flattenable' is not a type
status_t read(F
my Question is:How to get out of this error??
Whenever Any class Template need to be pre-declaration in another class or Anywhere then we need to give it with template
e.g:
in Parcel.h file of Android source, pre-declaration of class Flattenable is done as below:
class Flattenable;
but actually it should be:
template <typename T> class Flattenable;
because class Flattenable is Template class.
However i Amazed how this things not taken care into Android Source Code..!!

Monodroid issue with jar binding zubhium sdk

Iam trying to bind a jar(zubhium sdk jar) file to my mono droid project. I have followed their guide on xamarins website and created a new binding project which I reference I my mono droid project.
I am having some issues with package names. I get the following error:
Error 3 package com.zubhium.interfaces does not exist
com.zubhium.interfaces.ZubhiumListener
C:\Users\jbp\Desktop\ny\CmsApp.Droid\obj\Debug\android\src\mono\com\zubhium\interfaces\ZubhiumListenerImplementor.java 8 25 CmsApp.Droid
from the genrated source:
package mono.com.zubhium.interfaces;
public class ZubhiumListenerImplementor
extends java.lang.Object
implements
mono.android.IGCUserPeer,
com.zubhium.interfaces.ZubhiumListener
And that is because when the project is wrapped it automaticly adds mono(.com.zubhium....) to the package name. I cant find how to remove this mono or if i can set a rule to add this part.
I tried to put <attr path="/api/package[#name='com.zubhium.interfaces']" name="managedName">mono.com.zubhium.interfaces</attr>
in the xmldata.xml file but that did not work.
Do you guys have any sugestions?
Regards
package com.zubhium.interfaces does not exist
When you bind a .jar library and reference the binding project from another project, you need to also add the .jar to your Application project and set its Build action to AndroidJavaLibrary.
Failure to do so means that the .jar won't be added to the javac $CLASSPATH, resulting in javac compilation errors when compiling the Android Callable Wrapper (as you saw), and that the .jar won't included into your final .apk. Both of these are Badâ„¢.
And that is because when the project is wrapped it automaticly adds mono(.com.zubhium....) to the package name.
That's for an "Implementor" type; you can ignore it. It's used as part of the implementation of events. If you look at the generated C# code, there should be:
[global::Android.Runtime.Register ("mono/com/zubhium/interfaces/ZubhiumListenerImplementor")]
internal sealed class ZubhiumListenerImplementor : Java.Lang.Object, IZubhiumListener {
// ...
}
The mono.com.zubhium.interfaces.ZubhiumListenerImplementor is the ACW for the internal ZubhiumListenerImplementor type.
You can't rename this type; it's an internal construct that isn't controlled via metadata.

Categories

Resources