Monday, August 23, 2021

Unity 2018.4.27f1 Setup and Error - Enable Android 11 and API 30

 Installation:

1. Android Studio

    - SDK Manager


    - Uncheck Hide obselete

    - Check Show Package  Details

    - Check Android SDK Tools (Obselete)


    - Android 11


2. Unity

    -    Edit > Preferences


    - NDK r16b Download 

    - Gradle 5.6.4 Download (Android 11 introduce <queries> function so we need this support gradle, dont forget to check "Custom Gradle Template" on Player Setting)


    - Unity in you Project after install Google Mobile Ads SDk

    - Open : Asset >  Plugin > Android > mainTemplate : 

            change 'com.android.tools.build:gradle:3.4.0'

            to 'com.android.tools.build:gradle:3.6.0'

    - AAB file in Temp Folder which produce by Unity is gradleOut-release.aab, But Gradle 5.6.4 Look for gradleOut.aab. So open ''mainTemplate'' and replace: Source

 defaultConfig {  
   minSdkVersion **MINSDKVERSION**  
   targetSdkVersion **TARGETSDKVERSION**  
   applicationId '**APPLICATIONID**'  
   ndk {  
     abiFilters **ABIFILTERS**  
   }  
   versionCode **VERSIONCODE**  
   versionName '**VERSIONNAME**'  
 }  
    - with :
 defaultConfig {  
   minSdkVersion **MINSDKVERSION**  
   targetSdkVersion **TARGETSDKVERSION**  
   applicationId '**APPLICATIONID**'  
   ndk {  
     abiFilters **ABIFILTERS**  
   }  
   versionCode **VERSIONCODE**  
   versionName '**VERSIONNAME**'  
   // MLR: rename aab for gradle 3.6.x  
   tasks.whenTaskAdded { task ->  
     if (task.name.startsWith("bundle")) {  
       def renameTaskName = "rename${task.name.capitalize()}Aab"  
       def flavor = task.name.substring("bundle".length()).uncapitalize()  
       tasks.create(renameTaskName, Copy) {  
         def path = "${buildDir}/outputs/bundle/${flavor}/"  
         from(path)  
         include "gradleOut-release.aab"  
         destinationDir file("${buildDir}/outputs/bundle/${flavor}/")  
         rename "gradleOut-release.aab", "gradleOut.aab"  
       }  
       task.finalizedBy(renameTaskName)  
     }  
   }  
   // MLR: End of rename  
 }  
- Player Setting:
    



        



SDK Corrupt, need to reinstall
"Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager."

        next:

  1. go to the location

     "C:\Users\user\AppData\Local\Android\Sdk\build-tools\31.0.0"
    
  2. find a file named d8.bat. This is a Windows batch file.

  3. rename d8.bat to dx.bat.

  4. in the folder lib ("C:\Users\user\AppData\Local\Android\Sdk\build-tools\31.0.0\lib")

  5. rename d8.jar to dx.jar

Remember AppData is a hidden folder. Turn on hidden items to see the AppData folder.

ERROR: The option setting 'android.enableR8=false' is deprecated.

FIX 1: Close Unity, delete Library Folder, Reopen unity.

FIX 2: open "gradleTemplate" add "#" on android.enableR8 then "Android Resolver > Delete Resolved Libraries"

ERRORMutidex

FIX: Open MainTemplate.gradle and add following lines.

defaultConfig {
    ...
    multiDexEnabled true
}
and
dependencies {
    ...
    implementation 'com.android.support:multidex:2.0.1'
}


ERRORAdmob not showing ads

FIX: Uncheck all "Minify" settings

ERROR: Execution failed for task ':launcher:dexBuilderRelease'. > java.lang.UnsupportedOperationException: This feature requires ASM7

FIX: Player Setting > minify > Check: Use R8, Release, Debug

ERRORYou uploaded an APK or Android App Bundle which has an activity, activity alias, service or broadcast receiver with intent filter, but without 'android:exported' property set. This file can't be installed on Android 12 or higher. See: developer.android.com/about/versions/12/behavior-changes-12#exported

FIX 1: API Target version SDK 30

OR

FIX 2: Highest API but Change Android Manifest (Recommended)

  1. Build .aab file to produce AndroidManifest.xml temporary file
  2. Copy "\Temp\StagingArea\AndroidManifest.xml" to "Assets\Plugins\Android\"
  3. Find "activity"
  4. Add (android:exported="true")
  5. <activity android:name="com.unity3d.player.UnityPlayerActivity" android:exported="true" android:label="@string/app_name" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
          <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>