1. 新しい Android プロジェクトを作成する
1.1 Android プロジェクトの Gradle バージョンを変更する
次の図に示すように、Android Studio の左上隅にある [ファイル] メニューをクリックし、[プロジェクト構造] を選択します。
次の図に示すように、Android Gradle Plugin Versionのバージョンを3.5.4に 変更し、Gradle Versionのバージョンを6.5に変更します。
1.2 Androidプロジェクト(プロジェクト配下)のgradleバージョンを変更する
プロジェクトの下のbuild.gradleにRepluginのリポジトリ アドレスを追加します。コードは次のとおりです。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
//添加kotlin的支持
ext.kotlin_version = "1.6.10"
repositories {
google()
mavenCentral()
//replugin地址
maven {url "http://maven.geelib.360.cn/nexus/repository/replugin/"}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//replugin插件插件
classpath 'com.qihoo360.replugin:replugin-plugin-gradle:3.0.0'
//replugin宿主插件
classpath "com.qihoo360.replugin:replugin-host-gradle:3.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
//replugin的地址
maven {url "http://maven.geelib.360.cn/nexus/repository/replugin/"}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Android Studio の右上隅に同期を求めるメッセージが表示されます。クリックして更新します。
1.3 App の Gradle バージョンを変更する
Android クロージャの下に追加します。
apply plugin: 'replugin-host-gradle'
依存関係クロージャにrepluginの依存関係を追加します。
implementation "com.qihoo360.replugin:replugin-host-lib:3.0.0"
すべてのコードは次のようになります。
plugins {
id 'com.android.application'
id 'kotlin-android-extensions'
id 'kotlin-android'
}
android {
compileSdkVersion 28
buildToolsVersion "28.0.0"
defaultConfig {
applicationId "com.hong.replugin360"
minSdkVersion 16
targetSdkVersion 28
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
//必须在Android的闭包下
apply plugin: 'replugin-host-gradle'
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//添加replugin插件依赖
implementation "com.qihoo360.replugin:replugin-host-lib:3.0.0"
}
プロジェクトのbuild.gradleの下にある依存関係クロージャのgradle は3.5.4 、つまりcom.android.tools.build:gradle:3.5.4 であるため、 appcompatと、materialとconstraintlayoutのバージョンが異なることに注意してください。高すぎると互換性がありません。忘れずに同期してください。
引き続き、vaulesのThemes.xml内のテーマを変更し(テーマが見つからない場合は、ここで変更しましょう)、テーマを次のように変更します。
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Replugin360" parent="Theme.Design.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<!-- Status bar color. -->
<!-- Customize your theme here. -->
</style>
</resources>
ここでは、テーマをTheme.Design.Light.NoActionBarに変更し、他の存在しない色を削除します。
Repluginプラグインをプロジェクトに接続しました。忘れずに同期してください。次に、デモを書いて練習してみましょう
2.実戦リプラグイン
1. Appクラスを作成します。
RePluginApplicationクラスを統合するために、App モジュールの下に新しいAppクラスを作成します。コードは次のとおりです。
import com.qihoo360.replugin.RePluginApplication;
public class App extends RePluginApplication {
}
AndroidManifest.xmlでAppクラスを次のように構成します。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hong.replugin360">
<!--申请读写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--将App类添加到配置文件中-->
<application
android:name=".App"
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/Theme.Replugin360">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity のレイアウト ファイルを次のように変更します。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="跳转至插件"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="跳转至第二界面"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java のコードは次のとおりです。
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import com.qihoo360.replugin.RePlugin;
public class MainActivity extends AppCompatActivity {
private Button btn;
private Button secondBtn;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
private int REQUEST_PERMISSION_CODE = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = this.findViewById(R.id.button);
secondBtn = this.findViewById(R.id.button2);
//申请文件读写权限
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
}
}
}
//文件读写权限回传
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_CODE) {
for (int i = 0; i < permissions.length; i++) {
Log.i("MainActivity", "申请的权限为:" + permissions[i] + ",申请结果:" + grantResults[i]);
}
}
}
}
このようにして、ホスト コードが記述され、次にプラグイン APK のコードを記述します。
次の図に示すように、ルート プロジェクトの下にモジュール module を作成します。
以下に示すように、Phone&Table モジュールを選択します。
次のように、plugin1モジュールのbuild.gradle の下にプラグインの依存関係を追加します。
apply plugin: 'replugin-plugin-gradle'
repluginPluginConfig {
pluginName = "plugin1"
hostApplicationId = "com.hong.replugin360"
hostAppLauncherActivity = "com.hong.replugin360.MainActivity"
}
上記のコードは Android クロージャーの下にある必要があることに注意してください。
完全なコードは次のとおりです。
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 28
buildToolsVersion "28.0.0"
defaultConfig {
applicationId "com.hong.plugin1"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
apply plugin: 'replugin-plugin-gradle'
repluginPluginConfig {
pluginName = "plugin1"
hostApplicationId = "com.hong.replugin360"
hostAppLauncherActivity = "com.hong.replugin360.MainActivity"
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.qihoo360.replugin:replugin-plugin-lib:3.0.0'
}
MainActivity の plugin1 モジュールのレイアウトを次のように変更します。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Plugin!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
また、テーマをThemes.xmlファイル内のテーマに切り替える必要があります。これについては上記と同じなので、ここでは詳しく説明しません。
plugin1モジュールをAPKにパッケージ化するには、デバッグバージョンではなくリリースバージョンにパッケージ化することに注意する必要があります。
plugin1モジュールを選択し、 Android Studio の上部バーにあるビルドメニューをクリックし、 [Generate Signed Bundle /Apk]オプションを選択します。
リリースバージョンを選択するだけです
次の図に示すように、 app moduleの下にassetsディレクトリを作成し、assets directory の下にpluginsディレクトリを作成し、前にパッケージ化したプラグインの apk サフィックスを .jar に変更します。
アプリ モジュールの MainActivity.java を次のように変更します。
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import com.qihoo360.replugin.RePlugin;
public class MainActivity extends AppCompatActivity {
private Button btn;
private Button secondBtn;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
private int REQUEST_PERMISSION_CODE = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = this.findViewById(R.id.button);
secondBtn = this.findViewById(R.id.button2);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
}
}
btn.setOnClickListener(v -> {
Intent intent = RePlugin.createIntent("com.hong.plugin1", "com.hong.plugin1.MainActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RePlugin.startActivity(this, intent);
});
secondBtn.setOnClickListener(v -> startActivity(new Intent(this, SecondActivity.class)));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_CODE) {
for (int i = 0; i < permissions.length; i++) {
Log.i("MainActivity", "申请的权限为:" + permissions[i] + ",申请结果:" + grantResults[i]);
}
}
}
}
クリックして実行すると、次のような効果が得られます。
これで Replugin フレームワークへのアクセスが完了しました