Skip to content

Commit

Permalink
Support native project
Browse files Browse the repository at this point in the history
  • Loading branch information
linisme committed Dec 13, 2017
1 parent 75d395e commit 0fdc856
Show file tree
Hide file tree
Showing 36 changed files with 634 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.idik.lib.cipher.so.generater

class CMakeListsBuilder {

private String originCMakePath
private String cipherCMakePath

CMakeListsBuilder(String cipherCMakePath) {
this.cipherCMakePath = cipherCMakePath
}

def setOriginCMakePath(String originCMakePath) {
this.originCMakePath = originCMakePath
this
}

List<String> build() {
List<String> lines = new ArrayList<>()
lines.add("# Auto-Generated By CipherExt.so - 林帅斌([email protected])\n\n")
lines.add("cmake_minimum_required(VERSION 3.4.1)\n\n")
lines.add("add_subdirectory(${new File(cipherCMakePath).parentFile.path} cipher.out)\n\n")
if (originCMakePath != null) {
lines.add("add_subdirectory(${new File(originCMakePath).parentFile.path} origin.out)\n\n")
}
lines
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.idik.lib.cipher.so.task
package net.idik.lib.cipher.so.generater

import net.idik.lib.cipher.so.extension.KeyExt
import net.idik.lib.cipher.so.utils.StringUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.idik.lib.cipher.so.plugin

import com.android.build.gradle.AppExtension
import net.idik.lib.cipher.so.extension.CipherExt
import net.idik.lib.cipher.so.generater.CMakeListsBuilder
import net.idik.lib.cipher.so.task.GenerateCipherSoHeaderTask
import net.idik.lib.cipher.so.task.GenerateJavaClientFileTask
import net.idik.lib.cipher.so.utils.IOUtils
Expand Down Expand Up @@ -92,15 +93,27 @@ class CipherSoPlugin implements Plugin<Project> {
into new File(project.buildDir, "cipher.so")
}
def android = project.extensions.findByType(AppExtension)
android.defaultConfig.externalNativeBuild {
cmake {
String currentFlags = cppFlags ?: ""
cppFlags currentFlags
}
// android.defaultConfig.externalNativeBuild {
// cmake {
// String currentFlags = cppFlags ?: ""
// cppFlags currentFlags
// }
// }
def originCMakePath = android.externalNativeBuild.cmake.path?.path
def cmakelistsOutputDir = new File("${project.buildDir.path}/cipher.so/cmake")
if (!cmakelistsOutputDir.exists()) {
cmakelistsOutputDir.mkdirs()
}
def targetFile = new File(cmakelistsOutputDir, "CMakeLists.txt")
def writer = new FileWriter(targetFile)
new CMakeListsBuilder("${project.buildDir.path}/cipher.so/CMakeLists.txt").setOriginCMakePath(originCMakePath).build().each {
writer.append(it)
}
writer.flush()
writer.close()
android.externalNativeBuild {
cmake {
path "${project.buildDir.path}/cipher.so/CMakeLists.txt"
path "${targetFile.path}"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.idik.lib.cipher.so.task

import net.idik.lib.cipher.so.extension.KeyExt
import net.idik.lib.cipher.so.generater.CipherSoHeaderBuilder
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
Expand Down
1 change: 1 addition & 0 deletions sample2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
44 changes: 44 additions & 0 deletions sample2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
native-lib

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
native-lib

# Links the target library to the log library
# included in the NDK.
${log-lib} )
57 changes: 57 additions & 0 deletions sample2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
apply plugin: 'cipher.so'
apply plugin: 'com.android.application'

android {
compileSdkVersion 26



defaultConfig {
applicationId "net.idik.lib.cipher.so.sample2"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.0.0-beta1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
}
cipher.so {
keys {
数据库 {
value = '你好数据库!,😂'
}
// 网络底层 {
// value = '你好网络底层'
// }
}
}
21 changes: 21 additions & 0 deletions sample2/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
8 changes: 8 additions & 0 deletions sample2/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

add_subdirectory(../ c.out)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.idik.lib.cipher.so.sample2;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("net.idik.lib.cipher.so.sample2", appContext.getPackageName());
}
}
24 changes: 24 additions & 0 deletions sample2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.idik.lib.cipher.so.sample2">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
13 changes: 13 additions & 0 deletions sample2/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring

JNICALL
Java_net_idik_lib_cipher_so_sample2_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.idik.lib.cipher.so.sample2;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("native-lib");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public native String stringFromJNI();
}
34 changes: 34 additions & 0 deletions sample2/src/main/res/drawable-v24/ic_launcher_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeColor="#00000000"
android:strokeWidth="1" />
</vector>
Loading

0 comments on commit 0fdc856

Please sign in to comment.