在android studio中创建Hello-JNI工程
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.?Overview
What you'll need :
- Android Studio 2.2 or higher from formal?release?or?canary[version before 2.0 also fine].
- Android NDK?version 11c+.
- Latest?Android SDK?tools.
- A test device with a USB cable or Emulator with Android 5.0+.
2.?Create Java Sample App
a) Linux: Run?studio.sh?from your installed location
b) OSX: Find studio installation in Application folder, double click to start
If this is the first time you run this version of Android Studio on this system, Android Studio will prompt to import from previous settings, just select "I do not have a previous version of Studio or I do not want to import my settings", "Welcome to Android Studio" will be displayed.
This creates an Android "Hello World" Java app; your Android Studio looks like:
a) On Mac OS, menu "Android Studio" > "Preferences".
b) On Linux, menu "File" > "Settings".
c) Then "Build, Execution, Deployment" > "Build Tools" > "Gradle".
d) Select "Use Default Gradle wrapper (recommended)", click "OK".
a) Menu "Tools" > "Android" > "SDK Manager"
b) Select tab "SDK Tools"
c) Check "Android NDK"[ or "NDK"] if it is not checked
3.?Add JNI Build Capability to HelloAndroidJni Project
Android Studio supports native development via experimental plugin developed by Google, let's add it into our project.
with your latest version[it does not have to be 0.7.2]:
classpath 'com.android.tools.build:gradle-experimental:0.7.2'Select Android Studio "Project" pane, "Gradle Scripts" > "gradle-wrapper.properties (Gradle Version)" and change:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
to:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
Select Android Studio "Project" pane > "Gradle Scripts" > "build.gradle (Module: app)" and replace:
with:
apply plugin: 'com.android.model.application'model {android {compileSdkVersion = 23buildToolsVersion = "23.0.3"defaultConfig {applicationId = "com.google.sample.helloandroidjni"minSdkVersion.apiLevel = 22targetSdkVersion.apiLevel = 23versionCode = 1versionName = "1.0"}buildTypes {release {minifyEnabled falseproguardFiles.add(file('proguard-android.txt'))}}} } // others below this line: no changeNOTE: the version numbers may be different on your system, and you do not need to change the version number -- just use them as is. Only changing the highlighted part would be fine!
4.?Add JNI Code Into Project
Select the menu "File" > "Project Structure" > "SDK Location", "Android NDK Location" if it is not set yet, then click "...", and browse to your NDK location and click "OK" (you may also choose "download").
Select Android Studio "Project" pane > "Gradle Scripts" > "build.gradle (Module:app)", add the following inside the "model" block, after "buildTypes" block.
Select Android Studio "Project" pane > "app" > "java" > "com.google.sample.helloandroidjni" > "MainActivity", and add JNI function getMsgFromJni() and System.loadLibrary() to the end of class MainActivity.
Note:
- make sure library name is the same as moduleName inside build.gradle
- The "Build" step
In MainActivity.java file, "getMsgFromJni()" is highlighed with red because Android Studio could not find its implementation; let's get it implemented:
- Select function "getMsgFromJni()".
- Wait for context aware menu prompt?to appear.
- Click on?to bring up the popup menu.
- Select "Create Function Java_com_google_example_helloandroidjni_MainActivity_getMsgFromJni".
- Android Studio creates a prototype function for getMsgFromJNI() in hello-android-jni.c file under the "jni" folder. Both got created at once!
- Replace "returnValue" in the above code with our own message:
- Add an ID to the existing TextView.
Open "Android Studio" pane, "res" > "layout" > "content_main.xml"[if you have chosen template "Empty Activity" in step "Create Java Sample App", you file might be "activity_main.xml" instead], select "design" view, and click or "Hello World", inside "Properties" pane, put "@+id/jni_msgView" into?"ID"?field:
[The other way is to directly add into "text" view, and put id in with?android:id="@+id/jni_msgView".] - Display our jni message in the TextView.
In MainActivity::onCreate() function, append following code to the end of the function:
- Select "NewStringUTF" inside hello-android-jni.c, "right click" to bring up the pop-up menu.
- Select "Go To", and "Implementation(s)".
- You will see the function implementation of "NewStringUTF".
- Select other code to explore the native code browsing feature.
5.?Debugging JNI Code
[For Android Studio version earlier than 2.2, select?. Android Studio auto-generates this native debug configuration when it detects JNI code. In this config, debug configurations are enabled by default. If?is not visible, close this project and reopen it with Android Studio, it will be there; Android Studio version 2.2 integrated the debug functionality into app configure].
[Note: if you are using Android Studio RC 1.5 or better, you can set a breakpoint on getMsgFromJni() in Java code and "trace into" JNI code]
6.?Congratulations!
Your app is now ready to use Android Studio for your Native project development!
What we've covered with Android Studio
- Create a JNI project
- Debug native code in JNI project
Next Steps
- Adopt Android Studio to your Android Native project environment
- Reference to detailed online document for?gradle-experimental SDL?syntax
- Explore?Android Studio samples,?google play game samples, and?vulkan samples
Learn More
- Learn how to use?Android Studio.
- Learn Android?NDK?and?SDK.
- Explore more?NDK,?Vulkan tutorials,?Vulkan API, and?Play games?samples on?github.
- Google IO 2015 Presentation for?Android Studio.
- Post questions to?Android Tools Team
轉(zhuǎn)載于:https://my.oschina.net/nextowang/blog/716687
總結(jié)
以上是生活随笔為你收集整理的在android studio中创建Hello-JNI工程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用SQL Server 发送邮件
- 下一篇: 016-购物单