这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ android {
implementation "androidx.viewpager:viewpager:1.0.0"
implementation "androidx.drawerlayout:drawerlayout:1.1.0"
implementation project(":terminal-view")
implementation project(":native-entrypoint")
}

defaultConfig {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@
</intent-filter>
</activity>

<activity android:name="android.app.NativeActivity" android:configChanges="orientation|screenSize">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name" android:value="native-entrypoint"/>

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

<activity-alias
android:name=".HomeActivity"
android:targetActivity="com.termux.app.TermuxActivity">
Expand Down
1 change: 1 addition & 0 deletions native-entrypoint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions native-entrypoint/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
id "com.jfrog.bintray" version "1.7.3"
id "com.github.dcendents.android-maven" version "2.0"
}

apply plugin: 'com.android.library'

ext {
bintrayName = 'native-entrypoint'
publishedGroupId = 'com.termux'
libraryName = 'NativeEntryPoint'
artifact = 'native-entrypoint'
libraryDescription = 'The terminal view used in Termux'
siteUrl = 'https://github.com/termux/termux'
gitUrl = 'https://github.com/termux/termux.git'
libraryVersion = '0.50'
}

android {
compileSdkVersion project.properties.compileSdkVersion.toInteger()
ndkVersion project.properties.ndkVersion

dependencies {
implementation "androidx.annotation:annotation:1.1.0"
}

defaultConfig {
minSdkVersion project.properties.minSdkVersion.toInteger()
targetSdkVersion project.properties.targetSdkVersion.toInteger()
}

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

externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

apply from: '../scripts/bintray-publish.gradle'
21 changes: 21 additions & 0 deletions native-entrypoint/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
4 changes: 4 additions & 0 deletions native-entrypoint/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.termux.myapplication">
</manifest>
6 changes: 6 additions & 0 deletions native-entrypoint/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libnative-entrypoint
LOCAL_SRC_FILES:= main.cpp
LOCAL_LDLIBS:= -llog -landroid -ldl
include $(BUILD_SHARED_LIBRARY)
85 changes: 85 additions & 0 deletions native-entrypoint/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <stdint.h>
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

//BEGIN_INCLUDE(all)
#include <dlfcn.h>
#include <stdio.h>

#include <android/native_activity.h>
#include <android/log.h>

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))

jstring ANativeActivity_getIntent(ANativeActivity* activity, const char* intentName) {
JNIEnv* env = activity->env;
jobject me = activity->clazz;

jclass acl = env->GetObjectClass(me); //class pointer of NativeActivity
jmethodID giid = env->GetMethodID(acl, "getIntent", "()Landroid/content/Intent;");
jobject intent = env->CallObjectMethod(me, giid); //Got our intent

jclass icl = env->GetObjectClass(intent); //class pointer of Intent
jmethodID gseid = env->GetMethodID(icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");

return (jstring)env->CallObjectMethod(intent, gseid, env->NewStringUTF(intentName));
}

void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_t savedStateSize) {
JNIEnv* env = activity->env;
jstring dylib_path = ANativeActivity_getIntent(activity, "DYLIB_PATH");

const char *path = env->GetStringUTFChars(dylib_path, 0);
void* handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);

env->ReleaseStringUTFChars(dylib_path, path);

{
if (handle == NULL) {
char* errorText = dlerror();
LOGW("ERROR!!! %s\n", errorText);
printf("%s\n", errorText);
ANativeActivity_finish(activity);
return;
}

void* entryPoint = dlsym(handle, "ANativeActivity_onCreate");

if (entryPoint) {
LOGI("%s found.\n", "ANativeActivity_onCreate");
} else {
entryPoint = dlsym(handle, __func__);

if (entryPoint) {
LOGI("%s found.\n", __func__);
}
}

if (entryPoint) {
LOGI("calling main...\n");
(*(void(*)(ANativeActivity*, void*, size_t))entryPoint)(activity, savedState, savedStateSize);
LOGI("exited!...\n");
} else {
char* errorText = dlerror();
LOGW("ERROR!!! %s\n", errorText);
printf("%s\n", errorText);
}

dlclose(handle);
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app', ':terminal-emulator', ':terminal-view'
include ':app', ':terminal-emulator', ':terminal-view', ':native-entrypoint'