Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ndk-build with Android Studio. #15676

Merged
merged 4 commits into from
Feb 20, 2017
Merged

Use ndk-build with Android Studio. #15676

merged 4 commits into from
Feb 20, 2017

Conversation

chaoren
Copy link
Contributor

@chaoren chaoren commented May 18, 2016

This feature depends on the latest canary preview of Android Studio. You might want to hold off on this pull request until the feature stabilizes.

@minggo
Copy link
Contributor

minggo commented May 19, 2016

Thanks @chaoren . Can i debug C++ codes after apply this patch?
And do you know when will the stable version been released?

@chaoren
Copy link
Contributor Author

chaoren commented May 19, 2016

Can i debug C++ codes after apply this patch?

Yes. Please try it out and file bugs if you run into any.

And do you know when will the stable version been released?

I can't give you any specific dates here, but stable is usually about a month or so behind canary.

@minggo
Copy link
Contributor

minggo commented May 19, 2016

Thanks, i will have a try.

@zilongshanren
Copy link
Member

[ci rebuild]

@minggo
Copy link
Contributor

minggo commented May 20, 2016

@chaoren How to test it? I just downloaded Android Studio 2.2, and click run menu, it doesn't build native codes, just build java codes and generate the app. And the CPP files are not displayed in Android Studio, then how can i set a breakpoint if we fix compiling issue?

@minggo
Copy link
Contributor

minggo commented May 20, 2016

Oh, i tested cpp-empty-test, and i found you didn't modify it. I will try by creating a new project.

@minggo
Copy link
Contributor

minggo commented May 20, 2016

It seems i blocked on importing new generated project

screen shot 2016-05-20 at 6 35 44 pm

@minggo minggo self-assigned this May 20, 2016
@TheCodez
Copy link
Contributor

@chaoren I'm getting the following error on windows:
Error:Process 'command 'C:\NVPACK\android-sdk-windows\cmake\bin\cmake.exe'' finished with non-zero exit value 1

Any ideas?

@chaoren
Copy link
Contributor Author

chaoren commented May 20, 2016

@minggo Ah, my apologies, I'll change the tests as well.

@TheCodez currently, you'll need to install this for it to work on Windows: https://www.microsoft.com/en-us/download/details.aspx?id=48145
We're aware of this limitation, and there has been a change that removes this requirement, however, that'll probably not be available until next week or so.

@TheCodez
Copy link
Contributor

TheCodez commented May 20, 2016

@chaoren Thanks, but that didn't fix the problem.

[EDIT] I created a new project and now I'm getting a different error:

Error:Execution failed for task ':HelloCpp:externalNativeBuildDebug'.
> com.android.ide.common.process.ProcessException: Error while executing 'C:\NVPACK\android-sdk-windows\cmake\bin\cmake.exe' with arguments {--build C:\Users\Michi\Desktop\cpp-template-default\proj.android-studio\app\build\intermediates\cmake\debug\json\armeabi-v7a --target MyGame}

@chaoren
Copy link
Contributor Author

chaoren commented May 20, 2016

@TheCodez are you opening the test projects or creating a new project from template and opening that?

Please provide the steps you performed before reaching that error.

@TheCodez
Copy link
Contributor

TheCodez commented May 20, 2016

@chaoren I just created a new project using cocos console, and opened the proj.android-studio folder in Android Studio. While doing gradle sync it's encountering the cmake error.

I've attached my log, hope it's somehow useful.
idea.txt

[EDIT] When doing Build/Clean Project I get the following output:

Information:Gradle tasks [clean]
CMake Error at C:/NVPACK/android-sdk-windows/cmake/android.toolchain.cmake:616 (message):
  Could not find any working toolchain in the NDK.  Probably your Android NDK
  is broken.
Call Stack (most recent call first):
  C:/NVPACK/android-sdk-windows/cmake/share/cmake-3.4/Modules/CMakeDetermineSystem.cmake:98 (include)
  CMakeLists.txt:30 (project)
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
CMake Error at C:/NVPACK/android-sdk-windows/cmake/android.toolchain.cmake:616 (message):
  Could not find any working toolchain in the NDK.  Probably your Android NDK
  is broken.
Call Stack (most recent call first):
  C:/NVPACK/android-sdk-windows/cmake/share/cmake-3.4/Modules/CMakeDetermineSystem.cmake:98 (include)
  CMakeLists.txt:30 (project)
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage

@minggo
Copy link
Contributor

minggo commented May 23, 2016

I met the error when importing a new project:

Gradle 'proj.android-studio' project refresh failed
  Error:A problem occurred configuring project ':MyCppGame'.

Steps to reproduce:

  • use cocos new -l cpp to create a new project
  • use Android Studio 2.2 preview 1 to import the new project
  • then the error happened

I uses latests cocos2d-x codes and apply this PR. And my OS is Mac OS X 10.11.4. The full log file is

idea.log.zip

Edit: i installed Android Studio 2.2 preview 1 and Android Studio 2.0.

@chaoren
Copy link
Contributor Author

chaoren commented May 23, 2016

Could you please make sure that these SDK components are installed and up to date?

SDK Platforms -> Android 5.1 (Lollipop) 22
SDK Tools -> CMake
SDK Tools -> LLDB 2.2
SDK Tools -> NDK

We'll need to change that message to something more useful.

@minggo
Copy link
Contributor

minggo commented May 23, 2016

It seems LLDB 2.2 is missing. I will try to install it.

@minggo
Copy link
Contributor

minggo commented May 23, 2016

@chaoren My computer has Cmake, but Android Studio can not find it and ask me to install it. And i can not find a way to set Cmake path.

@chaoren
Copy link
Contributor Author

chaoren commented May 23, 2016

The CMake integration requires the custom CMake from the SDK, so the CMake on your computer would not work. When this feature stabilizes, we'll hopefully upstream our changes to CMake, but whether the ~20MB of space is worth the additional complexity is debatable.

@chaoren
Copy link
Contributor Author

chaoren commented May 23, 2016

@TheCodez could you please make sure that you're using the latest NDK?

@minggo
Copy link
Contributor

minggo commented May 23, 2016

@chaoren Ok, i downloaded the cmake and needed components. And it can compile the codes. But i can not see the log, for example, which cpp file is compiled, and what's the compiling flags and so on as i can before using cocos command.

@chaoren
Copy link
Contributor Author

chaoren commented May 23, 2016

But i can not see the log, for example, which cpp file is compiled, and what's the compiling flags and so on as i can before using cocos command.

Yes, that is the expected behavior right now. We could change the compiling to be more verbose, but we'll have to look at whether the extra output would significantly slow down compilation.

@minggo
Copy link
Contributor

minggo commented May 23, 2016

@chaoren The app crashed when i clicked Debug MyCppGame-native. But it can run of i use cocos run -p android, which means the project itself is not problem.

@chaoren
Copy link
Contributor Author

chaoren commented May 23, 2016

@minggo does clicking just run (without debug) work? Are you running against a physical device or an emulator?

@minggo
Copy link
Contributor

minggo commented May 23, 2016

No. It can not run no matter debug or not, and no matter select MyCppGame or MyCppGame-native. I tested on Android device(Sangsung S4 Zoom, Android 4.2.2).

@minggo
Copy link
Contributor

minggo commented May 23, 2016

And i found there is a strange log
Connected to the target VM, address: 'localhost:8605', transport: 'socket'

@chaoren
Copy link
Contributor Author

chaoren commented May 23, 2016

That's a normal message when attaching the debugger.

When you say "crashed", does it just look like it's hanging, or is it opening the app then immediately closing? Could you please provide the logcat as it crashes?

@minggo
Copy link
Contributor

minggo commented May 23, 2016

It closed immediately, the logcat is

logcat.txt.zip

And i found the output MyCppGame/proj.android-studio/app/build/intermediates/cmake/debug/lib/armeabi/libMyGame.so is 42.9M, while MyCppGame/proj.android/libs/armeabi/libMyGame.so is only 10.5M. I think it is because cmake full link all modules. Instead we use LOCAL_STATIC_LIBRARIES instead of LOCAL_WHOLE_STATIC_LIBRARIES for most modules in Android.mk.

@cezheng
Copy link
Contributor

cezheng commented May 23, 2016

I installed Android Studio 2.2 Preview 1 on OSX 10.11.4, checked out your branch, then created a new project. But I couldn't sync the gradle file with the project. The IDE simply got an exception.

Here's the Error Logs
image

@chaoren
Copy link
Contributor Author

chaoren commented May 23, 2016

@cezheng you get that error when the NDK isn't present, please make sure SDK Tools -> NDK is installed and up to date.

@minggo minggo removed this from the next milestone Feb 16, 2017
@minggo minggo mentioned this pull request Feb 16, 2017
@piotrros
Copy link

I'm using AS 2.2.3 and can see all .cpp files. Problem is with extra .h files. I have a file named "Definitions.h", it is in android.mk file, but studio isn't showing it.
As for apk size I think stripping is fine.
As for clean second attempt always works.

I had no other issues and moved all my cocos2d-x projects to android studio.
One thing, which can be annoying is when have something missing in android.mk file. Error is sooo long that it's very hard to find out what is missing. But it's just android studio :)

Also: some people report having this error: "Error: Configuration with name 'default' not found.". I don't know what causes this, because it's not happening on my device.

@minggo
Copy link
Contributor

minggo commented Feb 16, 2017

@piotrros you can see cocos2d-x .cpp files?

@piotrros
Copy link

@minggo it depends on build.gradle. When I was using cocos2d-x bundled in project I could see all cocos2d-x files. Now I use precompiled library so I naturally don't see them.

@minggo
Copy link
Contributor

minggo commented Feb 16, 2017

I mean with this PR, i can not see cocos2d-x files. Does it mean this PR has some problem in build.gradle?

@piotrros
Copy link

It should be fine. I've modified build.gradle to my own needs. Using precompiled libraries makes app compile in seconds.. By the way it's not fine to use all abiFilters (abiFilters 'x86', 'armeabi', 'armeabi-v7a', 'arm64-v8a'), apk will be huge. I'm personally using armeabi-v7a and can debug c++ code on all my devices.

@minggo
Copy link
Contributor

minggo commented Feb 16, 2017

@piotrros yep, we should only build one target. I will merge this PR in 3.15 if possible because we have to modify cocos command to adapt it too. And i am not familiar with build.gradle grammar. I have to learn more about it.

@jomof
Copy link

jomof commented Feb 16, 2017 via email

@minggo
Copy link
Contributor

minggo commented Feb 20, 2017

@jomof i will merge this PR though i can not view the cocos2d-x cpp files in Android Studio. Can you view the cocos2d-x cpp files in Android Studio?

@minggo minggo merged commit d14f427 into cocos2d:v3 Feb 20, 2017
@jomof
Copy link

jomof commented Feb 20, 2017 via email

@minggo
Copy link
Contributor

minggo commented Feb 20, 2017

@jomof thanks. I haven't tried 2.3 since it is not stable.

@minggo
Copy link
Contributor

minggo commented Feb 20, 2017

@jomof

sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        jniLibs.srcDir "libs"
        manifest.srcFile "AndroidManifest.xml"
        assets.srcDir "../../../src"  // how can include "src" folder name in "assets"
    }

I am doing lua project setting for Android Studio as mentioned above. I want to include src folder name in assets. How can i do it?

@minggo
Copy link
Contributor

minggo commented Feb 20, 2017

