2017-01-05 101 views
1

我能夠導出Unity3d的android項目,並在android studio中導入它,但我如何將多個unity3d項目導入到android studio? 我試圖導入它作爲一個模塊,但沒有奏效。向android studio導入多個unity3d項目?

每unity3d項目依賴於統一,classes.jar文件,所以當我導入多個項目,它拋出錯誤

Error:Execution failed for task :app:transformNative_libsWithMergeJniLibsForDebug'. com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK lib/armeabi-v7a/libmain.so

回答

1

在這裏,我是如何解決這個。

首先,您必須將android和您的場景導出爲android並檢查Google開發。之後,你打開任何Android項目。

  • 1.文件 - >新建 - >導入模塊...並選擇你的團結項目。

  • 2.Open您的統一項目的gradle這個(ex.build.gradle(模塊:團結)),並刪除「應用插件:......」和的applicationID
    ,而是你加在上面「申請插件:「com.android.library」」

  • 3.You去文件 - >項目結構和模塊,你的依賴性選擇app.Then,按下+ - >模塊依賴和
    您選擇你的團結工程。

  • 4.在java UnityActivity中,將活動擴展爲Fragment,並添加此代碼。

    public class UnityActivity extends Fragment { 
        protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code 
        private static final String ARG_SECTION_NUMBER = "section_number"; 
    public static UnityActivity newInstance(int sectionNumber) { 
        UnityActivity fragment = new UnityActivity(); 
        Bundle args = new Bundle(); 
        args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
        fragment.setArguments(args); 
        return fragment; 
    } 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
              Bundle savedInstanceState) 
    { 
        getActivity().getWindow().takeSurface(null); 
        getActivity().setTheme(android.R.style.Theme_NoTitleBar_Fullscreen); 
        getActivity().getWindow().setFormat(PixelFormat.RGB_565); 
        mUnityPlayer = new UnityPlayer(getActivity()); 
        if (mUnityPlayer.getSettings().getBoolean ("hide_status_bar", true)) { 
         getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
           WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        } 
        int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1); 
        boolean trueColor8888 = false; 
        mUnityPlayer.init(glesMode, trueColor8888); 
        mUnityPlayer.windowFocusChanged(true); 
        mUnityPlayer.setX((float)-5); 
        View playerView = mUnityPlayer.getView(); 
        return playerView; 
    } 
    // Quit Unity 
    @Override public void onDestroy() 
    { 
        mUnityPlayer.quit(); 
        super.onDestroy(); 
    } 
    // Pause Unity 
    @Override public void onPause() 
    { 
        super.onPause(); 
        mUnityPlayer.pause(); 
    } 
    // Resume Unity 
    @Override public void onResume() 
    { 
        super.onResume(); 
        mUnityPlayer.resume(); 
    } 
    // This ensures the layout will be correct. 
    @Override public void onConfigurationChanged(Configuration newConfig) 
    { 
        super.onConfigurationChanged(newConfig); 
        mUnityPlayer.configurationChanged(newConfig); 
    } 
    // Notify Unity of the focus change. 
    public void onWindowFocusChanged(boolean hasFocus) 
    { 
        super.getActivity().onWindowFocusChanged(hasFocus); 
        mUnityPlayer.windowFocusChanged(hasFocus); 
    } 
    // For some reason the multiple keyevent type is not supported by the ndk. 
    // Force event injection by overriding dispatchKeyEvent(). 
    public boolean dispatchKeyEvent(KeyEvent event) 
    { 
        if (event.getAction() == KeyEvent.ACTION_MULTIPLE) 
         return mUnityPlayer.injectEvent(event); 
        return super.getActivity().dispatchKeyEvent(event); 
    } 
    // Pass any events not handled by (unfocused) views straight to UnityPlayer 
    public boolean onKeyUp(int keyCode, KeyEvent event)  { return mUnityPlayer.injectEvent(event); } 
    public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); } 
    public boolean onTouchEvent(MotionEvent event)   { return mUnityPlayer.injectEvent(event); } 
    /*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); } 
        } 
    
+0

但問題是不同的Unity模塊* .so文件具有相同的名稱,當我們要構建項目時會拋出命名衝突。 – Pranav

0

我得到了完全相同的問題,當我試圖導入兩項單位工程爲兩個庫到Android工作室。有兩種解決方法:

  1. 在graddle.build你應用程式來添加:

    android { 
        ... 
    
        packagingOptions { 
         pickFirst 'lib/armeabi-v7a/libmain.so' 
         pickFirst 'lib/armeabi-v7a/libmono.so' 
         pickFirst 'lib/armeabi-v7a/libunity.so' 
         pickFirst 'lib/x86/libmain.so' 
         pickFirst 'lib/x86/libmain.so' 
         pickFirst 'lib/x86/libmain.so' 
        } 
    
        ... 
    } 
    
  2. 還是去項目窗口 - > Android的 - >在你的團結項目的一個定位文件夾jniLibs並刪除文件夾armeabi-v7x86

+0

對於不同的Unity模塊,這將不起作用即使我們使用pickFirst或merge作爲packagingOptions,a * .so文件體系結構也是不同的,它將只選擇一個僅適用於一個模塊的.so文件。 – Pranav