Xamarin Linking in Release mode with Proguard - android

I'm working on a Xamarin Forms app for Android and iOS. We use a third-party SDK that comes as an .aar package, build action set to LibraryProjectZip. Then, we have calls from the forms, the iOS, and the Android parts of the code. Linking works just fine in debug mode if I remember correctly.
I'm failing to get the Linker+ProGuard work together. I've read for days and experimented with numerous solutions. I was having java codes 1 and 2 - so I updated ProGuard and it started working. Then Linking was producing errors - UnauthorizedAccessException so I had to go to the respective folder and remove the ReadOnly permission. Then the project is built successfully but the app functionality is lost even though it runs and in Device Log I can see that some classes are missing.
To fix that, I created a ProGuard.cfg and added most of the assemblies that were crashing with the "-keep" command. However, I can't seem to find a way to preserve the functionality from this .aar package
Any suggestions?
I'm using VS 2017 and Xamarin 7.4 on Windows 10 with JDK 1.8, running as Administrator. Tried linking Sdk Assemblies / User&Sdk Assemblies. Multidex is on. I've tried putting the names of the assemblies of all the projects in the Skip Linking Assemblies field, no results. In the device log, I get a package name for the call that is failing so I tried adding this to ProGuard too but it didn't help. (it looked like "-keep class com.something.something")

I can't comment, but this blog post may be of use.
"The Linker Analyzer is a command line tool that analyzes dependencies which are recorded during the LinkAssemblies step. It will show you what items were marked and resulted in the linked assembly."
https://www.jon-douglas.com/2017/09/22/linker-analyzer/

Related

Xamarin android Manifest being replaced on build. only solution seems to be to repair visual studio

Im not really sure how to being this... this started a couple months ago every so often i would receive apk install failed errors and when i unzipped my built apk and looked at the android manifest mine was replaced with a android manifest from a sample serial port library i was testing with a while back. This problem has become constant to the point where every 3 builds or so i have to use the visual studio installer to repair visual studio. Cleaning the solution doesn't fix it nor does deleting my local visual studio files or bin/obj folders. I have cloned my solution over to my personal desktop and it built fine and has never exhibited these problems so i believe its environment specific. if anyone has run into a similar problem or has any suggestions i would greatly appreciate it.
With Xamarin.Android some of the AndroidManifest.xml file is generated at build time.
All types which have [Application], [Activity] or [Service] attributes and their extra stuff like [IntentFilter], [MetaData] will be added to the manifest at build time.
You can verify this by comparing Properties/AndroidManifest.xml to the one generated which usually can be found in the obj/Debug/Android folder. You should see that your Activities are added there.
If you use the Xamarin.Android.ManifestMerger package, additional content from Android Libraries might be added to the manifest as well
I found the problem! the newest non preview xamarin android will do this if you have a bound jar . here is a link to the github issue this should be fixed in future versions for now im updating to the preview visual studio.
https://github.com/xamarin/xamarin-android/issues/4804

Unable to build project with both Firebase and Mapbox

I have created a location-based game using the Mapbox SDK in Unity 2018.2.1f1, and it has been able to build just fine. My goal was to add database functionality to my project by using Firebase, to store GPS coordinates online.
I followed the official tutorial for setting up Firebase in a Unity project. When this was completed, a window popped up saying "Resolving Android Dependencies", which took about half an hour.
Since then, I have been unable to build to my Android device. I receive 96 gradle build error, and the one at the top is:
> No toolchains found in the NDK toolchains folder for ABI with prefix:
mips64el-linux-android"
I reverted the project to its pre-Firebase version, and it was still unable to build, as if something in the Android build tools had been changed.
Next, I tried an entirely different version of Unity, and installed a fresh install of Unity 2019.2.9f1. I opted to use this version of Unity's in-build Android SDK and NDK. I reverted my project back to the last known working version (pre-Firebase), ported it to the new version of Unity, and completed a successful build to my device before moving on.
Then I followed the appropriate steps to add Firebase to my project again, and the same exact pop-up windows appeared in Unity, claiming that it detected "obsolete files" that it wished to delete (this time I said no), and then it performed the same "Resolving Android Dependencies" process.
Once this process was complete, I made no other changes, and tried to build again. It failed, and I received the same 96 gradle build errors. This confirms this error as repeatable on two different versions of Unity.
I believe this has something to do with conflicting dependencies between Mapbox and Firebase. Has anyone encountered this issue, or successfully been able to use these two things together in a project?
So, the baseline problem is that something is telling the Android build process to generate project files for the MIPS architecture. This architecture was removed from the official NDK in r17 (see this link for supported architectures). I'm not sure if Unity ever supported it, but it's not in the current version:
I do not know how MapBox is packaged and shipped, so I don't exactly know what postprocessing hooks might be conflicting between that and Firebase. I'll try to give you some steps forward though.
First, make sure you have the latest SDK from Firebase and MapBox. Also, when it asks to delete obsolete files, say yes. The Play Services Resolver should only prompt this for files that it tracks (and the Firebase plugin ships in a state where this is expected).
If that doesn't help, look for a mips folder included in your Unity project (probably something like libs/mips or jnilibs/mips). Hopefully deleting these will fix the issue.
If the above doesn't work or is non-actionable, try using a mainTemplate.gradle file:
If you follow the instructions I wrote up here to instruct the Play Services Resolver to patch your mainTemplate.gradle file:
You can double check your ABI filters directly in the file. By default it shows:
ndk {
abiFilters **ABIFILTERS**
}
to match what you've selected in Unity. But you can use this to double check that mips64 isn't included via some other process.
Unfortunately I don't know what else to suggest if neither of the above options work. In that case I'd suggest posting a bit more of the error you're getting. If you can reproduce the error you're having with one of the Firebase sample projects, you can file an issue on that issue tracker directly. Finally, if you don't get an answer here and you do think that it's Firebase related, you can contact the Firebase support as well.
I hope something in my post helps!
--Patrick