I have to do like this to delete unneeded folders, but is there any other way to exclude them?

android.applicationVariants.all { variant ->
    def rmassets = task("delete${variant.name}Assets", type: Delete) {
        delete "${buildDir}/intermediates/assets/${variant.dirName}/project"
        delete "${buildDir}/intermediates/assets/${variant.dirName}/simulator"
    }
    variant.mergeAssets.finalizedBy rmassets
}

@jomof
Copy link

jomof commented Feb 20, 2017 via email

@minggo
Copy link
Contributor

minggo commented Feb 21, 2017

How to exclude resources? I tried assets.exclude or assets.filter.exclude, but can not work.

davidchai-6waves pushed a commit to davidchai-6waves/cocos2d-x that referenced this pull request Feb 21, 2017
* Use ndk-build integration in Android Studio.

* Update build.gradle

* Update build.gradle

* Update build.gradle
stevetranby added a commit to stevetranby/cocos2d-x that referenced this pull request Mar 2, 2017
…futurePRs

* commit 'eca39ba7aa7105133c012f53a8c3a7a71793f72c':
  fixed cocos2d#17413: [Android] AudioEngine::play2d may still wait 2 seconds if preload is too fast. (cocos2d#17414)
  Fix various typos in comments and strings (cocos2d#17410)
  Fix variable name typo in Sprite3DTest.js (cocos2d#17409)
  Update the reference of submodule cocos2d-console. (cocos2d#17407)
  Fix minor typos in tests (cocos2d#17404)
  Typo fix: Rename `MutiTouchTest` to `MultiTouchTest` (cocos2d#17399)
  Fix minor typos in UI test cases (cocos2d#17400)
  fixed cocos2d#17397: Updates CMakeList.txt for win32 audio module (cocos2d#17401)
  Update the reference of submodule web. (cocos2d#17396)
  Update the reference of submodule web. (cocos2d#17393)
  [ci skip][AUTO]: updating luabinding & jsbinding & cocos_file.json automatically (cocos2d#17392)
  Update the spine runtime of JS. (cocos2d#17391)
  [ci skip]make all tests and template support Android Studio (cocos2d#17389)
  Use ndk-build with Android Studio. (cocos2d#15676)
  [WebSocket] Adds support for getting url and server selected protocol (cocos2d#17381)
  [ci skip][AUTO]: updating luabinding & jsbinding & cocos_file.json automatically
  Refine editbox for issue 17346 --merge some member variables into EditBoxCommon (cocos2d#17361)

# Conflicts:
#	tools/cocos2d-console
@minggo
Copy link
Contributor

minggo commented Mar 13, 2017

@jomof I updated Android Studio to 2.3, can not see cocos2d-x c++ source codes.

@jomof
Copy link

jomof commented Mar 13, 2017 via email

@minggo
Copy link
Contributor

minggo commented Mar 14, 2017

I tested with latest codes which includes this PR. You can have a try. And the steps is the same as 11/15/16 in this thread.

@jomof
Copy link

jomof commented Mar 14, 2017 via email

@minggo
Copy link
Contributor

minggo commented Mar 15, 2017

After updating to latest gradle version and build tool version. The issue is fixed. Thanks.

@TheProphetOfRa
Copy link

@jomof Did the issue with the path lengths on Windows get resolved? The latest version of AS works perfectly on macOS but when trying to compile on Windows I get path length errors. When setting APP_SHORT_COMMANDS and LOCAL_SHORT_COMMANDS to true in my build.gradle if I run gradle in debug it shows that after I assign them to true they get assigned back to false.

@jomof
Copy link

jomof commented Mar 23, 2017 via email

@minggo
Copy link
Contributor

minggo commented Mar 24, 2017

Hi all, could you please discuss Android Studio issue in the forum? I already created a ticket. And i will lock this thread since there are too many conversations to open this page.

@cocos2d cocos2d locked and limited conversation to collaborators Mar 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.