Skip to content

Commit

Permalink
Use Android linter from cmdline-tools (flutter#28153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Garcia authored Aug 18, 2021
1 parent b13c765 commit a27da3e
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 221 deletions.
11 changes: 11 additions & 0 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,17 @@ deps = {
'dep_type': 'cipd',
},

'src/third_party/android_tools/sdk/cmdline-tools': {
'packages': [
{
'package': 'flutter/android/sdk/cmdline-tools',
'version': 'latest',
}
],
'condition': 'download_android_deps',
'dep_type': 'cipd',
},

'src/third_party/android_tools/sdk/licenses': {
'packages': [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* The most obvious example of when this may come in handy is if an application wishes to subclass
* the Android v4 support library's {@code FragmentActivity}.
*
* <p><b>Usage:</b></p>
* <p><b>Usage:</b>
*
* <p>To wire this class up to your activity, simply forward the events defined in {@link
* FlutterActivityEvents} from your activity to an instance of this class. Optionally, you can make
Expand Down Expand Up @@ -425,9 +425,7 @@ private Boolean showSplashScreenUntilFirstFrame() {
ActivityInfo activityInfo =
activity
.getPackageManager()
.getActivityInfo(
activity.getComponentName(),
PackageManager.GET_META_DATA | PackageManager.GET_ACTIVITIES);
.getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
Bundle metadata = activityInfo.metaData;
return metadata != null && metadata.getBoolean(SPLASH_SCREEN_META_DATA_KEY);
} catch (NameNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.flutter.embedding.android;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -99,6 +100,7 @@ private static void logW(String format, Object... args) {
}

@TargetApi(19)
@SuppressLint("WrongConstant") // RGBA_8888 is a valid constant.
@NonNull
private static ImageReader createImageReader(int width, int height) {
if (width <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -438,6 +439,7 @@ public void itWithMetadataWithoutSplashScreenResourceKeyDoesNotProvideSplashScre

static class FlutterActivityWithProvidedEngine extends FlutterActivity {
@Override
@SuppressLint("MissingSuperCall")
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.delegate = new FlutterActivityAndFragmentDelegate(this);
super.delegate.setupFlutterEngine();
Expand Down
66 changes: 12 additions & 54 deletions tools/android_lint/bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const LocalProcessManager processManager = LocalProcessManager();
/// Java 1.8.
Future<void> main(List<String> args) async {
final ArgParser argParser = setupOptions();
await checkJava1_8();
final int exitCode = await runLint(argParser, argParser.parse(args));
exit(exitCode);
}
Expand Down Expand Up @@ -74,7 +73,7 @@ Future<int> runLint(ArgParser argParser, ArgResults argResults) async {
<!-- WILL AUTOMATICALLY FIND ALL .java FILES AND INCLUDE THEM HERE -->
<project>
<sdk dir="${androidSdkDir.path}" />
<module name="FlutterEngine" android="true" library="true" compile-sdk-version="android-P">
<module name="FlutterEngine" android="true" library="true" compile-sdk-version="android-S">
<manifest file="${path.join(androidDir.path, 'AndroidManifest.xml')}" />
''');
for (final FileSystemEntity entity in androidDir.listSync(recursive: true)) {
Expand All @@ -89,12 +88,11 @@ Future<int> runLint(ArgParser argParser, ArgResults argResults) async {
</project>
''');
await projectXml.close();

print('Wrote project.xml, starting lint...');
final List<String> lintArgs = <String>[
path.join(androidSdkDir.path, 'tools', 'bin', 'lint'),
'--project',
projectXmlPath,
path.join(androidSdkDir.path, 'cmdline-tools', 'bin', 'lint'),
'--project', projectXmlPath,
'--compile-sdk-version', '31',
'--showall',
'--exitcode', // Set non-zero exit code on errors
'-Wall',
Expand All @@ -106,14 +104,13 @@ Future<int> runLint(ArgParser argParser, ArgResults argResults) async {
if (html) {
lintArgs.addAll(<String>['--html', argResults['out'] as String]);
}
final String? javaHome = await getJavaHome();
final String javahome = getJavaHome(inArgument);
print('Using JAVA_HOME=$javahome');
final Process lintProcess = await processManager.start(
lintArgs,
environment: javaHome != null
? <String, String>{
'JAVA_HOME': javaHome,
}
: null,
environment: <String, String>{
'JAVA_HOME': javahome,
},
);
lintProcess.stdout.pipe(stdout);
lintProcess.stderr.pipe(stderr);
Expand Down Expand Up @@ -166,50 +163,11 @@ ArgParser setupOptions() {
return argParser;
}

/// On macOS, we can try to find Java 1.8.
///
/// Otherwise, default to whatever JAVA_HOME is already.
Future<String?> getJavaHome() async {
String getJavaHome(String src) {
if (Platform.isMacOS) {
final ProcessResult result = await processManager.run(
<String>['/usr/libexec/java_home', '-v', '1.8', '-F'],
);
if (result.exitCode == 0) {
return (result.stdout as String).trim();
}
}
return Platform.environment['JAVA_HOME'];
}

/// Checks that `java` points to Java 1.8.
///
/// The SDK lint tool may not work with Java > 1.8.
Future<void> checkJava1_8() async {
print('Checking Java version...');

if (Platform.isMacOS) {
final ProcessResult result = await processManager.run(
<String>['/usr/libexec/java_home', '-v', '1.8', '-F'],
);
if (result.exitCode != 0) {
print('Java 1.8 not available - the linter may not work properly.');
}
return;
}
final ProcessResult javaResult = await processManager.run(
<String>['java', '-version'],
);
if (javaResult.exitCode != 0) {
print('Could not run "java -version". '
'Ensure Java is installed and available on your path.');
print(javaResult.stderr);
}
// `java -version` writes to stderr.
final String javaVersionStdout = javaResult.stderr as String;
if (!javaVersionStdout.contains('"1.8')) {
print('The Android SDK tools may not work properly with your Java version. '
'If this process fails, please retry using Java 1.8.');
return path.normalize('$src/third_party/java/openjdk/Contents/Home/');
}
return path.normalize('$src/third_party/java/openjdk/');
}

/// The root directory of this project.
Expand Down
Loading

0 comments on commit a27da3e

Please sign in to comment.