Unity Asset Bundles Google Play Asset Delivery - Proguard Minification - unity3d

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

Related

Fatal Crash when using Unity to call startMoniting from AAR file

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.

dagger android support to androidx.fragment

How to inject a fragment from the package androidx.fragment.app.Fragment ?
I'm using the dagger-android framework to inject my dependencies in my code.
As the documentation says I do this to inject my fragment
#Override
public void onAttach(Activity activity) {
AndroidInjection.inject(this);
super.onAttach(activity);
// ...
}
the problem is that AndroidSupportInjection class accept only fragments of the package android.support.v4.app.Fragment or if I use AndroidInjection class only accept fragments of the package android.app.Fragment and I want to use fragments of the androidx.fragment.app.Fragment package.
Also DaggerFrament extend from android.support.v4.app.Fragment and want to use a fragment from androidx
And If I try to implement HasSupportFragmentInjector also this interface use a fragment from android.support
add the below code to your gradle.properties
android.useAndroidX=true
android.enableJetifier=true
And if you are trying to inject into a Fragment you have to replace AndroidInjection.inject(this) with AndroidSupportInjection.inject(this)
I had the same problem in case of HasFragmentInjector. You need to use HasSupportFragmentInjector for fragment injection. This is because, HasFragmentInjector uses android.app.Fragment which is not effected by jetifier. You need to add android-dagger-support library, jetifier converts all the support packages to androidx in Studio 3.3 (if jetifier is enabled).
If jetifier does not change support packages to androidx packages. You can download jetifier tool from here and convert the android-dagger-support.aar file manually by using the following command.
./jetifier-standalone -i dagger-android-support-<version>.aar -o <output-name>
Then add the library to your project. This is the HasSupportFragment class after conversion
import androidx.fragment.app.Fragment;
import dagger.android.AndroidInjector;
public interface HasSupportFragmentInjector {
AndroidInjector<Fragment> supportFragmentInjector();
}
Somehow, jetifier tool was not converting libraries in AndroidStudio.
I had to do it manually.
I had a similar error and it was due to the Dagger version. On version 2.17 there is an strange issue, but if you roll back to version 2.16 it compiles perfectly (apart from the flags on gradle.properties that Paul posted).
From there using the tutorials you won't have trouble. Forgot to mention that on my project I had the non-androidX version of everything, then I ran the androidX migration that android studio offers, and after that I had to switch the Dagger version, but I suppose that if you do it from the start it's the same.
Hope this helps, if you switch and it doesn't work, post a little bit of your dagger implementation and plugins versions and I will try to help more!
Add the following to your gradle.properties file
android.useAndroidX = true
android.enableJetifier = true
Just for reference. i had the same problem. It was Jetifier issue. please upgrade your gradle build tools plugin to 3.3.0
classpath 'com.android.tools.build:gradle:3.3.0'
Sample code: https://github.com/jega-ms/android-dagger2-mvp-rx
This is what I did to work with androidx namespace for Dagger 2.21
Downloaded the jetifier tool from here: https://developer.android.com/studio/command-line/jetifier
Unzip into a folder and open a terminal pointing into the extracted bin folder
From Android Studio, open a class like DaggerFragment to check the path where the file is stored, for me (in MacOS) is something like this:
From terminal execute this command (replacing with the correct variables and path)
./jetifier-standalone -i /Users/{YOUR_USER}/.gradle/caches/{PATH_TO_DAGGER_ANDROID_SUPPORT_FOLDER}/dagger-android-support-2.21.aar -o dagger-android-support-2.21.aar
The converted dagger-android-support-2.21.aar will appear in your bin folder
Now open your app build.gradle file and change this line
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
adding the , '*.aar' part in the include array
Move the generated dagger-android-support-2.21.aar from bin to libs folder inside your project.
Remove (or comment) this line from the dependencies
implementation "com.google.dagger:dagger-android-support:2.21
Now you can proceed invalidating the cache and rebuild the project and now DaggerFragment will point to your converted version which uses androidx.fragment.app.Fragment
NOTE: Obviously this is a temporary workaround and you should move to the official version as soon this is fixed in Dagger
The solution to my particular problem was to use android dagger classes as interfaces instead of extend of them:
class MyFragment() : HasSupportFragmentInjector {
#Inject
lateinit var childFragmentInjector: DispatchingAndroidInjector<Fragment>
override fun onAttach(context: Context?) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}
override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return childFragmentInjector
}
........
}
To my Activities
class MyActivity : HasSupportFragmentInjector {
#Inject
internal lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}
override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector
......
}
and also I have this in my gradle.properties file:
android.useAndroidX = true
android.enableJetifier = true

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

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)