Is it possible to debug C/C++ in Android Studio?

I'm writing an Android app that includes C/C++ code that is compiled alongside the Java code. My app is crashing during the execution of the native code.
Is there a way to step through Android C/C++ source and inspect variables as it is possible with Java source?
I am not necessarily looking for a way to step through the source from within Android Studio. That is obviously the ideal solution, however, if I need to use an external tool after starting the app from Studio, that's fine. I want to be able to step through the execution line-by-line and inspect variables.
Android Studio 1.0.2
Android NDK r10d
Edit: Stemming from the immediate response of, "Use Eclipse with ADT plugin", let me add more requirements.
This project started as an Eclipse ADT project. However, I have now migrated to Android Studio. My project is now built using Gradle, and my project directory structure reflects this. I am willing to use Eclipse; I am not willing to change my project structure in order to revert to Eclipse from Android Studio. If there is a way to open the Android Studio project in Eclipse and debug it that way, please elaborate on the process.
[UPDATE]
As of July 2015, Android Studio DOES support NDK.
You can learn more about it on this link.
[OLD]
NDK is not yet supported in Android Studio.
When we have to deal with NDK, the only solution is to use Eclipse.
EDIT
We basically keep Eclipse project with NDK feature (map in our case) and Gradle project with other (non-map) features. So everything that has to be done with NDK, we do in Eclipse and then include changes into Gradle project.
To be more precise, inside directory /src/main/ we created another directory called jniLibs and put the compiled *.so file inside architecture-specific folders (armeabi-v7a, armeabi, x86...). Then in the main class of Gradle project simply added a line
static {
System.loadLibrary("OurNDKLibName");
}
You can add this line only once per project. Maybe I am wrong but we did not have any issues for doing so.
As of version 1.3+ Preview Android Studio supports C++ debugging, quoting Android M Developer Preview & Tools:
Most notable is a much requested feature from our Android NDK & game developers: code editing and debugging for C/C++ code. Based on JetBrains Clion platform, the Android Studio NDK plugin provides features such as refactoring and code completion for C/C++ code alongside your Java code. Java and C/C++ code support is integrated into one development experience free of charge for Android app developers.
It isn't wasn't isn't working very well.
hello-jni fails failed with:
Starting LLDB server: run-as com.example.sample
/data/data/com.example.sample/lldb/bin/start_lldb_server.sh
/data/data/com.example.sample/lldb
/data/data/com.example.sample/lldb/tmp/platform.port1442695333842 "lldb
process:gdb-remote packets"
Error while launching debug server on device:
com.android.tools.ndk.run.DebuggerContext$StartServerException:
java.lang.IllegalStateException: Failed to read platform port
/data/data/com.example.sample/lldb/tmp/platform.port1442695333842
I'm using 1.4RC1
compileSdkVersion 23
buildToolsVersion "23.0.1"
And the "app-native" run configuration #Gerry mentioned. It's nearly there, but I can find no trace of an lldb directory in my app's private data folder. I do find a 400kb gdbserver in the lib directory linked from there, with my own *.so right besides. Unfortunately the option for debugging with gdb instead of lldb has gone with the upgrade from 1.3 to 1.4. NDK 10e ships a 400kb gdbserver binary in ndk\prebuilt\android-arm\gdbserver which it is (I guess). On September 15th the gdb debug howto https://github.com/mapbox/mapbox-gl-native/wiki/Android-debugging-with-remote-GDB was updated. This is harder than logcat debugging IMO, but a current topic, sorry to be so negative but I don't believe lldb actually works yet.
I have run hello-jni and reached the same conclusion.
So in answer to your question, it is not yet possible to debug C/C++ in Android Studio. Sorry this wasn't the answer you hoped for and I hope to be proved wrong with conclusive evidence to the contrary, but right here, right now, no chance!
UPDATE I raised an issue where I was given a fix for one phone.
UPDATE2 Too early to tell, it is experimental after all, but I am occasionally failing to connect, often if not always getting out of order stepping and breakpoints with nonsense parameter values. I just set a conditional breakpoint that didn't fire but the logcat shows it should. Initially I assumed it was my logical error, but it goes deeper.
Simply hitting breakpoints at all is very helpful but it is still a million miles short of Java debugging, or Vis Studio C++ debugging.
UPDATE3 Gone back to stable 1.3.1 as the canaray channel version was pausing for a toilet break every keystroke. While native debug build at least launches with my "fixed" phone, no breakpoints are hit even in hellojni, or a slightly extended version to give more opportunity. I'm used to logcat now but have had better degbugging experiences with Android, NDK and Eclipse. A more elaborate solution is also easier to break, but at least it works.
Yes, Android Studio does support C++ debugging at least with Android studio 1.5.1 and android-ndk-r10e.
In the old days, you will have to write make files Android.mk and Application.mk in order to build the C++ code in Android Studio. You don't need them any more. As long as you put them under the jni folder, gradle will be able to pick it up and compile it. You should see something like the following:
And also allow you to set break point and debug it.
You should follow this step-by-step tutorial and learn more about it. And read more about the documentation here. I've uploaded a complete working example so that you can download and try it out on GitHub.
With Android Studio 1.4 from tools.android.com, debugging is possible. When you open/create a project with C++[jni] code in it, Android studio would create a new configuration, something like $(module_name)-native
I would select this configuration, sync gradle, build, set breakpoint etc and run it with Android Studio. After a long wait, my debugger is connected, app is broken in[I think you could also set breakpoint at this time]; this break is not at my set break points, I let it go in android studio and my breakpoint is hit. Hope this helps
Some samples are supposed to work with android debugger
yes! it is possible to debug native code in android studio. As from the updates Android Studio 2.2+ support in built tools for debugging native code.

