1

After hitting the 64k method limit I tried to export my Unity project as a gradle build. After importing it to Android Studio, I got the error

Error: A library uses the same package as this project: com.myproject.name

After finding this question I added

enforceUniquePackageName = false

to my gradle project. Now I can run the project in Android Studio, but as noted in the answer to that question, I now get the following error when I try to build an APK

Multiple dex files define Lcom/myproject/myappname/BuildConfig;

My gradle file is as follows

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
    }
}

allprojects {
   repositories {
      flatDir {
        dirs 'libs'
      }
   }
}

apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name: 'ShortcutBadger-1.1.5', ext:'aar')
    compile(name: 'animated-vector-drawable-23.4.0', ext:'aar')
    compile(name: 'appcompat-v7-23.4.0', ext:'aar')
    compile(name: 'cardview-v7-23.4.0', ext:'aar')
    compile(name: 'customtabs-23.4.0', ext:'aar')
    compile(name: 'facebook-android-sdk-4.17.0', ext:'aar')
    compile(name: 'facebook-android-wrapper-7.9.4', ext:'aar')
    compile(name: 'firebase-analytics-11.0.2', ext:'aar')
    compile(name: 'firebase-analytics-impl-11.0.2', ext:'aar')
    compile(name: 'firebase-app-unity-4.0.2', ext:'aar')
    compile(name: 'firebase-common-11.0.2', ext:'aar')
    compile(name: 'firebase-core-11.0.2', ext:'aar')
    compile(name: 'firebase-iid-11.0.2', ext:'aar')
    compile(name: 'firebase-messaging-11.0.2', ext:'aar')
    compile(name: 'firebase-messaging-unity-4.0.2', ext:'aar')
    compile(name: 'play-services-base-11.0.2', ext:'aar')
    compile(name: 'play-services-basement-11.0.2', ext:'aar')
    compile(name: 'play-services-gcm-11.0.2', ext:'aar')
    compile(name: 'play-services-iid-11.0.2', ext:'aar')
    compile(name: 'play-services-tasks-11.0.2', ext:'aar')
    compile(name: 'support-compat-26.0.0-alpha1', ext:'aar')
    compile(name: 'support-core-ui-26.0.0-alpha1', ext:'aar')
    compile(name: 'support-core-utils-26.0.0-alpha1', ext:'aar')
    compile(name: 'support-fragment-26.0.0-alpha1', ext:'aar')
    compile(name: 'support-media-compat-26.0.0-alpha1', ext:'aar')
    compile(name: 'support-v4-26.0.0-alpha1', ext:'aar')
    compile(name: 'support-vector-drawable-23.4.0', ext:'aar')
    compile(name: 'utnotifications', ext:'aar')
    compile project(':Firebase')
    compile project(':UTNotificationsRes')
    compile fileTree(dir: 'fabric-init/libs', include: ['*.jar'])
    compile fileTree(dir: 'crashlytics-wrapper/libs', include: ['*.jar'])
    compile fileTree(dir: 'crashlytics/libs', include: ['*.jar'])
    compile fileTree(dir: 'beta/libs', include: ['*.jar'])
    compile fileTree(dir: 'answers/libs', include: ['*.jar'])
    compile project(':fabric')
    compile (project(':unity-android-resources'))
}

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.0'
    enforceUniquePackageName = false

    defaultConfig {
        targetSdkVersion 25
    }

    lintOptions {
        abortOnError false
        disable 'MissingTranslation'
    }
    signingConfigs { 
      //omitted!
    }

    buildTypes {
        debug {
            jniDebuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
            signingConfig signingConfigs.release
        }
    }

}

After removing different plugins and trying to build, it seems that I can build the project if I comment out the last plugin unity-android-resources, but of course my app doesn't run properly. Why would this error occur here, and how I can I resolve this issue?

Steven Coull
  • 468
  • 3
  • 14
  • How do you import it into Android Studio? – Programmer Jul 11 '17 at 05:09
  • I'm using Unity 5.5.0f3, Build System is set to "Gradle (new)" with the "export project' checkbox ticked. Once the build finishes open Android Studio 2.3.3, Import Project, then select the build (not the root folder, the project inside). On import Android Studio throws a gradle version error so I updated it to 2.3.3. – Steven Coull Jul 11 '17 at 05:19
  • Ok. How about you don't export with "Gradle (New)". Just export as "ADT (Legacy). Follow [this](https://stackoverflow.com/questions/38980792/exporting-and-running-unity3d-project-to-android-studio/39008549#39008549) step. It will show you how to do that and gradle will be added after that. Then follow that step to import it. If that fails, export as "Internal (Default)" then follow that step in I linked. Don't forget that you **will** get Error in Android Studio. Step 2A will fix that error. – Programmer Jul 11 '17 at 05:37
  • Gave this a try, but end up with a rather nasty error as it tries to merge my manifests: Error:Execution failed for task ':bLA:processDebugManifest'. > Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.0-alpha1) from [adttest:supportcompat2600alpha1:unspecified] AndroidManifest.xml:28:9-38 is also present at [com.android.support:support-v4:25.3.1] AndroidManifest.xml:27:9-31 value=(25.3.1). Suggestion: add 'tools:replace="android:value"' to element at AndroidManifest.xml:26:5-28:41 to override. – Steven Coull Jul 11 '17 at 06:22
  • There was also a pretty long import-summary outlining what wasn't included in the gradle project, containing a lot of manifest files etc from my plugins. – Steven Coull Jul 11 '17 at 06:25
  • The error is just because you have so many plugins that came with different Manifests. They defined the-same attribute with different values in each Manifests. Ask yourself what the compiler should do. You have the error right there and it clearly tells you which attributes are causing the problems. Simply open the Manifests and remove the duplicated attributes. That's it. – Programmer Jul 11 '17 at 06:31
  • While I'm sure on simpler projects exporting to ADT is viable solution, in comparison to the gradle export (which is supported, unlike ADT) this export is a complete mess and unmaintainable. You said that I simply have to remove the duplicated attributes, but clearing one seems to just bring up another. Since this isn't an answer to my question but an alternative solution I'd rather focus on the question, and if someone else has the same issue as me they can try yours if they like. – Steven Coull Jul 11 '17 at 06:51
  • Alright. I am sorry for chipping in. When you find a one click or a very easy solution to export and import Unity Project that has over 8 different plugins, please leave an answer to help others. See [this](https://stackoverflow.com/a/26294495/3785314) for a possible solution to your problem. Good luck. – Programmer Jul 11 '17 at 07:32
  • No worries! I know you were just offering help, but since either build requires a lot of time to solve (if I thought gradle was a one click solution I'd not be posting this!!) I'd rather focus on the answer that will be supported from now on by Unity. Sadly I've tried that link too and had no luck, but thanks for your input anyway! – Steven Coull Jul 11 '17 at 08:41

1 Answers1

1

Your last project unity-android-resources has the same packageName in AndroidManifest.xml as the base application (in gradle parameter applicationId). You can write any other packageName, for example com.example.myapplication.resources.

  • Thanks for your reply. This package is a default package created by Unity if you use the 'res' folder inside the Plugins > Android directory, which you need in order to add app name localizations. To get around this it seems you have to create a plugin containing your localized names and import it, as described here: https://forum.unity.com/threads/unity-5-6-gradle-build-problem.465074/#post-3186363 – Steven Coull Oct 12 '17 at 02:13