"You cannot use API level 30 and below"
You just need to set the API level to 31+ in Edit/Project Settings/Player/Other Settings/Identification/Target API Level
android.enableR8
Google made breaking changes with how Unity builds the Android projects. The solution is to download a gradle that is v6.1.1+ and below v7 in https://gradle.org/releases/. This YouTube video explains it clearly. I'll list down the things he did to get his project working. Note: His steps didn't work for me; hence, the succeeding solutions may help you.
- Download gradle from link whose version is greater than or equal to 6.1.1 and less than 7.0.0. (why not below 6.1.1, because Unity said so.)(why not 7+, see section below). Extract it to a folder. Copy the folder path. The folder path should contain the
lib
folder which Unity will be needing.
- In
Edit/Preferences/External Tools
, untick the gradle checkbox and paste the folder path to your gradle you've extracted.
- In
Edit/Project Settings/Player/Publishing Tools
, check these 5 items.
- Add
android:export="true"
in the first Activity tag in Assets\Plugins\Android\AndroidManifest.xml
. (In my case, this didn't work but, for some, it worked for them. Just try doing it first)
- In
Assets\Plugins\Android\baseProjectTemplate.gradle
, make sure the version in this line is 4.0.1
.
classpath 'com.android.tools.build:gradle:4.0.1'
- In
Assets\Plugins\Android\gradleTemplate.properties
, comment out the android.enableR8
by placing a #
at the start of the line.
- Attempt to build.
If not working, proceed to next section.
unityLibrary:compileDebugJavaWithJavac
- Make sure you downloaded a gradle between 6.1.1 and the last version before it becomes 7. Here's a reference
- Attempt to build. If not working, proceed to next step.
AAPT: error: attribute android:export not found
- Remove
android:export=true
in the first Activity tag in Assets\Plugins\Android\AndroidManifest.xml
- Attempt to build. For me, the Unity project was already able to export the game.
Gradle Build Failed. Android resource linking failed...
- If still not working, see below content which is from here
android.useAndroidX=true
android.enableJetifier=true
- Another solution is to go to the Editor folder in your project, create a file called
AndroidManifestPostGenerateGradleProject.cs
, and then paste the following script into it:
{
public void OnPostGenerateGradleAndroidProject(string path)
{
Debug.Log("Build path : " + path);
string gradlePropertiesFile = path + "/../gradle.properties";
if (File.Exists(gradlePropertiesFile)) File.Delete(gradlePropertiesFile);
StreamWriter writer = File.CreateText(gradlePropertiesFile);
writer.WriteLine("android.useAndroidX=true");
writer.WriteLine("android.enableJetifier=true");
writer.Flush();
writer.Close();
}
}
- If there's still a problem, see conversation regarding unicode characters
These are pretty much my research over the past week. Hopefully, this helps some devs.