Build Configurations and the Android NDK

I've spent the last few days struggling with getting a system up and running for building multiple configurations of our Android NDK App. I am able to build the libraries fine; the issues I run into usually stem from keeping those libraries up to date when changing build configurations.
I need a system that is for the most part fully automated: the user should just change build configurations and everything just works!
How have you addressed build configurations and the issues with libraries not being properly rebuilt?
I've tried a bunch of different approaches to this, but they all seem to end up with issues that are out of my hands.
Initially we just outputted a Shared Object file with the same name regardless of Build Configuration (eg. game.so). The problem is, changing build configurations doesn't trigger the library to be rebuilt; you have to make a change to the source or build scripts. So people change from Debug to Release, run the app, and are unknowingly actually linking to the Debug version still.
So then we tried outputting the library to a unique Shared Object based on the build configuration. For example, the debug version is gamed.so. The release version is gamer.so. The problem is, the Java code has no concept of build configuration, so how do we know which version of the library to load (which is done from Java)?
Well to get around that we do a try/catch looping through all the possible versions of the library attempting to load them. When we find the one that exists, load it! Problem being that the old versions of the library do not get removed when deploying a new configuration to the phone. So if you have built and run both Debug and Release, both of those libraries are now on the phone! Which ever one you ask for first, it will find and load.
So anyway, this seems like something that must have been solved already, but I have found very little information online. Nothing that has met my needs.
My question is: how have you addressed build configurations and the issues with libraries not being properly rebuilt?
This is actually strange. I have always seen that install -r refreshes the /data/data/app.pack.age/lib directory. Which device do you use for development?

Running Android project through Eclipse

I have a problem running my android project through eclipse. It gives following error when I try to run it:
trouble processing "javax/net/SocketFactory.class": ... Conversion to
Dalvik format failed with error 1
I am using Android 2.3.1 API level 9. The android.jar that is downloaded from AVD Manager has the javax/net package inside it.
I have searched on this issue and it seems to be the issue related to conversion of classes into Dalvik. But "javax/net/SocketFactory.class" is part of the android.jar. Any suggestions ?
The problem is resolved. I was mistakenly adding two android libraries at a time. One is added by default and other is added via Maven dependency. Removing the android dependency from pom.xml works. But now it is not working on terminal through mvn install command. This is a separate issue now.
As you know android sdk comes complete with java sdk build in. This sdk is light weight and customized for use by the dalvik engine. Now, if we also add java jar’s along with the android app, obviously the android system will have trouble converting that java jar to dalvik. Hence the “conversion to dalvik” issue.
So now that you know what happens, lets figure out why it happens.
In my case it was the barcode, zxing framework that caused the problem. You have to find out what is causing the issue in your setup.

Categories

Resources