最近在折騰用Android Studio替代Eclipse處理和Unity3d通信的問題,由于對Android Studio的不了解,踩了不少坑,特留筆記一篇。
踩過坑之后,總結就一個字:同
嗯,是你以為的那個意思,就是相同的意思,相同是指Unity3d和Android Studio的有些參數要配置一樣,具體是
1.包名要相同
2.minSdkVersion和targetSdkVersion要分別相同
Android Studio在build.gradle里設置,Unity在PlayerSetting里設置
如果不同會出現錯誤:
CommandInvokationFailure: Unable to merge android manifests. See the Console for more details.
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/java -Xmx2048M -Dcom.android.sdkmanager.toolsdir="/Users/tomtang/Downloads/android-sdk-macosx/tools" -Dfile.encoding=UTF8 -jar "/Applications/Unity5.6/PlaybackEngines/AndroidPlayer/Tools/sdktools.jar" -
要點
1.在Android Studio里新建一個Android Library Module,不要在默認的app module里打aar.
如果你這樣做了,會出現各種問題,提示好像是資源缺省,我搜索到的一些文章提到是build tool 24的bug,我按文章說的卸載了一些build tool,然鵝,并沒有什么用?。?!
后面新建一個干凈的module就沒有這些問題,所以是不是bug我也沒去求證。

屏幕快照 2017-08-07 下午10.04.27.png

屏幕快照 2017-08-07 下午10.04.42.png
2.還有一些需要注意的地方是不要把unity的classes.jar打包進aar里,重點在下面的注釋里
錯誤如下:

1B3BC27A-08AF-4BF3-B3D5-420826F0C16B.png
改成provided就不用手動刪除classes.jar了。
apply plugin: 'com.android.library'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar']) 引用 改成->
//provided fileTree(dir: 'libs', include: ['*.jar']) 僅僅在編譯時使用,但最終不會被編譯到apk或aar里
provided fileTree(include: ['*.jar'],dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
3.AndroidManifest.xml里刪除引用到資源的幾個地方
原:

屏幕快照 2017-08-07 下午11.44.20.png
改:

屏幕快照 2017-08-07 下午11.51.00.png
附測試工程地址
https://github.com/tomtc123/Unity-AndroidStudio-AAR.git
參考: