Fatal Crash when using Unity to call startMoniting from AAR file - unity3d

I use the android-beacon-library-2.17.1.aar to make an Android App and detect the ibeacon's major/minor numbers. When I call beaconManager.startMonitoring(new Region("myMonitoringUniqueId", null, null, null)); the App runs well. And I make the program I design to the classes.jar file.
Now I use Unity and try to use this jar file to detect the ibeacon on my Unity Android Application. I put the classes.jar and AndroidManifest.xml in to my Unity project and try to call the beaconManager.startMonitoring(new Region("myMonitoringUniqueId", null, null, null)); the compile and Build are passed, but when I install the apk on my phone and run it, the application crash.
The Phone I use: Zenfone 7 -- Android 11
The version of Unity: Unity2019.4.31f1
The error log:
java.lang.NoSuchMethodError: No virtual method startRangingBeacons(Lorg/altbeacon/beacon/Region;)V in class Lorg/altbeacon/beacon/BeaconManager; or its super classes (declaration of 'org.altbeacon.beacon.BeaconManager' appears in /data/app/~~lOm13WWD8uZ_nbS2pIVb3A==/com.example.aartesting-LqhAFNQZtBYPkoICvZt1ig==/base.apk)
It's sorry that my English is not very well, and I afraid that I can't describe my problem clearly. If you want to know more about my problem, please let me know, I will try to make you know more about the situation I met.
Thank you!

I suspect the problem is caused by R8 or ProGuard settings, which obfuscate code to prevent reverse engineering. Aggressive R8 or ProGuard settings will rename certain classes and methods (including library class methods like startRangingBeacons mentioned in the stack trace).
You can read more about how R8 and ProGuard work here: https://developer.android.com/studio/build/shrink-code
To confirm this is the problem, you should edit your project settings for Android to disable R8 and ProGuard. Doing so for a native Android project is as simple as editing the build.gradle file and setting minifyEnabled false. Since I am not a Unity expert, I cannot tell you how to do the same thing from Unity.

Related

Unity Asset Bundles Google Play Asset Delivery - Proguard Minification

I am having problems configuring Proguard to prevent minification of java libraries in the Google Play Asset Bundles and Asset Delivery Plugins when building a .aab using the Google Android App Bundle Unity Editor tool, using the following configuration:
Build Settings:
Create Symbols.zip - Debugging
Compression Method - LZ4
Project Settings - Player - Other:
API minimum - 5
Target API - 33
Scripting Backend - IL2CPP (.NET Standard 2.1)
IL2CPP Generation - Fastest
ARMv7 & ARM64
Strip Engine Code - true
Publishing Settings:
Custom Proguard file
Minify:
R8 - false
Release - true
The custom Proguard file in Assets\Plugins\Android:
-keep public class com.google.play.core.**{
public *;
}
-keep public class com.google.play.common.**{
public *;
}
-keep public class com.google.play.appbundle.**{
public *;
}
-keep public class com.google.play.assetdelivery.**{
public *;
}
-keepattributes *Annotation*
-dontobfuscate
This is linked form the mainTemplate.gradle build file.
Building this with the Google Editor Plugin continues to strip out both Methods and Interfaces resulting in the app crashing on start-up. I have tested the above Proguard by introducing each element which corrects some issues, but is still removing Interfaces from play.core.
Note: I have not included Stack Traces as they are pointing to specific methods and Interfaces in the Plugin libraries which were incrementally fixed by adding each exception to the Proguard file above, but is still not preserving all functions.
I have followed multiple other threads stating the same issue which suggested - deleting the library folder, using custom .gradle, downgrading all of the Google Plugins (1.7.0 release). This had previously worked using R8, but I have not been able to produce a working build since the new updates.
I have also tried multiple combinations of builds but continue to get the same issue to varying degrees of what is and is not kept, all builds cause the app to crash.
Any help in the Proguard configuration or suggestions in build configuration would be greatly appreciated, as this seems to be a popular topic with no clear solution. I am trying not export the project and build in Android Studio as that would be a significant break from our current build process.
If there is any more information I can provide please let me know.
Thanks everyone.
UPDATE: The above issue is not to do with Proguard settings.
Copying in the Proguard settings from the google.play. plugins in to a proguard-user.txt file did not fix the issue as the files were not being correctly handled during gradle build.
R8 minification was not enabled and should have been. The underlying issue is conflicts in class dependencies in the google.play plugins. Specifically;
Assets/GooglePlayPlugins/com.google.play.assetdelivery 2.0.0
Assets/GooglePlayPlugins/com.google.play.core 1.10.3
This is a documented issue in play-unity-plugins for google;
https://github.com/google/play-unity-plugins/blob/master/README.md#play-core-conflicts
However, their fix in the link is proving difficult to implement as I am unfamiliar with gradle and using local maven repositories, and difficulties with EDM4U build patching.
I have started a new thread on the issue;
Unity google.play.core dependency conflict with EDM4U maven local repository build error

FB.Init() error during gradle build unity

I am using Unity 2018.1.6f1 and facebook-unity-sdk-7.13.0. When I try to build the project using gradle, the build is completed successfully but it gives an error while calling FB.Init()
"AndroidJavaException: java.lang.ClassNotFoundException:
com.facebook.unity.FB".
I tried the solution in the following links:
Android ProGuard settings for Facebook
I even checked whether I was using debug keyhash, which I was, but I addded release key hash and still getting the same error.
Any help would be appreciated.
P.S. It works fine in debug build
On Android publishing settings section (in the place you set the keystore stuff), there is a checkbox for User ProGuard file (at least if you are on Gradle). If you check it, Unity will make such file and put it in the project.
Then, on the proguard file, I added:
-keep class com.facebook.** { *; }
-keepattributes Signature
And that seems to be all!
Then I see the FB SDK initing properly both on Debug and Release:
Hope it works for you as well.

Error in Build After InAppPurchase Unity

CommandInvokationFailure: Unable to merge android manifests. See the Console for more details.
C:/Program Files/Java/jdk1.8.0_161\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="C:/Users/Sabasoft Developer/Downloads/tools_r25.2.3-windows\tools" -Dfile.encoding=UTF8 -jar "D:\Unity\Editor\Data\PlaybackEngines\AndroidPlayer/Tools\sdktools.jar" -
stderr[
]
stdout[
Warning: [Temp\StagingArea\AndroidManifest-main.xml:12, D:\unity projects\Pizza maker\Temp\StagingArea\android-libraries\GooglePlay\AndroidManifest.xml:3] Main manifest has but library uses targetSdkVersion='24'
]
exit code: 1
Update Java SdK
Update Android SdK
Check target in menifest file and Unity editor. Both should be same.
Remove duplicate .jar under plugins folder if there are any.
You are using a library somewhere in your code (possibly something for GooglePlay?). The library has a manifest.xml file that is likely located in a folder called plugin or one of its subfolders.
The manifest is the file used by android to describe the app: how it starts, what permissions it has, and so on.
Any android project in unity will include a default manifest.xml file. This file is edited by unity with some info relative to you game (the name, and the target version, are part of it).
Now, the libraries you include in your project might need different options and values in the manifest, so Unity libraries for android usually come with another manifest.xml file. This will will be automatically merged with the default one by unity at build time.
Usually, it goes well, the additional line in the manifest from the library get added to the default manifest.
But in your case, there is a conflict between these manifests. The target version is specified both in the library and in your project. So unity can't resolve it.
To fix this, use a target version for your project that is the same or higher than the one of the library. Edit your target version under player settings

Unknown globals when installing ZXing Scanner

I have a Xamarin.Forms project which needs a QR code scanner. I found ZXing Scanner which seems to be a well established library for such purposes.
I installed it in the corresponding android project which worked without errors. When I wanted to build the app, Resource.Designer.cs was adjusted with the following lines:
global::ZXing.Mobile.Resource.Id.contentFrame = global::my.project.Droid.Resource.Id.contentFrame;
global::ZXing.Mobile.Resource.Layout.zxingscanneractivitylayout = global::my.project.Droid.Resource.Layout.zxingscanneractivitylayout;
global::ZXing.Mobile.Resource.Layout.zxingscannerfragmentlayout = global::my.project.Droid.Resource.Layout.zxingscannerfragmentlayout;
The problem is that I as well get the following errors:
'my.project.Droid.Resource.Id' does not contain a definition for 'contentFrame'
'my.project.Droid.Resource.Layout' does not contain a definition for 'zxingscanneractivitylayout'
'my.project.Droid.Resource.Layout' does not contain a definition for 'zxingscannerfragmentlayout'
I installed ZXing using NuGet but I as well tried to add the dlls manually. I get the same errors. Can anyone help me how to fix this?
Thank you in advance.
It's quite possible that your Resource.Designer.cs is not updating correctly:
Clean Project Build
Remove ZXing package.
Clean Project Build
Add ZXing package
Clean and Build Project once again
If it won't help:
https://kb.xamarin.com/customer/portal/articles/1638018-my-android-resource-designer-cs-file-will-not-update or https://forums.xamarin.com/discussion/13339/resource-designer-cs-not-regenerated/

how to import javax.swing in android studio

I have just setup Android Studio and started a simple application. Well started is an over statement, I got stuck on first few lines and am unable to import JFrame into android studio.
I have the latest SDK installed aling with LibGDX. I am still not able to setup JFrame. I have searched the net/youtube and no solutions have been found.
I can see the javax and swing in my external libraries but just cannot import.
Any ideas what I am doing wrong or not doing?
I am not looking for a "how to tutorial", I just a pointer where I should go hunting for the answer.
wow, not huge amount of response.
Please advise if I have asked a stupid question or difficult question.
public hungryDog() {
JFrame jframe = new JFrame();
Timer timer = new Timer(20, this);
renderer = new Renderer();
rand = new Random();
jframe.add(renderer);
jframe.setTitle("Hungry Dog");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(WIDTH, HEIGHT);
jframe.addMouseListener(this);
jframe.addKeyListener(this);
jframe.setResizable(false);
jframe.setVisible(true);
dog = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
columns = new ArrayList<Rectangle>();
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
timer.start();
}
You can't use Swing on Android. Android has its own UI framework which is different, and Swing isn't supported. To see what Java APIs are supported on Android look at the API docs at http://developer.android.com/reference/packages.html
I used to have the same problem; here's how to solve it.
You see, we were trying to use swing lib in a totally wrong context, that is, within an Android app. As Scott Barta pointed out, Android has its own mechanisms for doing what we wanted to achieve and that's why IntelliJ didn't let us import anything that would interfere with Android API.
Hence, do NOT use Android app when, say, simply learning how to code in Java or, on a more advanced level, when testing/debugging your algorithms. Instead, build a standalone Java program (yes, in Android Studio). This very topic is covered here: Can Android Studio be used to run standard Java projects? , "Tested on Android Studio 0.8.6 - 1.0.2" by idunnololz). A few clarifying remarks to this solution:
1) wnen preparing your configuration (Run | Edit Configurations...), use your new Java module name and its main class in the appropriate fields.
2) before clicking Run make sure you selected same configuration.
Incidentally, there indeed IS a way to import swing library into any Android app: based on http://www.youtube.com/watch?v=fHEvI_G6UtI. Specifically, add a new line to build.gradle file for your Module:app:
compile files ('<path_to_your_jdk's_rt.jar>')
Like this:
compile files ('C:/Program Files/Java/jdk1.8.0_31/jre/lib/rt.jar')
Note single quotes and forward slashes. Then click Sync Gradle icon and enjoy the view.
In our particular case, however, the program won't run. The code itself will be clean, there'll be no errors; with swing lib well in place and connected, IntelliJ won't suspect a thing; the project WILL compile... but then we'll get a runtime error. Now your know why. On the other hand, this method works like magic when we do not interfere with Android API --- which we shouldn't in the first place.
That's all there is to it.
Yes you can. As addition to Igor's answer here are the steps:
Create new Android Studio project: File -> New -> New Project -> Phone and Tablet -> Add No Activity, select Java (not Kotlin) as language obviously.
Add new Java module: File -> New -> New Module -> Java Library
Remove Android module: File -> Project Structure -> Modules -> app, click - button.
NOTE: step 5 is probably not needed. Try without it first.
Edit build.gradle of your java module, and add in dependencies implementation fileTree pointing to the JRE you need.
Example of build.gradle:
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree ('/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/rt.jar')
}
sourceCompatibility = "6"
targetCompatibility = "6"
Now code completion works and you can even run the app:
Click on Add Configuration: near run button
Click on + -> Application
Select your Main Class
At Use classpath of module: select your java module there (not project's)