Search code examples
androidunity-game-engineandroid-manifestandroid-install-apk

How to Build Apk from Unity with custom android Code


I have a unity project and normally what i do is export it to android make some changes in the gradle and manifest file along with adding some custom Java files that I have and then I sync the project and build the apk. Is there any other way to do this without going through all this hassle like using some command line tool? or any other scripting which will basically just automate this entire process for me?


Solution

  • First solution is generate .jar from your java code and put it in Assets/Plugins/Android/ folder as mentioned in comments

    Another solution is to use custom gradle template, follow this link to enable custom gradle template. Then, in your Assets/Plugins/Android/mainTemplate.gradle file under android folder add this:

    sourceSets 
    {
        main
        {
            java
            {
                srcDirs = ['src']
                // 'D:/Projects/somep/app/src/main/java/com'
                srcDirs += ['"PathToYourJavaCode"']
            }
        }
    }
    

    So your gradle file should look like this

    enter image description here