- Home /
Running native code in OnCreate() Method - Android
Hi guys, so I'm trying to integrate Amazon Ads SDK into my Unity project for Android. I feel like I'm missing something basic. When I export my Unity project to an Android Project, I can launch and run my application just fine. However, any code that I put into the OnCreate() method of NPUnityPlayerActivity does not get called. Even leaving all the code as is and adding a simple Log.V() message doesn't get printed.
When I create a brand new Android project, I can call code in the OnCreate() method and get the Amazon Ads to show no problem. What am I missing? It's like the NPUnityPlayerActivity is not actually getting run, but the application runs so it has to be.
I haven't done much coding with the ADT so any help would be appreciated. Thanks!
Answer by liortal · Jul 09, 2014 at 08:03 PM
Where in your project are you placing your custom activity?
Have you read the docs here: http://docs.unity3d.com/Manual/PluginsForAndroid.html
Basically, you need to place your custom compiled java code under Plugins/Android, or, in case of an Android library project, place it under Plugins/Android/ProjName.
The project should be compiled to a .jar (will be looked for under Plugins/Android/ProjName/bin or libs).
Also, in case you implement your own activity, you must:
Derive it from UnityPlayerActivity (not from Activity)
Provide your custom AndroidManifest.xml, so Unity will know which custom activity it should use for the exported APk.
It's not a custom activity, I was just trying to get something...anything to happen for me by placing one line of code into the NPUnityPlayerActivity.
You're saying I am required to make a jar file? What I'm trying to do is only like 10 lines of code so I was just trying to stick it into the existing activity. Guess I will make it a plugin.
Where do you have the source of the existing activity? and where did you place that file ?
Unity will not compile any java code for you. All has to be pre-compiled (e.g: Jar)
Ha alright it's all starting to make sense now I think. When I open the eclipse project there is an NPUnityPlayerActivity class that isn't compiled. I was under the impression I could stick code in there as that's the only activity registered in the manifest. Thinking about it now that is a really dumb way to do things because even if it worked I would have to paste that coed back in every time I generated the eclipse project. I'm going to make a jar. Thanks for your help. I know java but have never worked with android dev tools before so I'm a little lost.
Now I see. You export a project and not an apk ("Google Android project"). Adding code to the activity class should be O$$anonymous$$ then... I just tried it now and i see my Log calls from onCreate
Your answer
