实例项目的module结构

image_thumb

app module 的 manifests,其中android:value=”native-activity”与nativeactivity的build.gradle中的moduleName对应

<?xml version="1.0" encoding="utf-8"?><!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.native_activity"
    android:versionCode="1"
    android:versionName="1.0">
    <!-- This .apk has no Java code itself, so set hasCode to false. -->
    <application
        android:allowBackup="false"
        android:fullBackupContent="false"
        android:hasCode="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <!-- Our activity is the built-in NativeActivity framework class.
             This will take care of integrating with our NDK code. -->
        <activity
            android:name="android.app.NativeActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name">
            <!-- Tell NativeActivity the name of or .so -->
            <meta-data
                android:name="android.app.lib_name"
                android:value="native-activity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest><!-- END_INCLUDE(manifest) -->

native module的manifests

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.foohao.nativeactivity">
    <application/>
</manifest>

项目的build.gradle

// Top-level build file where you can add
// configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.7.0'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

app module的 build.gradle

apply plugin: 'com.android.model.application'
model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = '23.0.3'
        defaultConfig {
            applicationId = 'com.example.native_activity'
            minSdkVersion.apiLevel    = 18
            targetSdkVersion.apiLevel = 23
        }
        ndk {
            platformVersion = 18
            moduleName ='native-activity'
            toolchain = 'gcc'
            stl = 'gnustl_static'
            cppFlags.add('-std=c++11')
            ldLibs.addAll(['log', 'android', 'EGL', 'GLESv1_CM'])
        }
        sources {
            main {
                jni {
                    dependencies {
                        project ':nativeactivity' linkage 'static'
                    }
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled = false
                proguardFiles.add(file('proguard-rules.txt'))
            }
        }
    }
}

nativeactivity module的 build.gradle

apply plugin: 'com.android.model.library'
def ndkDir = System.getenv("ANDROID_NDK_HOME")
def propertiesFile = project.rootProject.file('local.properties')
if (propertiesFile.exists()) {
    Properties properties = new Properties()
    properties.load(propertiesFile.newDataInputStream())
    ndkDir = properties.getProperty('ndk.dir')
}
model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.3"
        defaultConfig.with {
            minSdkVersion.apiLevel = 18
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "0.0.1"
        }
        ndk {
            moduleName = 'native-activity'
            toolchain = 'gcc'
            ldLibs.addAll(['log', 'android'])
            ldFlags.add("-c")
        }
        sources {
            main {
                jni {
                    source {
                        srcDir "${ndkDir}/sources/android/native_app_glue"
                    }
                    exportedHeaders {
                        srcDir "${ndkDir}/sources/android/native_app_glue"
                    }
                }
            }
        }
    }
}

转载于:https://my.oschina.net/foohao/blog/675289

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