flutter混淆编译生成releaseAPK文件

flutter 学习demo记录:https://github.com/carlzhan/learning_flutter

喜欢的随手来个star,谢谢

通过命令生成key.jks文件

keytool -genkey -v -keystore 秘钥文件目录/名称.jks -keyalg RSA -keysize 2048 -validity 有效天数 -alias 昵称 

keytool -genkey -v -keystore ./key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

在目录android->app->build.gradle中配置

def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
...
signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
 buildTypes {
        release {
            signingConfig signingConfigs.debug

            minifyEnabled true
            useProguard true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

	    dependencies {
	        implementation 'com.android.support:support-fragment:28.0.0'
	    }
        }
    }
...
}

在目录android下创建文件key.properties

storePassword=123456
keyPassword=123456
keyAlias=key
storeFile=../key.jks

在android->app目录下创建混淆文件proguard-rules.pro

#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }

命令行中执行命令:flutter build apk

就可以在\build\app\outputs\apk\release目录中看到app-release.apk文件

常见混淆失败报错:
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'void setContentView(android.view.View)' in program class io.flutter.embedding.android.FlutterActi
vity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'void addContentView(android.view.View,android.view.ViewGroup$LayoutParams)' in program class io.f
lutter.embedding.android.FlutterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.content.res.Resources$Theme getTheme()' in program class io.flutter.embedding.android.Flu
tterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.content.res.Resources getResources()' in program class io.flutter.embedding.android.Flutt
erActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.view.Window getWindow()' in program class io.flutter.embedding.android.FlutterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.support.v4.app.FragmentManager getSupportFragmentManager()' in program class io.flutter.e
mbedding.android.FlutterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.content.Intent getIntent()' in program class io.flutter.embedding.android.FlutterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.content.Context getApplicationContext()' in program class io.flutter.embedding.android.Fl
utterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.content.pm.PackageManager getPackageManager()' in program class io.flutter.embedding.andr
oid.FlutterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.content.ComponentName getComponentName()' in program class io.flutter.embedding.android.F
lutterActivity
Warning: io.flutter.embedding.android.FlutterActivity: can't find referenced method 'android.content.pm.ApplicationInfo getApplicationInfo()' in program class io.flutter.embedding.an
droid.FlutterActivity
Warning: io.flutter.embedding.android.FlutterFragment: can't find referenced method 'void setArguments(android.os.Bundle)' in program class io.flutter.embedding.android.FlutterFragme
nt
Warning: io.flutter.embedding.android.FlutterFragment: can't find referenced method 'android.support.v4.app.FragmentActivity getActivity()' in program class io.flutter.embedding.andr
oid.FlutterFragment
Warning: io.flutter.embedding.android.FlutterFragment: can't find referenced method 'android.os.Bundle getArguments()' in program class io.flutter.embedding.android.FlutterFragment
Warning: io.flutter.embedding.android.FlutterFragment: can't find referenced method 'android.content.Context getContext()' in program class io.flutter.embedding.android.FlutterFragme
nt
Warning: io.flutter.embedding.android.FlutterFragment: can't find referenced method 'android.content.res.Resources getResources()' in program class io.flutter.embedding.android.Flutt
erFragment
Warning: io.flutter.embedding.android.FlutterFragment$1: can't find referenced method 'android.support.v4.app.FragmentActivity getActivity()' in program class io.flutter.embedding.an
droid.FlutterFragment
Warning: io.flutter.embedding.android.FlutterFragment$Builder: can't find referenced method 'void setArguments(android.os.Bundle)' in program class io.flutter.embedding.android.Flutt
erFragment
Warning: there were 18 unresolved references to program class members.
         Your input classes appear to be inconsistent.
         You may need to recompile the code.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
Warning: Exception while processing task java.io.IOException: Please correct the above warnings first.
Thread(Tasks limiter_10): destruction

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
> Job failed, see logs for details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done                       4.7s
*******************************************************************************************
The Gradle failure may have been because of AndroidX incompatibilities in this Flutter app.
See https://goo.gl/CP92wY for more information on the problem and how to fix it.
*******************************************************************************************
Gradle task assembleRelease failed with exit code 1

The Flutter Android Java code contains references to the Android fragment support library. But the default template Flutter app does not use fragments, so the template build.gradle script does not link in the fragment support library in order to reduce APK binary size.

However, Proguard will scan all the classes in the app and will generate the errors seen above unless it can find the fragment support classes.

You may need to declare a different dependency if you app uses the AndroidX libraries instead of support-fragment.

For example:

buildTypes {
        release {
            signingConfig signingConfigs.debug

            minifyEnabled true
            useProguard true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

	    dependencies {
	        implementation 'com.android.support:support-fragment:28.0.0'
	    }
        }
    }

猜你喜欢

转载自blog.csdn.net/Qyuewei/article/details/90073735