1

I have an NDK Android app based on NativeActivity Android sample, and I need to change slightly a behavior of NativeActivity. I cannot do it by subclassing (inheriting), because the thing that I want to change is not exposed.

Therefore, my idea was to take source code of NativeActivity.java, re-work it a bit and then rename it as say MyNativeActivity.java and use it in manifest instead of NativeActivity.

So I took source code for from Android\sdk\sources\android-25\android\app\NativeActivity.java, renamed it as MyNativeActivity.java and inserted into my project. But when I tried to compile it, I got errors:

Error:(179, 41) error: cannot find symbol method getLdLibraryPath()
Error:(200, 70) error: cannot find symbol method getNativePtr()
Error:(295, 59) error: cannot find symbol method getNativePtr()
Error:(301, 61) error: cannot find symbol method getNativePtr()

And I cannot find these getLdLibraryPath() and getNativePtr() in the source code folder (Android\sdk\sources\android-25)

My question is if it's possible to use Android source code in my project, and how to deal with problems like this?

code-glider
  • 239
  • 2
  • 3
Nick
  • 3,205
  • 9
  • 57
  • 108

2 Answers2

1

My question is if it's possible to use Android source code in my project

It depends on the scope and contents of the source code.

But when I tried to compile it, I got errors:

Those are references to native methods. You would need to find the JNI code that implements the functions behind those methods and add that to your NDK project, then modify MyNativeActivity.java to refer to your library.

I do not hold out a lot of hope that this will prove to be practical.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

The mentioned methods are annotated with @hide in the source code which means they are stripped from the Android SDK .jars. They should still be available at runtime. You can call them using reflection, like this:

private static long callGetNativePtrOnInputQueue(InputQueue inputQueue) {

    Class clazz = Class.forName("android.view.InputQueue");
    Method method = clazz.getMethod("getNativePtr");
    Object returnValue = method.invoke(inputQueue);

    if(returnValue instanceof  Long)
        return ((Long)returnValue).longValue();

    return 0;
}

However, these methods are not part of the public API and there's a potential danger of them being removed/renamed in the future.

Vlad Alex
  • 170
  • 1
  • 12