code style

9/07/2016

How to create an Unity AAR Plugin for Android

Environment: Android Studio 2.1.3

1. new Module -> Android Library

2. new -> Java Class for your Java code.

3. new -> Folder -> JNI folder

4. new -> C++ Class for your C++ code

5. new -> Folder -> Assets folder for your plugin asset files.

6. edit build.gradle for your library

apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        ndk {
            moduleName "unity_plugin"

            // STL library
            stl "gnustl_static"

            // target platforms
            abiFilters "armeabi-v7a", "x86"

            // link other libraries for your C++ code
            String path = file(projectDir).absolutePath + "/src/main/jniLibs/\$(TARGET_ARCH_ABI)/"
            ldLibs "log", "z", "m", "android", path+"libCustom.so"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'

    // reference for Unity Java classes,
    // see https://docs.unity3d.com/Manual/PluginsForAndroid.html
    // "Extending the UnityPlayerActivity Java Code" part
    provided files('classes.jar') 
}

7. add permissions in AndroidManifest.xml,
 such as < uses-permission android:name="android.permission.INTERNET" >

8. double click on YourApplication.:YourLibrary.Tasks.build.assembleRelease in Gradle projects panel to generate the release version of AAR.

9. copy AAR to Unity Assets/Plugins/Android folder.

沒有留言: